Hi guys, I tried to modify Nicolas’s code regarding simulated trading. I have included the functions written by Nicolas (https://www.prorealcode.com/blog/learning/how-to-improve-a-strategy-with-simulated-trades-1/) for the short as well, but I believe that I made some mistake. Can someone help me?
defparam cumulateorders=false
// --- strategy settings
tpratio = 2.7 //2.7
stoploss = 65 //65
takeprofit = stoploss*tpratio
// --- simulated trading settings
equityCurvePeriod = 20 //orders quantity for the equity curve average
activateSimulatedTrading = 1 //(0= false / 1 =true)
//strategies
buysignal = average[7] crosses over average[21]//rsi[2] crosses over 30
sellsignal = average[7] crosses under average[21]//rsi[2] crosses over 30
if realtrading then //real trading
if not longonmarket and buysignal then
buy 1 contracts at market
ordercount=ordercount+1 //counting the order to know we have sufficient ones to activate the simulation later
endif
set target pprofit takeprofit
set stop ploss stoploss
elsif not realtrading and ordercount>equityCurvePeriod then //fake trading
if not longontrading and buysignal then
openprice=close //fake order open price
longontrading=1 //we are now on market
//reset MFE & MAE values
mfe=0
mae=0
//fake orders count
fakeorders=fakeorders+1
endif
endif
if realtrading then //real trading
if not shortonmarket and sellsignal then
sellshort 1 contract at market
ordercount=ordercount+1 //counting the order to know we have sufficient ones to activate the simulation later
endif
set target pprofit takeprofit
set stop ploss stoploss
elsif not realtrading and ordercount>equityCurvePeriod then //fake trading
if not shortontrading and sellsignal then
openprice=close //fake order open price
shortontrading=1 //we are now on market
//reset MFE & MAE values
mfe=0
mae=0
//fake orders count
fakeorders=fakeorders+1
endif
endif
//check profit n loss of the fake order
if longontrading=1 then
mfe = max(high-openprice,mfe) //compute the MaxFavorableExcursion
mae = min(low-openprice,mae) //compute the MaxAdverseExcursion
//profit achieved
if mfe>=takeprofit*pointsize then //testing the takeprofit
orderPNL=((openprice+takeprofit*pointsize)/openprice)-1
longontrading=0 //not on market anymore
endif
//shit happens!
if mae<=-stoploss*pointsize then //testing the stoploss
orderPNL=-(((openprice-stoploss*pointsize)/openprice)-1)
longontrading=0 //not on market anymore
endif
endif
//check profit n loss of the fake order
if shortontrading=1 then
mfe = max(high-openprice,mfe) //compute the MaxFavorableExcursion
mae = min(low-openprice,mae) //compute the MaxAdverseExcursion
//profit achieved
if mfe>=takeprofit*pointsize then //testing the takeprofit
orderPNL=((openprice+takeprofit*pointsize)/openprice)-1
shortontrading=0 //not on market anymore
endif
//shit happens!
if mae<=-stoploss*pointsize then //testing the stoploss
orderPNL=-(((openprice-stoploss*pointsize)/openprice)-1)
shortontrading=0 //not on market anymore
endif
endif
//compute equity curve and its average
if ( (not onmarket and onmarket[1]) or (tradeindex(1)=tradeindex(2) and tradeindex(1)=barindex[1]) or (not longontrading and longontrading[1])or (not shortontrading and shortontrading[1]) ) and lastcheck<>barindex then //check if an order has just closed or not
lastcheck = barindex
if(not longontrading[1]) then //if it was a real order
orderPNL = positionperf(1) //let's take the real position perf (otherwise the last orderPNL is kept (fake order)
endif
if(not shortontrading[1]) then //if it was a real order
orderPNL = positionperf(1) //let's take the real position perf (otherwise the last orderPNL is kept (fake order)
endif
strategyPNL = strategyPNL+orderPNL //cumulate the strategy PnL
//build a loop to make the equity curve average
count=0
sum=0
lastPNL=0
for i = 0 to barindex do
if strategyPNL[i]<>lastPNL then
lastPNL=strategyPNL[i]
count=count+1
sum=sum+strategyPNL[i]
if count=equityCurvePeriod+1 then
sum=sum-last
last=strategyPNL[i]
break
//
endif
endif
next
if last<>last[1] then
avg = (sum/equityCurvePeriod)
endif
if strategyPNL>avg then
realtrading=1 //activate real trading if the PnL is superior to its average
else
realtrading=0 //or desactivate real trading
endif
if ordercount<=equityCurvePeriod or activateSimulatedTrading=0 then
realtrading=1 //if not enough orders since the beginning or if simulated trading is force to false, we keep on real trading
endif
endif
//plot the fake orders activation
if longontrading then
plotLong = 0.1 //this value might be changed, depending of the strategy
else
plotLong = 0
endif
//
//plot the fake orders activation
if shortontrading then
plotShort = -0.1 //this value might be changed, depending of the strategy
else
plotShort = 0
endif
//
//plot values
graph strategyPNL //plot the cumulated PnL
graph avg coloured(0,0,255) //plot the average of the equity curve
graph plotLong coloured(0,155,0) //plot the fake orders activation
graph plotShort coloured(255,0,0) //plot the fake orders activation
Your MAE/MFE calculation for short orders is wrong, it should be the inverse of the long orders! 🙂
Lines 72 & 73:
mfe = max(openprice-low,mfe) //compute the MaxFavorableExcursion
mae = min(openprice-high,mae) //compute the MaxAdverseExcursion
this is the first thing I spotted, I did not read all the code.
for the short as well,
Hi have you managed to get this to work as yet? Ive also tried it, but not managing to get to work with SHORT positions. i fixed the Lines 72 & 73 as nicolas suggested above too.