Skip to content

Instantly share code, notes, and snippets.

@itsXactlY
Created May 15, 2022 16:00
Show Gist options
  • Save itsXactlY/5c19602d8296a12d8ffd964f15775193 to your computer and use it in GitHub Desktop.
Save itsXactlY/5c19602d8296a12d8ffd964f15775193 to your computer and use it in GitHub Desktop.
Super Z++ Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//Original script
//https://www.tradingview.com/script/wYknDlLx-super-Z/
//@version=4
strategy("Super Z++ Strategy", shorttitle="Super Z++",overlay=true, initial_capital=50, slippage=3, calc_on_every_tick=true, calc_on_order_fills=true, default_qty_type=strategy.fixed, default_qty_value=5, pyramiding = 1, commission_type=strategy.commission.percent, commission_value=0.07)
// === INPUT BACKTEST RANGE ===
useDate = input(true, title='Use Date', type=input.bool)
FromMonth = input(defval=6, title="From Month", minval=1, maxval=12)
FromDay = input(defval=10, title="From Day", minval=1, maxval=31)
FromYear = input(defval=2020, title="From Year", minval=2017)
ToMonth = input(defval=1, title="To Month", minval=1, maxval=12)
ToDay = input(defval=1, title="To Day", minval=1, maxval=31)
ToYear = input(defval=9999, title="To Year", minval=2017)
start = timestamp(FromYear, FromMonth, FromDay, 00, 00) // backtest start window
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59) // backtest finish window
leverage = input(minval=1, defval=1,maxval=100, step=0.5, type=input.float, title = "Leverage: 1 = No leverage")
window() => // create function "within window of time"
time >= start and time <= finish ? true : false
// === INPUT BACKTEST RANGE ===
src5 = input(close)
tf = input(1440)
len5 = timeframe.isintraday and timeframe.multiplier >= 1 ?
tf / timeframe.multiplier * 7 :
timeframe.isintraday and timeframe.multiplier < 60 ?
60 / timeframe.multiplier * 24 * 7 : 7
ma = ema(src5*volume, len5) / ema(volume, len5)
//script taken from https://www.tradingview.com/script/kChCRRZI-Hull-Moving-Average/
src1 = ma
p(src1, len5) =>
n = 0.0
s = 0.0
for i = 0 to len5 - 1
w = (len5 - i) * len5
n := n + w
s := s + src5[i] * w
s / n
hm = 2.0 * p(src1, floor(len5 / 2)) - p(src1, len5)
vhma = p(hm, floor(sqrt(len5)))
lineColor = vhma > vhma[1] ? color.lime : color.red
plot(vhma, title="VHMA", color=lineColor ,linewidth=3)
hColor = true,vis = true
hu = hColor ? (vhma > vhma[2] ? #00ff00 : #ff0000) : #ff9800
vl = vhma[0]
ll = vhma[1]
m1 = plot(vl, color=hu, linewidth=1, transp=60)
m2 = plot(vis ? ll : na, color=hu, linewidth=2, transp=80)
fill(m1, m2, color=hu, transp=70)
//
b = timeframe.isintraday and timeframe.multiplier >= 1 ?
60 / timeframe.multiplier * 7 :
timeframe.isintraday and timeframe.multiplier < 60 ?
60 / timeframe.multiplier * 24 * 7 : 7
//
res5 = input("120", type=input.resolution)
o = security(syminfo.tickerid, res5, open, barmerge.gaps_off, barmerge.lookahead_on)
c = security(syminfo.tickerid, res5, close, barmerge.gaps_off, barmerge.lookahead_on)
hz = security(syminfo.tickerid, res5, high, barmerge.gaps_off, barmerge.lookahead_on)
l = security(syminfo.tickerid, res5, low, barmerge.gaps_off, barmerge.lookahead_on)
col = c >= o ? color.lime : color.red
ppo = plot(b ? o >= c ? hz : l : o, color=col, title="Open", style=plot.style_stepline, transp=100)
ppc = plot(b ? o <= c ? hz : l : c, color=col, title="Close", style=plot.style_stepline, transp=100)
plot(b and hz > c ? hz : na, color=col, title="High", style=plot.style_circles, linewidth=2,transp=60)
plot(b and l < c ? l : na, color=col, title="Low", style=plot.style_circles,linewidth=2, transp=60)
fill(ppo, ppc, col)
//
// INPUTS //
st_mult = input(1, title = 'SuperTrend Multiplier', minval = 0, maxval = 100, step = 0.01)
st_period = input(50, title = 'SuperTrend Period', minval = 1)
// CALCULATIONS //
up_lev =l - (st_mult * atr(st_period))
dn_lev = hz + (st_mult * atr(st_period))
up_trend = 0.0
up_trend := c[1] > up_trend[1] ? max(up_lev, up_trend[1]) : up_lev
down_trend = 0.0
down_trend := c[1] < down_trend[1] ? min(dn_lev, down_trend[1]) : dn_lev
// Calculate trend var
trend = 0
trend := c > down_trend[1] ? 1: c < up_trend[1] ? -1 : nz(trend[1], 1)
// Calculate SuperTrend Line
st_line = trend ==1 ? up_trend : down_trend
// Plotting
//plot(st_line[1], color = trend == 1 ? color.green : color.red , style = plot.style_cross, linewidth = 2, title = "SuperTrend")
buy=crossover( c, st_line)
sell=crossunder(c, st_line)
signal=input(true)
/////////////// Plotting ///////////////
plotshape(signal and buy, style=shape.triangleup, size=size.normal, location=location.belowbar, color=color.lime)
plotshape(signal and sell, style=shape.triangledown, size=size.normal, location=location.abovebar, color=color.red)
// -------------- Strategy Logic --------------------- //
usesuperz = input(true, "Use superzbot Alert Triggers", type=input.bool)
superzStublong = input("superz", "Account Stub Long", type=input.string)
superzStubshort = input("superzshort", "Account Stub Short", type=input.string)
superzSize = input(25, "Position Size", type=input.integer)
superzMaxSize = input(100, "Position MaxSize", type=input.integer)
superzSizeType = input("$", "Size Type", options=["$", "x", "%"])
plow=low
phigh=high
longCommand = usesuperz ? superzStublong+":long size=+"+tostring(superzSize)+(superzSizeType == "$" ? "" : superzSizeType)+" price="+tostring(close)+" symbol="+syminfo.ticker+" maxsize="+tostring(superzMaxSize)+" comment=Long": "Long"
shortCommand = usesuperz ? superzStubshort+":short size=+"+tostring(superzSize)+(superzSizeType == "$" ? "" : superzSizeType)+" price="+tostring(close)+" symbol="+syminfo.ticker+" maxsize="+tostring(superzMaxSize)+" comment=Short" : "Short"
closelongCommand = usesuperz ? superzStublong+":close"+" symbol="+syminfo.ticker+" comment=CloseLong": "Long"
closeshortCommand = usesuperz ? superzStubshort+":close"+" symbol="+syminfo.ticker+" comment=CloseShort" : "Short"
//if (buy)
strategy.close(id="Short", when=buy, comment = closeshortCommand, alert_message = closeshortCommand)
strategy.exit("strategy exit short", "Short")
strategy.entry(id="Long", long=true, when=buy and window(), comment = longCommand, alert_message = longCommand, limit=close, qty=leverage*strategy.equity/close)
//if (sell)
strategy.close(id="Long", when=sell, comment = closelongCommand, alert_message = closelongCommand)
strategy.exit("strategy exit Long", "Long")
strategy.entry(id="Short", long=false, when=sell and window(), comment = shortCommand, alert_message = shortCommand, limit=close, qty=leverage*strategy.equity/close)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment