Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © NKactive
- //@version=5
- strategy("EMA Crosses With Regime Filter Total Rework", overlay=true)
- //Library
- import EliCobra/CobraMetrics/4 as cobra
- //// PConstruct Cobra Table
- 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 = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
- 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 = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
- type_table = input.string("None", "Table Type", options = ["Full", "Simple", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
- plot(cobra.curve(disp_ind))
- cobra.cobraTable(type_table, pos_table)
- // Declare Functions for regime filter
- f_sec(_market, _res, _exp) => request.security(_market, _res, _exp[barstate.isconfirmed ? 0 : 1])
- // *** Inputs ***
- //
- //Inputs for EMA lines. EMAshort plots EMA1 and should have a shorter length input than EMAlong which plots EMA2
- 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")
- 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")
- // Inputs for Regime Filter
- regimeTimeFrame = input.timeframe(title="Regime Filter Timeframe", defval="D", group = "Regime Filter")
- regimeEMALen = input.int(title="Regime Filter EMA Length", defval=20, group = "Regime Filter")
- market = input.symbol(title="Market", defval="NASDAQ:NDX", group = "Regime Filter")
- //Inputs to turn testing plots off
- EMACrossOverPlotsTrue = input.bool(defval=false, title="Turn On CrossOver EMA Plots", group="Test Plots")
- EMACrossOverTradesTrue = input.bool(defval=false, title="Turn On CrossOver EMA Trades", group="Test Plots")
- RegimeFilterEMATrue = input.bool(defval=false, title="Turn On RegimeFilter EMA", group="Test Plots")
- RegimeFilterColourTrue = input.bool(defval=false, title="Turn On RegimeFilter Colours", group="Test Plots")
- // *** Collect data ***
- // ema cross over data
- ema1 = ta.ema(close, emaShort) //also tried 50, 16, 9 for ema1
- ema2 = ta.ema(close, emaLong) //also tried 100, 20, 14 for ema2
- //
- // *** Calculations ***
- //
- // Get EMA value for regime filter
- marketPrice = f_sec(market, regimeTimeFrame, close)
- emaValue = ta.ema(marketPrice, regimeEMALen)
- //filterEma = ta.ema(close, regimeEMALen)
- //emaValue = f_sec(market, regimeTimeFrame, filterEma)
- // Check if price is above or below EMA filter
- regimeFilter = marketPrice < emaValue or marketPrice[1] < emaValue[1] //bullish => true
- //
- // *** Logic For plotting ***
- //
- // Set variable to false before checking to see if we put on position
- triggerEMAcrossBuy = false
- triggerEMAcrossSell = false
- triggerRegimeBuy = false // allows only buy signals
- triggerRegimeSell = false // allows only sell signals
- triggerPositionBuy = false
- triggerPositionSell = false
- // Calculate signal for ema crossover
- triggerEMAcrossBuy := ta.crossover(ema1, ema2)
- triggerEMAcrossSell := ta.crossunder(ema1, ema2)
- triggerRegimeBuy := regimeFilter ? true : false // regimeFilter = true for bullish and allow buy
- triggerRegimeSell := regimeFilter ? false : true
- //Final Position Signal
- //triggerRegimeBuy := true
- //triggerRegimeSell := true
- triggerPositionBuy := triggerEMAcrossBuy and triggerRegimeBuy ? true : false
- triggerPositionSell := triggerEMAcrossSell and triggerRegimeSell ? true : false
- // *** Plots for testing ***
- // plot crossing ema lines
- emaH=plot(EMACrossOverPlotsTrue? ema1 : na, color=color.green)
- emaL=plot(EMACrossOverPlotsTrue? ema2 : na, color=color.red)
- // crossing ema fill colours
- Bull = ema1>ema2
- BullCol = color.new(color.green, 60)
- Bear = ema1<ema2
- BearCol = color.new(color.red, 60)
- fill (emaL, emaH, color= Bull ? BullCol : Bear? BearCol :na)
- // Background colour to define regime filler bullish (Green) or Bearish (Red)
- rColbull = color.new(color.green, 100)
- rColbear = color.new(color.red, 100)
- if RegimeFilterColourTrue
- rColbull := color.new(color.green, 90)
- rColbear := color.new(color.red, 90)
- bgcolor(regimeFilter ? rColbull : rColbear)
- // Regime EMA and Regime Market Price
- plot(RegimeFilterEMATrue ? emaValue : na, color = color.orange, title = "ema Value")
- plot(RegimeFilterEMATrue ? marketPrice : na, color = color.yellow, title = "Market Price")
- //
- // *** Test Signals ***
- //
- plotshape(triggerPositionBuy and EMACrossOverTradesTrue ? triggerPositionBuy : na, title = "Buy", location = location.belowbar, style = shape.triangleup, color=color.green)
- plotshape(triggerPositionSell and EMACrossOverTradesTrue ? triggerPositionSell : na, title = "sell",location = location.abovebar, style = shape.triangledown, color=color.red)
- //
- // *** Strategy Positions ***
- //
- if triggerPositionBuy
- strategy.entry("My Long Entry Id", strategy.long)
- if triggerPositionSell
- strategy.entry("My Short Entry Id", strategy.short)
Add Comment
Please, Sign In to add comment