Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. //@version=3
  2. study("Engulfing Candles", overlay = true)
  3. //strategy("Engulfing Candles") //keep this commented out unless backtesting
  4.  
  5. openBarPrevious = open[1]
  6. closeBarPrevious = close[1]
  7. openBarCurrent = open
  8. closeBarCurrent = close
  9.  
  10. //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
  11. bullishEngulfing = (openBarCurrent <= closeBarPrevious) and (openBarCurrent < openBarPrevious) and (closeBarCurrent > openBarPrevious)
  12. //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
  13. bearishEngulfing = (openBarCurrent >= closeBarPrevious) and (openBarCurrent > openBarPrevious) and (closeBarCurrent < openBarPrevious)
  14.  
  15. //bullishEngulfing/bearishEngulfing return a value of 1 or 0; if 1 then plot on chart, if 0 then don't plot
  16. plotshape(bullishEngulfing, style = shape.triangleup, location = location.belowbar, color = green, size = size.tiny)
  17. plotshape(bearishEngulfing, style = shape.triangledown , location = location.abovebar, color = red, size = size.tiny)
  18.  
  19. alertcondition(bullishEngulfing, title = "Bullish Engulfing", message = "[CurrencyPair] [TimeFrame], Bullish candle engulfing previous candle")
  20. alertcondition(bearishEngulfing, title = "Bearish Engulfing", message = "[CurrencyPair] [TimeFrame], Bearish candle engulfing previous candle")
  21.  
  22. //================================BACKTEST================================//
  23.  
  24. // === INPUT BACKTEST RANGE ===
  25. FromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)
  26. FromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
  27. FromYear = input(defval = 2017, title = "From Year", minval = 2017)
  28. ToMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
  29. ToDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
  30. ToYear = input(defval = 9999, title = "To Year", minval = 2017)
  31.  
  32. // === FUNCTION EXAMPLE ===
  33. start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
  34. finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
  35. window() => time >= start and time <= finish ? true : false // create function "within window of time"
  36.  
  37. // === EXECUTION === //Keep this commented out unless backtesting
  38. // strategy.entry("L", strategy.long, 25000, when = bullishEngulfing == 1 and window()) // buy long when "within window of time" AND crossover
  39. // strategy.exit("exit", "L", profit = 1000, loss = 50)
  40.  
  41. // strategy.entry("S", strategy.short, 25000, when = bearishEngulfing == 1 and window()) // buy long when "within window of time" AND crossover
  42. // strategy.exit("exit", "S", profit = 1000, loss = 50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement