Try changing TradePrice in the TS snippets to PositionPrice (is that the correct term for the average trade price of all open positions)?
Just an idea before bed! 🙂
PaulParticipant
Master
if you limit the number of open positions to i.e 5, an option would be maybe to have separeated individual trailing stops for tradeprice(1),tradeprice(2) etc
ive made in the past a ts for 2 cul. positions in some topic
not a nice way because you get lots of code
Grahal’s suggestion of using positionprice instead of tradeprice seems to make a big difference. TS should now start from the average price.
Revised with correction to the TS, tames the excesses – lower profit but better win %, optimized for max pos = 6
further revision to the TS (grazie Roberto) and re-optimized. Interestingly, there is now a sweet spot for max positions – it starts getting good at 4 and peaks at 8 (attached illustration). Above that performance falls off.
near identical performance on the Dow (equivalent position size =.4), with the advantage of smaller min position.
PaulParticipant
Master
Hi Nonetheless,
I took the excellent cumulative trailingstop snippet of robertogozzi and splitted it for long & short, same as vectorial. This gives another nice improvement overall.
// %trailing stop function incl. cumulative positions
once trailingstoptype = 1
if trailingstoptype then
//====================
once trailingpercentlong = 0.49 // %
once trailingpercentshort = 0.28 // %
once accelerator = 1 // 1 = default; always > 0 (i.e. 0.5-3)
once ts2sensitivity = 0 // [0]close;[1]high/low;[2]low;high
//====================
once steppercentlong = (trailingpercentlong/10)*accelerator
once steppercentshort = (trailingpercentshort/10)*accelerator
if onmarket then
trailingstartlong = positionprice[1]*(trailingpercentlong/100)
trailingstartshort = positionprice[1]*(trailingpercentshort/100)
trailingsteplong = positionprice[1]*(steppercentlong/100)
trailingstepshort = positionprice[1]*(steppercentshort/100)
endif
if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then
newsl = 0
mypositionprice = 0
endif
positioncount = abs(countofposition)
if newsl > 0 then
if positioncount > positioncount[1] then
if longonmarket then
newsl = max(newsl,positionprice * newsl / mypositionprice)
else
newsl = min(newsl,positionprice * newsl / mypositionprice)
endif
endif
endif
if ts2sensitivity=1 then
ts2sensitivitylong=high
ts2sensitivityshort=low
elsif ts2sensitivity=2 then
ts2sensitivitylong=low
ts2sensitivityshort=high
else
ts2sensitivitylong=close
ts2sensitivityshort=close
endif
if longonmarket then
if newsl=0 and ts2sensitivitylong-positionprice>=trailingstartlong*pipsize then
newsl = positionprice+trailingsteplong*pipsize
endif
if newsl>0 and ts2sensitivitylong-newsl>=trailingsteplong*pipsize then
newsl = newsl+trailingsteplong*pipsize
endif
endif
if shortonmarket then
if newsl=0 and positionprice-ts2sensitivityshort>=trailingstartshort*pipsize then
newsl = positionprice-trailingstepshort*pipsize
endif
if newsl>0 and newsl-ts2sensitivityshort>=trailingstepshort*pipsize then
newsl = newsl-trailingstepshort*pipsize
endif
endif
if barindex-tradeindex>1 then
if longonmarket then
if newsl>0 then
sell at newsl stop
endif
if newsl>0 then
if low crosses under newsl then
sell at market
endif
endif
endif
if shortonmarket then
if newsl>0 then
exitshort at newsl stop
endif
if newsl>0 then
if high crosses over newsl then
exitshort at market
endif
endif
endif
endif
mypositionprice = positionprice
endif
I thought that 8 positions was a bit too big.
I tried to optimized this parameters and the better one was 4 (and for me obviously it decrease the DD)
Results on 200k attached
Nice one Paul! I have also been working on long and short versions – your solution is more elegant, I just made 2 different algos.
But I’m also trying an alteration to the entry code so that the initial position is based on the 6min turnaround, with additional positions based on the 2min. Logically I think this makes more sense – to catch the bottom/top of the 6min run rather than entering anywhere along that trend. I’m running all these versions in demo to see what works better in actual trading.
Ich I have a little bit better results with Paul´s Version with small changes.
Ctime = time >=143000 and time <213000
BUY positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.1
SET TARGET %PROFIT 2.0
ENDIF
// Conditions to enter short positions
IF Ctime and c2 and c2a AND C4 and c4a AND C6 and c6a and abs(CountOfPosition) < MaxPositionsAllowed THEN
SELLSHORT positionsize CONTRACT AT MARKET
SET STOP %LOSS 1.1
SET TARGET %PROFIT 2.0
@VinzentVega
Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read and try not to mix/use different languages.
Thanks 🙂
Hi,
All of codes above don’t work for me. The message is : “do you want to restart the backtest in tick by tick mode or without”
Anyone has an idea ?
Thanks in advance.
Hello
I have trust some modification SL and TP
the code :
// Definition of code parameters
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
DEFPARAM preloadbars = 5000
MaxPositionsAllowed = 100
//Money Management NAS
MM = 0 // = 0 for optimization
if MM = 0 then
positionsize=1
ENDIF
if MM = 1 then
ONCE startpositionsize = 1
ONCE factor = 60 // factor of 10 means margin will increase/decrease @ 10% of strategy profit; factor 20 = 5% etc
ONCE margin = (close*.005) // tier 1 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE margin2 = (close*.01)// tier 2 margin value of 1 contract in instrument currency; change decimal according to available leverage
ONCE tier1 = 200 // IG first tier margin limit
ONCE maxpositionsize = 2000 // IG tier 2 margin limit
ONCE minpositionsize = 1 // enter minimum position allowed
IF StrategyProfit <> StrategyProfit[1] THEN
positionsize = startpositionsize + Strategyprofit/(factor*margin)
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
IF startpositionsize + Strategyprofit/(factor*margin) > tier1 then
positionsize = (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 //incorporating tier 2 margin
ENDIF
IF StrategyProfit <> StrategyProfit[1] THEN
if startpositionsize + Strategyprofit/(factor*margin) < minpositionsize THEN
positionsize = minpositionsize //keeps positionsize from going below allowed minimum
ENDIF
IF (((startpositionsize + (Strategyprofit/(factor*margin))-tier1)*(factor*margin))/(factor*margin2)) + tier1 > maxpositionsize then
positionsize = maxpositionsize// keeps positionsize from going above IG tier 2 margin limit
ENDIF
ENDIF
ENDIF
ENDIF
Ctime = time >=143000 and time <210000
TIMEFRAME(18 minutes)
Period= 155
inner = 2*weightedaverage[round( Period/2)](typicalprice)-weightedaverage[Period](typicalprice)
HULLa = weightedaverage[round(sqrt(Period))](inner)
c1 = HULLa > HULLa[1]
c2 = HULLa < HULLa[1]
ST1 = SAR[.005,.005,.02]
c1a = (close > ST1)
c2a = (close < ST1)
TIMEFRAME(6 minutes)
Periodb= 29
innerb = 2*weightedaverage[round( Periodb/2)](typicalprice)-weightedaverage[Periodb](typicalprice)
HULLb = weightedaverage[round(sqrt(Periodb))](innerb)
c3 = HULLb > HULLb[1]
c4 = HULLb < HULLb[1]
ST2 = SAR[.005,.005,.02]
c3a = (close > ST2)
c4a = (close < ST2)
TIMEFRAME(default)
Periodc= 4
innerc = 2*weightedaverage[round( Periodc/2)](typicalprice)-weightedaverage[Periodc](typicalprice)
HULLc = weightedaverage[round(sqrt(Periodc))](innerc)
c5 = HULLc > HULLc[1] and HULLc[1] < HULLc[2]
c6 = HULLc < HULLc[1] and HULLc[1] > HULLc[2]
ST3 = SAR[.01,.01,.005]
c5a = (close > ST3)
c6a = (close < ST3)
// Conditions to enter long positions
IF not longonmarket and Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a THEN
BUY positionsize CONTRACT AT MARKET
SET STOP pLOSS 15
SET TARGET pPROFIT 55
elsif longonmarket and Ctime and c1 and c1a AND C3 and c3a AND C5 and c5a and abs(CountOfPosition) < MaxPositionsAllowed THEN
if positionperf(0)*100>0 then
BUY positionsize CONTRACT AT MARKET
SET STOP pLOSS 282
SET TARGET pPROFIT 322
endif
endif
// Conditions to enter short positions
IF not shortonmarket and Ctime and c2 and c2a AND C4 and c4a AND C6 and c6a THEN
SELLSHORT positionsize CONTRACT AT MARKET
SET STOP pLOSS 68
SET TARGET pPROFIT 21
elsif shortonmarket and Ctime and c2 and c2a AND C4 and c4a AND C6 and c6a and abs(CountOfPosition) < MaxPositionsAllowed THEN
if positionperf(0)*100>0 then
SELLSHORT positionsize CONTRACT AT MARKET
SET STOP pLOSS 159
SET TARGET pPROFIT 411
endif
endif
//%trailing stop function
trailingstop = 1
if trailingstop =1 then
once trailingPercent = 0.38
once stepPercent = 0.006
if onmarket then
trailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profit
trailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart THEN
newSL = tradeprice(1)+trailingstep
ENDIF
//next moves
IF newSL>0 AND close-newSL>trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart THEN
newSL = tradeprice(1)-trailingstep
ENDIF
//next moves
IF newSL>0 AND newSL-close>trailingstep THEN
newSL = newSL-trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
endif