NKactive

Untitled

Aug 1st, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 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", group = "Regime Filter")
  26. regimeEMALen = input.int(title="Regime Filter EMA Length", defval=20, group = "Regime Filter")
  27. market = input.symbol(title="Market", defval="NASDAQ:NDX", group = "Regime Filter")
  28. //Inputs to turn testing plots off
  29. EMACrossOverPlotsTrue = input.bool(defval=false, title="Turn On CrossOver EMA Plots", group="Test Plots")
  30. EMACrossOverTradesTrue = input.bool(defval=false, title="Turn On CrossOver EMA Trades", group="Test Plots")
  31. RegimeFilterEMATrue = input.bool(defval=false, title="Turn On RegimeFilter EMA", group="Test Plots")
  32. RegimeFilterColourTrue = input.bool(defval=false, title="Turn On RegimeFilter Colours", group="Test Plots")
  33.  
  34.  
  35. // *** Collect data ***
  36. // ema cross over data
  37. ema1 = ta.ema(close, emaShort) //also tried 50, 16, 9 for ema1
  38. ema2 = ta.ema(close, emaLong) //also tried 100, 20, 14 for ema2
  39. //
  40. // *** Calculations ***
  41. //
  42. // Get EMA value for regime filter
  43. marketPrice = f_sec(market, regimeTimeFrame, close)
  44. emaValue = ta.ema(marketPrice, regimeEMALen)
  45. //filterEma = ta.ema(close, regimeEMALen)
  46. //emaValue = f_sec(market, regimeTimeFrame, filterEma)
  47.  
  48. // Check if price is above or below EMA filter
  49. regimeFilter = marketPrice < emaValue or marketPrice[1] < emaValue[1] //bullish => true
  50. //
  51. // *** Logic For plotting ***
  52. //
  53. // Set variable to false before checking to see if we put on position
  54. triggerEMAcrossBuy = false
  55. triggerEMAcrossSell = false
  56. triggerRegimeBuy = false // allows only buy signals
  57. triggerRegimeSell = false // allows only sell signals
  58. triggerPositionBuy = false
  59. triggerPositionSell = false
  60. // Calculate signal for ema crossover
  61. triggerEMAcrossBuy := ta.crossover(ema1, ema2)
  62. triggerEMAcrossSell := ta.crossunder(ema1, ema2)
  63. triggerRegimeBuy := regimeFilter ? true : false // regimeFilter = true for bullish and allow buy
  64. triggerRegimeSell := regimeFilter ? false : true
  65. //Final Position Signal
  66. //triggerRegimeBuy := true
  67. //triggerRegimeSell := true
  68. triggerPositionBuy := triggerEMAcrossBuy and triggerRegimeBuy ? true : false
  69. triggerPositionSell := triggerEMAcrossSell and triggerRegimeSell ? true : false
  70.  
  71. // *** Plots for testing ***
  72. // plot crossing ema lines
  73.  
  74. emaH=plot(EMACrossOverPlotsTrue? ema1 : na, color=color.green)
  75. emaL=plot(EMACrossOverPlotsTrue? ema2 : na, color=color.red)
  76. // crossing ema fill colours
  77. Bull = ema1>ema2
  78. BullCol = color.new(color.green, 60)
  79. Bear = ema1<ema2
  80. BearCol = color.new(color.red, 60)
  81. fill (emaL, emaH, color= Bull ? BullCol : Bear? BearCol :na)
  82. // Background colour to define regime filler bullish (Green) or Bearish (Red)
  83. rColbull = color.new(color.green, 100)
  84. rColbear = color.new(color.red, 100)
  85. if RegimeFilterColourTrue
  86. rColbull := color.new(color.green, 90)
  87. rColbear := color.new(color.red, 90)
  88. bgcolor(regimeFilter ? rColbull : rColbear)
  89. // Regime EMA and Regime Market Price
  90. plot(RegimeFilterEMATrue ? emaValue : na, color = color.orange, title = "ema Value")
  91. plot(RegimeFilterEMATrue ? marketPrice : na, color = color.yellow, title = "Market Price")
  92. //
  93. // *** Test Signals ***
  94. //
  95. plotshape(triggerPositionBuy and EMACrossOverTradesTrue ? triggerPositionBuy : na, title = "Buy", location = location.belowbar, style = shape.triangleup, color=color.green)
  96. plotshape(triggerPositionSell and EMACrossOverTradesTrue ? triggerPositionSell : na, title = "sell",location = location.abovebar, style = shape.triangledown, color=color.red)
  97. //
  98. // *** Strategy Positions ***
  99. //
  100. if triggerPositionBuy
  101. strategy.entry("My Long Entry Id", strategy.long)
  102. if triggerPositionSell
  103. strategy.entry("My Short Entry Id", strategy.short)
  104.  
Add Comment
Please, Sign In to add comment