Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- strategy(title="STAGGONAUT .12.23", initial_capital=10000, slippage=1, default_qty_value=100, pyramiding=0, default_qty_type=strategy.percent_of_equity, process_orders_on_close=true,shorttitle="STGN", format=format.price, precision=4)
- useDateFilter = input.bool(true, title="Filter Date Range of Backtest",
- group="Backtest Time Period")
- backtestStartDate = input.time(timestamp("1 Jan 2018"),
- title="Start Date", group="Backtest Time Period",
- tooltip="This start date is in the time zone of the exchange " +
- "where the chart's instrument trades. It doesn't use the time " +
- "zone of the chart or of your computer.")
- backtestEndDate = input.time(timestamp("1 Jan 2092"),
- title="End Date", group="Backtest Time Period",
- tooltip="This end date is in the time zone of the exchange " +
- "where the chart's instrument trades. It doesn't use the time " +
- "zone of the chart or of your computer.")
- //Range Conditions
- inDateRange = not useDateFilter or (time >= backtestStartDate and
- time < backtestEndDate)
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- //_________________________________________________________________________________________________________________
- ///////////////////////////////////////////////////////////////STC
- // STC Indicator
- fastLength = input(title='MACD Fast Length', defval=9,group = "STC")
- slowLength = input(title='MACD Slow Length', defval=34,group = "STC")
- cycleLength = input(title='Cycle Length', defval=11,group = "STC")
- d1Length = input(title='1st %D Length', defval=1,group = "STC")
- d2Length = input(title='2nd %D Length', defval=4,group = "STC")
- src = close
- upper = 75
- lower = 25
- macd = ta.ema(src, fastLength) - ta.ema(src, slowLength)
- k = nz(fixnan(ta.stoch(macd, macd, macd, cycleLength)))
- d = ta.ema(k, d1Length)
- kd = nz(fixnan(ta.stoch(d, d, d, cycleLength)))
- stc = ta.ema(kd, d2Length)
- stc := math.max(math.min(stc, 100), 0)
- STCLong = ta.crossover(stc, lower)
- STCShort = ta.crossunder(stc, upper)
- //RSI
- timeframersi = input.string("1D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "RSI Settings")
- ma(source, length, type) =>
- switch type
- "SMA" => ta.sma(source, length)
- "EMA" => ta.ema(source, length)
- "SMMA (RMA)" => ta.rma(source, length)
- "WMA" => ta.wma(source, length)
- "VWMA" => ta.vwma(source, length)
- // RSI Settings
- rsiLengthInput = input.int(24, minval=1, title="RSI Length", group="RSI Settings")
- rsiSourceInput = input.source(close, "Source", group="RSI Settings")
- // Overbought and Oversold Level Inputs
- overboughtLevel = input.float(53, title="Overbought Level", group="RSI Settings")
- oversoldLevel = input.float(54, title="Oversold Level", group="RSI Settings")
- // RSI Calculation
- up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
- down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
- rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
- // SMA and EMA Length Inputs
- //smaLength = input.int(14, title="SMA Length")
- emaLength = input.int(14, title="EMA Length")
- // SMA and EMA of RSI
- //smaRsi = ta.sma(rsi, smaLength)
- emaRsi = ta.ema(rsi, emaLength)
- //plot(smaRsi, color=color.green, title="SMA of RSI")
- plot(emaRsi, color=color.red, title="EMA of RSI")
- plot(rsi, "RSI", color=color.rgb(243, 173, 33))
- //hline(overboughtLevel, "Overbought Level", color=color.red)
- //hline(oversoldLevel, "Oversold Level", color=color.green)
- Longrsi = rsi > emaRsi //ta.crossover(rsi,oversoldLevel)
- Shortrsi = rsi <emaRsi //ta.crossunder(rsi,overboughtLevel)
- longrsi = request.security(syminfo.tickerid,timeframersi,Longrsi)
- shortrsi = request.security(syminfo.tickerid,timeframersi,Shortrsi)
- //BB%
- timeframebb = input.string("2D", "Days", ["1D", "2D", "3D", "4D", "5D", "6D", "7D"],group = "BB Settings")
- length_bb = input.int(24 , title="BB Length",group = "BB Settings")
- mult = 2
- src_bb = input(close, title="BB Source",group = "BB Settings")
- basis = ta.sma(src_bb, length_bb)
- dev = mult * ta.stdev(src_bb, length_bb)
- upper_bb = basis + dev
- lower_bb = basis - dev
- bb_percent = (src_bb - lower_bb) / (upper_bb - lower_bb)
- bb_Long = bb_percent > 0.5
- bb_Short = bb_percent < 0.5
- bb_Long_cn = request.security(syminfo.tickerid,timeframebb,bb_Long)
- bb_Short_cn = request.security(syminfo.tickerid,timeframebb,bb_Short)
- //AFR
- p = input(100, "Period",group= "AFR")
- atr_factor = input.float(0.9, "Factor", step = 0.005,group= "AFR")
- // Calculations
- atr = ta.atr(p)
- e = atr * atr_factor
- afr = close
- afr := nz(afr[1], afr)
- atr_factoryHigh = close + e
- atr_factoryLow = close - e
- if atr_factoryLow > afr
- afr := atr_factoryLow
- if atr_factoryHigh < afr
- afr := atr_factoryHigh
- buy = afr > afr[1] and not (afr[1] > afr[2])
- sell = afr < afr[1] and not (afr[1] < afr[2])
- ls = 0
- ls := buy ? 1 : sell ? -1 : ls[1]
- AFRLong = buy and ls != ls[1]
- AFRShort = sell and ls != ls[1]
- //Super
- atrPeriod = input.int(1, "ATR Length", minval = 1,group= "SUPER")
- factor = input.float(1, "Factor", minval = 0.01, step = 0.01,group= "SUPER")
- [supertrend, direction] = ta.supertrend(factor, atrPeriod)
- superlong = direction < 0
- supershort = direction > 0
- // TRADE CONDITIONS
- moon = (longrsi and superlong)
- hades = STCShort and supershort
- // Strategy Execution
- if moon and inDateRange
- strategy.entry("Long", strategy.long)
- if hades and inDateRange
- strategy.entry("Short", strategy.short)
- import EliCobra/CobraMetrics/4 as cobra
- //// PLOT DATA
- disp_ind = input.string ("Equity" , 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("Simple", "Table Type", options = ["Full", "Simple", "None"], group = "🐍 𝓒𝓸𝓫𝓻𝓪 𝓜𝓮𝓽𝓻𝓲𝓬𝓼 🐍")
- plot(cobra.curve(disp_ind))
- cobra.cobraTable(type_table, pos_table)
Advertisement