0% found this document useful (0 votes)
41 views8 pages

Z Strategy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Z Strategy

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

strategy("Quantum Hero", overlay=true, process_orders_on_close =

true, calc_on_every_tick = true, pyramiding = 2)


import TradingView/Strategy/3

//EMA{

string ema_grp = "EMA"


show_ma = input.bool(true, title="Show", group=ema_grp)
ema1_value = input.int(10, title="Length 1", group=ema_grp)
ema2_value = input.int(20, title="Length 2", group=ema_grp)
ema3_value = input.int(50, title="Length 3", group=ema_grp)
ema1 = ta.ema(close, ema1_value)
ema2 = ta.ema(close, ema2_value)
ema3 = ta.ema(close, ema3_value)
color_ema1 = ema1 > ema3 ? color.new(#0CFF00, 50) :
color.new(#FF0000, 50)
color_ema2 = ema1 > ema3 ? color.new(#0CFF00, 25) :
color.new(#FF0000, 25)
color_ema3 = ema1 > ema3 ? color.new(#0CFF00, 0) :
color.new(#FF0000, 0)
color_ema4 = ema1 > ema3 ? color.new(#0CFF00, 85) :
color.new(#FF0000, 85)
color_ema5 = ema1 > ema3 ? color.new(#0CFF00, 70) :
color.new(#FF0000, 70)
pema1 = plot(show_ma ? ema1 : na, title='MA-1',
color=color_ema1, linewidth=1)
pema2 = plot(show_ma ? ema2 : na, title='MA-2',
color=color_ema2, linewidth=1)
pema3 = plot(show_ma ? ema3 : na, title='MA-3',
color=color_ema3, linewidth=2)
// fill(pema1, pema2, color_ema4)
// fill(pema2, pema3, color_ema5)

///ema}

//Settings
//-----------------------------------------------------------------------------{
length2 = input.int(20, 'Minimum Range Length', minval = 2)
mult2 = input.float(1., 'Range Width', minval = 0, step = 0.1)
atrLen2 = input.int(500, 'ATR Length', minval = 1)
//Style
upCss2 = input(#089981, 'Broken Upward', group = 'Style')
dnCss2= input(#f23645, 'Broken Downward', group = 'Style')
unbrokenCss2 = input(#2157f3, 'Unbroken', group = 'Style')

//-----------------------------------------------------------------------------}
//Detect and highlight ranges
//-----------------------------------------------------------------------------{
//Ranges drawings
var box bx = na
var line lvl = na
//Extensions
var float max = na
var float min = na
var os1 = 0
color detect_css = na
n1 = bar_index
atr = ta.atr(atrLen2) * mult2
ma = ta.sma(close, length2)
count = 0
for i = 0 to length2-1
count += math.abs(close[i] - ma) > atr ? 1 : 0

if count == 0 and count[1] != count


//Test for overlap and change coordinates
if n1[length2] <= bx.get_right()
max := math.max(ma + atr, bx.get_top())
min := math.min(ma - atr, bx.get_bottom())
//Box new coordinates
bx.set_top(max)
bx.set_rightbottom(n1, min)
bx.set_bgcolor(color.new(unbrokenCss2, 80))
//Line new coordinates
avg = math.avg(max, min)
lvl.set_y1(avg)
lvl.set_xy2(n1, avg)
lvl.set_color(unbrokenCss2)
else
max := ma + atr
min := ma - atr
//Set new box and level
bx := box.new(n1[length2], ma + atr, n1, ma - atr, na,
bgcolor = color.new(unbrokenCss2, 80))
lvl := line.new(n1[length2], ma, n1, ma, color =
unbrokenCss2, style = line.style_dotted)
detect_css := color.new(color.gray, 80)
os1 := 0
else if count == 0
bx.set_right(n1)
lvl.set_x2(n1)
//Set color
if close > bx.get_top()
bx.set_bgcolor(color.new(upCss2, 80))
lvl.set_color(upCss2)
os1 := 1
else if close < bx.get_bottom()
bx.set_bgcolor(color.new(dnCss2, 80))
lvl.set_color(dnCss2)
os1 := -1
rrg_cond = bar_index <= bx.get_right() and bar_index >=
bx.get_left()

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
//Range detection bgcolor
bgcolor(detect_css)
plot(max, 'Range Top', max != max[1] ? na : os1 == 0 ?
unbrokenCss2 : os1 == 1 ? upCss2 : dnCss2)
plot(min, 'Range Bottom', min != min[1] ? na : os1 == 0 ?
unbrokenCss2 : os1 == 1 ? upCss2 : dnCss2)

string momentum_grp = "Momentum"


lookback = input.float(5, title="Sequence Lookback", group =
momentum_grp, minval = 1) - 1
result = input.float(3, title="Number of candles of the same color in
the sequence", group = momentum_grp)
float counter_lookback_long = 0
for counter_long = 0 to lookback
if close[counter_long] > open[counter_long]
counter_lookback_long += 1
float counter_lookback_short = 0
for counter_short = 0 to lookback
if close[counter_short] < open[counter_short]
counter_lookback_short += 1
momentum_long = counter_lookback_long >= result
momentum_short = counter_lookback_short >= result
plot(counter_lookback_long)
plot(result)

plot(counter_lookback_short)

//Consolidation
prd_cons = input.int(defval= 10, title='Loopback Period', minval=2,
maxval=50, group="Consolidation")
conslen = input.int(defval=7, title='Min Consolidation Length',
minval=2, maxval=20, group="Consolidation")
float hb_ = ta.highestbars(prd_cons) == 0 ? high : na
float lb_ = ta.lowestbars(prd_cons) == 0 ? low : na
var int dir = 0
float zz = na
float pp = na
iff_11 = lb_ and na(hb_) ? -1 : dir
dir := hb_ and na(lb_) ? 1 : iff_11
if hb_ and lb_
if dir == 1
zz := hb_
zz
else
zz := lb_
zz
else
iff_11 = lb_ ? lb_ : na
zz := hb_ ? hb_ : iff_11
zz
for x = 0 to 1000 by 1
if na(close) or dir != dir[x]
break
if zz[x]
if na(pp)
pp := zz[x]
pp
else
if dir[x] == 1 and zz[x] > pp
pp := zz[x]
pp
if dir[x] == -1 and zz[x] < pp
pp := zz[x]
pp
var int conscnt = 0
var float condhigh = na
var float condlow = na
float H_ = ta.highest(conslen)
float L_ = ta.lowest(conslen)
var line upline = na
var line dnline = na
bool breakoutup = false
bool breakoutdown = false

if ta.change(pp)
if conscnt > conslen
if pp > condhigh
breakoutup := true
breakoutup
if pp < condlow
breakoutdown := true
breakoutdown
if conscnt > 0 and pp <= condhigh and pp >= condlow
conscnt += 1
conscnt
else
conscnt := 0
conscnt
else
conscnt += 1
conscnt
if conscnt >= conslen
if conscnt == conslen
condhigh := H_
condlow := L_
condlow
else
condhigh := math.max(condhigh, high)
condlow := math.min(condlow, low)
condlow

////}
//Entry Condition

longcondition = ((ta.crossover(ema1, ema3) and not (conscnt >


conslen)) or (ema1 > ema3 and ta.crossover(close, condhigh))) and
momentum_long
shortcondition = ((ta.crossunder(ema1, ema3) and not (conscnt >
conslen)) or (ema1 < ema3 and ta.crossunder(close, condlow))) and
momentum_short

/////REDO EXITS ENTRIES AND TP/SL from here ////

////////THIS says that after there was a trade next one must be in
other directiom
float position_size = 0
position_size := position_size[1]
if longcondition
position_size := 1
if shortcondition
position_size := -1
//barcolor(ema1 > ema3 ? #0CFF00 : #FF0000)

plotshape(longcondition and position_size[1] == -1, text="BUY",


title="BUY", location = location.belowbar, style = shape.labelup,
color=#0CFF00, textcolor = color.white)
plotshape(shortcondition and position_size[1] == 1, text="SELL",
title="SELL", location = location.abovebar, style = shape.labeldown,
color=#FF0000, textcolor = color.white)

////only TP1/sls2=not used


// strategy set up
stratg= 'strategy settings'
pipstp= input.int(3, 'Take profits pips', group= stratg)

var float tpl1 = close + (pipstp* GetPipSize())


sll1= ta.lowest(10)
var float tpl2 = close + (30* GetPipSize() )
sll2 = ema3
var float nsll = close
var int ilrange= na

var float tps1 = close - (pipstp* GetPipSize())


sls1 = ta.highest(10)
var float tps2 = close - (30* GetPipSize() )
sls2 = ema3
var float nsls = close
var int isrange= na

if longcondition and position_size[1] == -1 and not rrg_cond and


strategy.position_size == 0 //and ci < chop
tpl1 := close + (pipstp* GetPipSize())
tpl2 := close + (10* GetPipSize())
nsll := close + (close*0.0001)
ilrange := bar_index
longCond_txt = pc_id + "," + 'buy' + "," + symbol + ",risk=" +
str.tostring(pc_risk) + "," + "sl=" + str.tostring(sll1, '#.##') +
",tp=" + str.tostring(tpl1,'#.####') + ",comment='lo1'"
longcond2_txt= pc_id + "," + 'buy' + "," + symbol + ",risk=" +
str.tostring(pc_risk) + "," + "sl=" + str.tostring(sll1,'#.##') +
",tp=" + str.tostring(nsll + (32 * GetPipSize()),'#.####') +
",comment='lo2'"
strategy.entry('long1', strategy.long, alert_message =
longCond_txt )
strategy.exit('exit long 1', from_entry='long1', limit = tpl1,
stop= sll1,alert_message = closelonga1)
strategy.entry('long2', strategy.long, alert_message =
longcond2_txt )
strategy.exit('exit long2', from_entry= 'long2', stop =sll1, limit =
nsll + (32 * GetPipSize()), alert_message= closelonga2)

if ta.crossover(high, tpl1) and strategy.position_size > 0


strategy.exit('exit long2', from_entry = 'long2', stop = tpl1, limit
= nsll + (32 * GetPipSize()), alert_message= closelonga2)
if shortcondition and position_size[1] == 1 and not rrg_cond and
strategy.position_size == 0 //and ci < chop
tps1 := close - (pipstp* GetPipSize())
tps2 := close - (10* GetPipSize() )
nsls := close - (close*0.0001)
isrange := bar_index
shortCond_txt = pc_id + "," + 'sell' + "," + symbol + ",risk=" +
str.tostring(pc_risk) + "," + "sl=" + str.tostring(sls1, '#.##') +
",tp=" + str.tostring(tps1, '#.####') + ",comment='sh1'"
shortcond2_txt = pc_id + "," + 'sell' + "," + symbol + ",risk=" +
str.tostring(pc_risk) + "," + "sl=" + str.tostring(sls1, '#.##') +
",tp=" + str.tostring(nsls - (32* GetPipSize()),'#.####')+
",comment='sh2'"
strategy.entry('short1', strategy.short, alert_message =
shortCond_txt )
strategy.exit('exit short 1', from_entry='short1', limit = tps1,
stop= sls1, alert_message= closeshorta1)
strategy.entry('short2', strategy.short, alert_message=
shortcond2_txt)
strategy.exit('exit short2', from_entry= 'short2', stop = sls1,
limit = nsls - (32* GetPipSize()), alert_message= closeshorta2)

if ta.crossunder(low, tps1) and strategy.position_size < 0


strategy.exit('exit short2', from_entry = 'short2', stop = tps1,
limit = nsls - (32* GetPipSize()), alert_message= closeshorta2)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy