Advertisement
NKactive

Untitled

Jul 29th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © NKactive
  3.  
  4. //@version=5
  5. strategy("EMA Crosses With Regime Filter Total Rework", overlay=true)
  6.  
  7. //Library
  8. import EliCobra/CobraMetrics/4 as cobra
  9. //// PConstruct Cobra Table
  10. disp_ind = input.string ("None" , title = "Display Curve" , tooltip = "Choose which data you would like to display", options=["Strategy", "Equity", "Open Profit", "Gross Profit", "Net Profit", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  11. pos_table = input.string("Middle Left", "Table Position", options = ["Top Left", "Middle Left", "Bottom Left", "Top Right", "Middle Right", "Bottom Right", "Top Center", "Bottom Center"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  12. type_table = input.string("None", "Table Type", options = ["Full", "Simple", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
  13. plot(cobra.curve(disp_ind))
  14. cobra.cobraTable(type_table, pos_table)
  15.  
  16. // Declare Functions for regime filter
  17. f_sec(_market, _res, _exp) => request.security(_market, _res, _exp[barstate.isconfirmed ? 0 : 1])
  18.  
  19. // *** Inputs ***
  20. //
  21. //Inputs for EMA lines. EMAshort plots EMA1 and should have a shorter length input than EMAlong which plots EMA2
  22. emaShort = input(defval=1, title="EMA Short Length", tooltip="Enter the length /(number of bars/) for the short EMA series in green", group="EMA Lengths")
  23. emaLong = input(defval=8, title="EMA Long Length", tooltip="Enter the length /(number of bars/) for the Long EMA series in red", group="EMA Lengths")
  24. // Inputs for Regime Filter
  25. regimeTimeFrame = input.timeframe(title="Regime Filter Timeframe", defval="D")
  26. regimeEMALen = input.int(title="Regime Filter EMA Length", defval=20)
  27. market = input.symbol(title="Market", defval="NASDAQ:NDX")
  28.  
  29. // *** Collect data ***
  30. // ema cross over data
  31. ema1 = ta.ema(close, emaShort) //also tried 50, 16, 9 for ema1
  32. ema2 = ta.ema(close, emaLong) //also tried 100, 20, 14 for ema2
  33.  
  34.  
  35. //
  36. // *** Calculations ***
  37. //
  38. // Get EMA value for regime filter
  39. marketPrice = f_sec(market, regimeTimeFrame, close)
  40. emaValue = ta.ema(marketPrice, regimeEMALen)
  41. //filterEma = ta.ema(close, regimeEMALen)
  42. //emaValue = f_sec(market, regimeTimeFrame, filterEma)
  43.  
  44. // Check if price is above or below EMA filter
  45. regimeFilter = marketPrice < emaValue or marketPrice[1] < emaValue[1] //bullish => true
  46. //
  47. // *** Logic For plotting ***
  48. //
  49. // Set variable to false before checking to see if we put on position
  50. triggerEMAcrossBuy = false
  51. triggerEMAcrossSell = false
  52. triggerRegimeBuy = false // allows only buy signals
  53. triggerRegimeSell = false // allows only sell signals
  54. triggerPositionBuy = false
  55. triggerPositionSell = false
  56. // Calculate signal for ema crossover
  57. triggerEMAcrossBuy := ta.crossover(ema1, ema2)
  58. triggerEMAcrossSell := ta.crossunder(ema1, ema2)
  59. triggerRegimeBuy := regimeFilter ? true : false // regimeFilter = true for bullish and allow buy
  60. triggerRegimeSell := regimeFilter ? false : true
  61. //Final Position Signal
  62. //triggerRegimeBuy := true
  63. //triggerRegimeSell := true
  64. triggerPositionBuy := triggerEMAcrossBuy and triggerRegimeBuy ? true : false
  65. triggerPositionSell := triggerEMAcrossSell and triggerRegimeSell ? true : false
  66.  
  67. // *** Plots for testing ***
  68. // plot crossing ema lines
  69. emaH=plot(ema1, color=color.green)
  70. emaL=plot(ema2, color=color.red)
  71. // crossing ema fill colours
  72. Bull = ema1>ema2
  73. BullCol = color.new(color.green, 60)
  74. Bear = ema1<ema2
  75. BearCol = color.new(color.red, 60)
  76. fill (emaL, emaH, color= Bull ? BullCol : Bear? BearCol :na)
  77. // Background colour to define regime filler bullish (Green) or Bearish (Red)
  78. rColbull = color.new(color.green, 90)
  79. rColbear = color.new(color.red, 90)
  80. bgcolor(regimeFilter ? rColbull : rColbear)
  81. // Regime EMA and Regime Market Price
  82. plot(emaValue, color = color.orange, title = "ema Value")
  83. plot(marketPrice, color = color.yellow, title = "Market Price")
  84. //
  85. // *** Test Signals ***
  86. //
  87. plotshape(triggerPositionBuy ? triggerPositionBuy : na, title = "Buy", location = location.belowbar, style = shape.triangleup, color=color.green)
  88. plotshape(triggerPositionSell ? triggerPositionSell : na, title = "sell",location = location.abovebar, style = shape.triangledown, color=color.red)
  89. //
  90. // *** Strategy Positions ***
  91. //
  92. if triggerPositionBuy
  93. strategy.entry("My Long Entry Id", strategy.long)
  94. if triggerPositionSell
  95. strategy.entry("My Short Entry Id", strategy.short)
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement