//@version=3 study("Engulfing Candles", overlay = true) //strategy("Engulfing Candles") //keep this commented out unless backtesting openBarPrevious = open[1] closeBarPrevious = close[1] openBarCurrent = open closeBarCurrent = close //If current bar open is less than equal to the previous bar close AND current bar open is less than previous bar open AND current bar close is greater than previous bar open THEN True bullishEngulfing = (openBarCurrent <= closeBarPrevious) and (openBarCurrent < openBarPrevious) and (closeBarCurrent > openBarPrevious) //If current bar open is greater than equal to previous bar close AND current bar open is greater than previous bar open AND current bar close is less than previous bar open THEN True bearishEngulfing = (openBarCurrent >= closeBarPrevious) and (openBarCurrent > openBarPrevious) and (closeBarCurrent < openBarPrevious) //bullishEngulfing/bearishEngulfing return a value of 1 or 0; if 1 then plot on chart, if 0 then don't plot plotshape(bullishEngulfing, style = shape.triangleup, location = location.belowbar, color = green, size = size.tiny) plotshape(bearishEngulfing, style = shape.triangledown , location = location.abovebar, color = red, size = size.tiny) alertcondition(bullishEngulfing, title = "Bullish Engulfing", message = "[CurrencyPair] [TimeFrame], Bullish candle engulfing previous candle") alertcondition(bearishEngulfing, title = "Bearish Engulfing", message = "[CurrencyPair] [TimeFrame], Bearish candle engulfing previous candle") //================================BACKTEST================================// // === INPUT BACKTEST RANGE === FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12) FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31) FromYear = input(defval = 2017, title = "From Year", minval = 2017) ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12) ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31) ToYear = input(defval = 9999, title = "To Year", minval = 2017) // === FUNCTION EXAMPLE === start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window window() => time >= start and time <= finish ? true : false // create function "within window of time" // === EXECUTION === //Keep this commented out unless backtesting // strategy.entry("L", strategy.long, 25000, when = bullishEngulfing == 1 and window()) // buy long when "within window of time" AND crossover // strategy.exit("exit", "L", profit = 1000, loss = 50) // strategy.entry("S", strategy.short, 25000, when = bearishEngulfing == 1 and window()) // buy long when "within window of time" AND crossover // strategy.exit("exit", "S", profit = 1000, loss = 50)