0% found this document useful (0 votes)
477 views3 pages

Multi TP Strategy

This document contains the code for a trading strategy that uses standard deviation to determine trend and entries. It takes adjustable percentage take profits at 5 levels and a static stop loss. The strategy plots trend lines and takes both long and short trades with multiple take profit levels.

Uploaded by

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

Multi TP Strategy

This document contains the code for a trading strategy that uses standard deviation to determine trend and entries. It takes adjustable percentage take profits at 5 levels and a static stop loss. The strategy plots trend lines and takes both long and short trades with multiple take profit levels.

Uploaded by

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

// Strategy using standard deviation for trend to detemine entries with

// adjustable % Take Profits x 5, adjustable % static Stop loss x 1, using 14


plots.
//@version=5
strategy("Multi TP Strategy", overlay=true)

// Bactest ('myWindow'- used in strategy calls)


daysBack = input.int(7, title="Plot days back", inline='1')
millisecs_IN_DAY = 24 * 60 * 60 * 1000 //hour*mins*sec*millisec
lastBarDate = timestamp(year(timenow), month(timenow), dayofmonth(timenow),
hour(timenow), minute(timenow), second(timenow))
thisBarDate = timestamp(year, month, dayofmonth, hour, minute, second), daysLeft =
math.floor((lastBarDate - thisBarDate) / millisecs_IN_DAY)
myWindow = daysLeft <= daysBack-1

// No repaint
res = timeframe.ismonthly ? str.tostring(timeframe.multiplier, '###M')
: timeframe.isweekly ? str.tostring(timeframe.multiplier, '###W')
: timeframe.isdaily ? str.tostring(timeframe.multiplier, '###D')
: timeframe.isintraday ? str.tostring(timeframe.multiplier, '####') : '60'
closey = request.security(syminfo.tickerid, res, close[barstate.isrealtime ? 1:0])
openy = request.security(syminfo.tickerid, res, open[barstate.isrealtime ? 1:0])

// Inputs
tradeType = input.string('BOTH', title='Trades ', options=['LONGS', 'SHORTS',
'BOTH', 'NONE'], inline='1')
TD = input.int(70, title='Trade infrequency', inline='1')
txt1 = "If no trend smoother ticked then default trend step is used with the Trade
infrequency number for the steps"
SmA = input.bool(false, title='Use Trend smoother with', inline='2',
tooltip=txt1)
smaAmount = input.int(100, title=' amount', step=5, inline='2')
tpPer = input.float(1.5, title='TP %', step=0.1, inline='3')
slPer = input.float(1, title='SL %', minval=0, step=0.1, inline='3')

// Decs
var float entryprice = 0.0
var float tradeStopPrice = 0.0
var float tp1 = 0.0, var float tp2 = 0.0, var float tp3 = 0.0, var float tp4 = 0.0,
var float tp5 = 0.0
float SL = close/100 * slPer
SLtick = math.round_to_mintick(slPer*close)
shortstopprice = math.round_to_mintick(low+SL)
longstopprice = math.round_to_mintick(high-SL)

// Trends / trades entries


my_xtrend(src) =>
var _f = .0, dev = ta.stdev(close,2)*TD
_f := dev < 2 ? closey : closey > nz(_f[1],closey) + dev ?
closey : closey < nz(_f[1],closey) - dev ? closey : _f[1]
var _x = .0, var xcols = color.white, dev = ta.stdev(hlcc4,2)*TD
_x := dev < 1 ? closey : closey > nz(_x[1],closey) + dev ?
closey : closey < nz(_x[1],closey) - dev ? closey : _x[1]
xcols := fixnan(_x > _x[1] ? #33cc33 : _x < _x[1] ?
#cc0000 : na), Dn_x = my_xtrend(openy),Up_x = my_xtrend(closey)

// Colors
xbear = xcols == #cc0000 and (nz(Dn_x,xcols == #cc0000))
xbull = xcols == #33cc33 and (nz(Up_x,xcols == #33cc33))
barColor = xbull ? #33cc33 : xbear ? #cc0000 : na //green and red
barcolor(barColor, title='Bar Colours')

// Trade Entry conditions


longCondition = not SmA ? ta.crossover(openy, Dn_x) : ta.crossover(openy,
ta.sma(Dn_x,smaAmount))
shortCondition = not SmA ? ta.crossover(closey, Up_x) : ta.crossunder(closey,
ta.sma(Up_x,smaAmount))

//Main collated conditions


validLong = strategy.position_size == 0 and barstate.isconfirmed and longCondition
and xbull //ensure position flat, bar closed, and long cond and in local bull
trend
validShort = strategy.position_size == 0 and barstate.isconfirmed and
shortCondition and xbear //ensure position flat, bar closed, and short cond and in
local bear trend

// long entry and stop price capture at point of entry (keeps the plot lines
straight and allows constant saved variable for strat exits below)
if validLong
entryprice := ta.valuewhen(longCondition,close,0)
tradeStopPrice := int(longstopprice)
tp1 := entryprice*(tpPer/100)/5*1/syminfo.mintick
tp2 := entryprice*(tpPer/100)/5*2/syminfo.mintick
tp3 := entryprice*(tpPer/100)/5*3/syminfo.mintick
tp4 := entryprice*(tpPer/100)/5*4/syminfo.mintick
tp5 := entryprice*(tpPer/100)/5*5/syminfo.mintick

// short entry and stop price capture at point of entry (keeps the plot lines
straight and allows constant saved variable for strat exits below)
if validShort
entryprice := ta.valuewhen(shortCondition,close,0)
tradeStopPrice := int(shortstopprice)
tp1 := entryprice*(tpPer/100)/5*1/syminfo.mintick
tp2 := entryprice*(tpPer/100)/5*2/syminfo.mintick
tp3 := entryprice*(tpPer/100)/5*3/syminfo.mintick
tp4 := entryprice*(tpPer/100)/5*4/syminfo.mintick
tp5 := entryprice*(tpPer/100)/5*5/syminfo.mintick

//.......... sl pink circles


hs=plot(strategy.position_size > 0 ? tradeStopPrice : na, color=color.new(#fd0e70,
0), style=plot.style_circles, linewidth=1) //debug trade length in bars
ls=plot(strategy.position_size < 0 ? tradeStopPrice : na, color=color.new(#fd0e70,
0), style=plot.style_circles, linewidth=1) //debug trade length in bars

// main trend line plots


sp=plot(not SmA ? Dn_x : ta.sma(Dn_x,smaAmount), title='Short', color=xcols,
style=plot.style_linebr, linewidth=3, editable=true)
lp=plot(not SmA ? Up_x : ta.sma(Up_x,smaAmount), title='Long', color=xcols,
style=plot.style_linebr, linewidth=3, editable=true)

// conditional trade tp's


Lx=strategy.position_size != 0 and strategy.position_size > 0,
Sx=strategy.position_size != 0 and strategy.position_size < 0
ltp1 = plot(Lx ? (entryprice+entryprice/100*tpPer/5*1) : na, title='tp1',
color=#00ff00, style=plot.style_linebr, linewidth=1)//green
ltp2 = plot(Lx ? (entryprice+entryprice/100*tpPer/5*2) : na, title='tp2',
color=#00ff00, style=plot.style_linebr, linewidth=1)//green
ltp3 = plot(Lx ? (entryprice+entryprice/100*tpPer/5*3) : na, title='tp3',
color=#00ff00, style=plot.style_linebr, linewidth=1)//green
ltp4 = plot(Lx ? (entryprice+entryprice/100*tpPer/5*4) : na, title='tp4',
color=#00ff00, style=plot.style_linebr, linewidth=1)//green
ltp5 = plot(Lx ? (entryprice+entryprice/100*tpPer/5*5) : na, title='tp5',
color=#00ff00, style=plot.style_linebr, linewidth=1)//green
stp1 = plot(Sx ? (entryprice-entryprice/100*tpPer/5*1) : na, title='tp1',
color=#ff0000, style=plot.style_linebr, linewidth=1)//red
stp2 = plot(Sx ? (entryprice-entryprice/100*tpPer/5*2) : na, title='tp2',
color=#ff0000, style=plot.style_linebr, linewidth=1)//red
stp3 = plot(Sx ? (entryprice-entryprice/100*tpPer/5*3) : na, title='tp3',
color=#ff0000, style=plot.style_linebr, linewidth=1)//red
stp4 = plot(Sx ? (entryprice-entryprice/100*tpPer/5*4) : na, title='tp4',
color=#ff0000, style=plot.style_linebr, linewidth=1)//red
stp5 = plot(Sx ? (entryprice-entryprice/100*tpPer/5*5) : na, title='tp5',
color=#ff0000, style=plot.style_linebr, linewidth=1)//red

if myWindow
if (tradeType == 'LONGS' or tradeType == 'BOTH')
strategy.entry(id='L', direction=strategy.long, qty=5, when=validLong)
strategy.exit(id='L_tp1', from_entry='L', qty=1, profit=tp1, loss=SLtick,
when=strategy.position_size > 0)
strategy.exit(id='L_tp2', from_entry='L', qty=1, profit=tp2, loss=SLtick,
when=strategy.position_size > 0)
strategy.exit(id='L_tp3', from_entry='L', qty=1, profit=tp3, loss=SLtick,
when=strategy.position_size > 0)
strategy.exit(id='L_tp4', from_entry='L', qty=1, profit=tp4, loss=SLtick,
when=strategy.position_size > 0)
strategy.exit(id='L_tp5', from_entry='L', qty=1, profit=tp5, loss=SLtick,
when=strategy.position_size > 0)
strategy.close(id='L', qty_percent=100, when=ta.crossunder(close,
tradeStopPrice))// close if stop hit

if (tradeType == 'SHORTS' or tradeType == 'BOTH')


strategy.entry(id='S', direction=strategy.short, qty=5, when=validShort)
strategy.exit(id='S_tp1', from_entry='S', qty=1, profit=tp1, loss=SLtick,
when=strategy.position_size < 0)
strategy.exit(id='S_tp2', from_entry='S', qty=1, profit=tp2, loss=SLtick,
when=strategy.position_size < 0)
strategy.exit(id='S_tp3', from_entry='S', qty=1, profit=tp3, loss=SLtick,
when=strategy.position_size < 0)
strategy.exit(id='S_tp4', from_entry='S', qty=1, profit=tp4, loss=SLtick,
when=strategy.position_size < 0)
strategy.exit(id='S_tp5', from_entry='S', qty=1, profit=tp5, loss=SLtick,
when=strategy.position_size < 0)
strategy.close(id='S', qty_percent=100, when=ta.crossover(close,
tradeStopPrice))// close if stop hit
//end

You might also like