Hi Lombard974
Doesn’t exactly work the way I wanted, it takes one position based on high/low from 15min bar after crossing, on the next bar, with only 1 trade a day between 9.00-9.15
Lines display the high/low from the 15min candle 845-0900. You also see your stoploss and the trailing-stops.
Your second option isn’t implemented and is perhaps not suitable for this code.
The code is a bit long, it the layout I work with. Focus on the main criteria part for adjustment and time settings.
Tested on DAX30 1 minute.
// common rules
defparam cumulateorders = false
defparam preloadbars = 10000
TIMEFRAME (default)
// on/off
once enablesl = 1 // stoploss
once enablept = 1 // profit target
once enablets = 1 // trailing stop
once enablebe = 0 // breakeven stop
//activate graph(onprice); maximum 5 lines active !
once displaysl = 1 // stop loss [1 line]
once displaypt = 0 // profit target [1 line]
once displayts = 1 // trailing stop [3 lines]
once displaybe = 0 // breakeven stop [1 line]
once displaydim = 0 // days in market [1 line]
once displaypp = 0 // position performance [1 line]
positionsize = 1
sl = 1 // % stoploss
pt = 1 // % profit target
// trailingstop exit activates when positionperformance (pp)
ts1=0.35 // is greater then this percentage then use trailingstop ts1
switch=0.45 // is greater then this percentage then use trailingstop ts2
ts2=0.25
switch2=0.60 // is greater then this percentage then use trailingstop ts3
ts3=0.20
// breakeven exit
besg = 0.30 // % break even stop gain
besl = 0.05 // % break even stop level
// used for sl/pt/ (not used for ts); not to be optimized!
// forex use 0.01; securities use 1, index use 100
// profittargets and stoploss have to match the lines with displaysl/tp/ts set to 1
underlaying=100
// day & time
once entertime = 090000
once lasttime = 091500
once closetime = 210000 // greater then 23.59 means it continues position overnight
once closetimefr = 180000
tradetime = (time >= entertime and time < lasttime)
// reset at start
if intradaybarindex=0 then
longtradecounter=0
shorttradecounter=0
tradecounter=0
endif
// [pc] position criteria (also set a limit to the number of trades a day) (or set high for no impact)
pclong = countoflongshares < 1 and longtradecounter < 1 and tradecounter < 1
pcshort = countofshortshares < 1 and shorttradecounter < 1 and tradecounter < 1
//[mc] main criteria
TIMEFRAME (15 minutes, updateonclose)
if intradaybarindex=35 then //0845-0900 on 15 min
MaxPrice = high
MinPrice = low
else
MaxPrice = 0
MinPrice = 0
ENDIF
graphonprice MaxPrice coloured(0,200,0) as "MaxPrice"
graphonprice MinPrice coloured(200,200,0) as "MinPrice"
TIMEFRAME (default)
if intradaybarindex=0 then
mclong=0
mcshort=0
endif
if close crosses over MaxPrice then
mclong=1
endif
if close crosses under MinPrice then
mcshort=1
endif
// long & short entry
if tradetime then
if pclong and mclong then
buy positionsize contract at MaxPrice stop
longtradecounter=longtradecounter + 1
tradecounter=tradecounter+1
elsif pcshort and mcshort then
sellshort positionsize contract at MinPrice stop
shorttradecounter=shorttradecounter + 1
tradecounter=tradecounter+1
endif
endif
//
pp=(positionperf*100)
// trailing stop
if enablets then
trailingstop1 = (tradeprice(1)/100)*ts1
trailingstop2 = (tradeprice(1)/100)*ts2
trailingstop3 = (tradeprice(1)/100)*ts3
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
maxprice1=0
minprice1=close
priceexit1=0
maxprice2=0
minprice2=close
priceexit2=0
maxprice3=0
minprice3=close
priceexit3=0
a1=0
a2=0
a3=0
pp=0
endif
if onmarket then
if pp>=ts1 then
a1=1
endif
if pp>=switch then
a2=1
endif
if pp>=switch2 then
a3=1
endif
endif
// setup long
if longonmarket then
maxprice1=max(maxprice1,close)
maxprice2=max(maxprice2,high)
maxprice3=max(maxprice3,high)
if a1 then
if maxprice1-tradeprice(1)>=(trailingstop1)*pointsize then
priceexit1=maxprice1-(trailingstop1/(underlaying/100))*pointsize
endif
endif
if a2 then
if maxprice2-tradeprice(1)>=(trailingstop2)*pointsize then
priceexit2=maxprice2-(trailingstop2/(underlaying/100))*pointsize
endif
endif
if a3 then
if maxprice3-tradeprice(1)>=(trailingstop3)*pointsize then
priceexit3=maxprice3-(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
// setup short
if shortonmarket then
minprice1=min(minprice1,close)
minprice2=min(minprice2,low)
minprice3=min(minprice3,low)
if a1 then
if tradeprice(1)-minprice1>=(trailingstop1)*pointsize then
priceexit1=minprice1+(trailingstop1/(underlaying/100))*pointsize
endif
endif
if a2 then
if tradeprice(1)-minprice2>=(trailingstop2)*pointsize then
priceexit2=minprice2+(trailingstop2/(underlaying/100))*pointsize
endif
endif
if a3 then
if tradeprice(1)-minprice3>=(trailingstop3)*pointsize then
priceexit3=minprice3+(trailingstop3/(underlaying/100))*pointsize
endif
endif
endif
// exit long
if longonmarket and priceexit1>0 then
sell at priceexit1 stop
endif
if longonmarket and priceexit2>0 then
sell at priceexit2 stop
endif
if longonmarket and priceexit3>0 then
sell at priceexit3 stop
endif
// exit short
if shortonmarket and priceexit1>0 then
exitshort at priceexit1 stop
endif
if shortonmarket and priceexit2>0 then
exitshort at priceexit2 stop
endif
if shortonmarket and priceexit3>0 then
exitshort at priceexit3 stop
endif
if displayts then
graphonprice priceexit1 coloured(0,0,255,255) as "trailingstop1"
graphonprice priceexit2 coloured(0,0,255,255) as "trailingstop2"
graphonprice priceexit3 coloured(0,0,255,255) as "trailingstop3"
endif
endif
// break even stop
if enablebe then
if not onmarket then
newsl=0
endif
if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl=0
endif
if longonmarket then
if close-tradeprice(1)>=(((tradeprice(1)/100)*besg)/(underlaying/100))*pointsize then
newsl=tradeprice(1)+(((tradeprice(1)/100)*besl)/(underlaying/100))*pointsize
endif
endif
if shortonmarket then
if tradeprice(1)-close>=(((tradeprice(1)/100)*besg)/(underlaying/100))*pointsize then
newsl=tradeprice(1)-(((tradeprice(1)/100)*besl)/(underlaying/100))*pointsize
endif
endif
if longonmarket and newsl>0 then
sell at newsl stop
endif
if shortonmarket and newsl>0 then
exitshort at newsl stop
endif
if displaybe then
//graphonprice newsl coloured(244,102,27,255) as "breakevenstop"
endif
endif
// set stoploss
if enablesl then
if not onmarket then
sloss=0
elsif longonmarket then
sloss=tradeprice(1)-((tradeprice(1)*sl)/underlaying)*pointsize
elsif shortonmarket then
sloss=tradeprice(1)+((tradeprice(1)*sl)/underlaying)*pointsize
endif
set stop %loss sl
if displaysl then
graphonprice sloss coloured(255,0,0,255) as "stoploss"
sloss=sloss
endif
endif
// to display profittarget
if enablept then
if not onmarket then
ptarget=0
elsif longonmarket then
ptarget=tradeprice(1)+((tradeprice(1)*pt)/underlaying)*pointsize
elsif shortonmarket then
ptarget=tradeprice(1)-((tradeprice(1)*pt)/underlaying)*pointsize
endif
set target %profit pt
if displaypt then
//graphonprice ptarget coloured(121,141,35,255) as "profittarget"
ptarget=ptarget
endif
endif
// exit at closetime
if onmarket then
if time >= closetime or (currentdayofweek=5 and time>=closetimefr) then
sell at open stop
exitshort at open stop
endif
endif
// display days in market
if displaydim then
if not onmarket then
dim=0
endif
if (onmarket and not onmarket[1]) or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
dim=1
endif
if not (dayofweek=1 and hour = 1) then
if onmarket then
if openday <> openday[1] then
dim = dim + 1
endif
endif
endif
//graph dim coloured(121,141,35,255) as "days in market"
endif
if displaypp then
//graph pp coloured(0,0,0,255) as "positionperformance"
endif
//for trading all graph & graphonprice have to be comment out
//for backtesting and to have a visual idea of exits or pp
//uncomment and set displaysl and/or displaypt and/or displayts to 1
//maximum of 5 can be used to display lines (graph & graphonprice combined)