MACD crossover and EMA strategy
This simple strategy uses the MACD indicator along with the EMA trend line. For the buy signal, the strategy looks for a cross-over of the MACD line over the signal line and the price is above the 50-period EMA line. The MACD line uses 12 and 26 periods for the MACD and 9 periods for the signal line by default. But the user can change these values. The EMA line uses a 50-period EMA.
The test of the strategy was done on SQQQ only.
The script is written in Pine script and tested on Trading View.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © facelessfinance
//@version=5
strategy("MACD strategy", overlay=true, initial_capital = 50000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)
// MACD Inputs
input_macd_fast_line = input.int(defval = 12, title = 'MACD Fast Line', step = 1, tooltip = 'MACD Fast Line')
input_macd_slow_line = input.int(defval = 26, title = 'MACD Slow Line', step = 1, tooltip = 'MACD Slow Line')
input_macd_smooth_signal = input.int(defval = 9, title = 'MACD smoothing signal', step = 1, tooltip = 'MACD smoothing signal')
// time frame
input_startTime = input.time(title = 'Start time', defval = timestamp("01 Jan 2020 13:30 +0000"), group = 'Time Filter', tooltip = 'Start date & time')
input_endTime = input.time(title = 'End time', defval = timestamp("1 Jan 2099 19:30 +0000"), group = 'Time Filter', tooltip = 'End date & time')
// date filter
filter_date = time >= input_startTime and time <= input_endTime
// EMA inputs
input_ema1 = input.int(defval = 50, title = 'EMA 1', step = 10, tooltip = 'EMA 1')
// Profit stop loss input
input_take_profit = input.float(defval = 0.01, title = 'Take Profit %', step = 0.01, tooltip = 'Take Profit %')
input_stop_loss = input.float(defval = 0.01, title = 'Stop Loss %', step = 0.01, tooltip = 'Stop Loss %')
[macdLine, signalLine, smooth] = ta.macd(close, input_macd_fast_line, input_macd_slow_line, input_macd_smooth_signal)
// MACD line below zero condition
macd_below_zero = false
if macdLine < 0 and signalLine < 0
macd_below_zero := true
// MACD Crossover condition
bullish_macd_crossover = false
if ta.crossover(macdLine, signalLine)
bullish_macd_crossover := true
// EMA condition
bullish_trend = false
if close > ta.ema(close, input_ema1) and (close - ta.ema(close, input_ema1)) > 1
bullish_trend := true
plot(ta.ema(close, input_ema1), color = color.yellow, linewidth = 3)
// buy sell condition
var float buy_price = 0
buy_condition = bullish_macd_crossover and macd_below_zero and bullish_trend and strategy.position_size == 0
take_profit_condition = strategy.position_size > 0 and close > buy_price + (buy_price * input_take_profit)
stop_loss_condition = strategy.position_size > 0 and close < buy_price - (buy_price * input_stop_loss)
if buy_condition
strategy.entry(id = 'long', direction = strategy.long)
if buy_condition[1]
buy_price := open
if take_profit_condition or stop_loss_condition
strategy.close(id = 'long', comment = (stop_loss_condition ? 'Stop Loss' : 'Take Profit'))
buy_price := na
Comments
Post a Comment