Nicolas Simulated Trading with fake orders
Forums › ProRealTime English forum › ProOrder support › Nicolas Simulated Trading with fake orders
- This topic has 2 replies, 3 voices, and was last updated 3 years ago by
SkippyTrader.
-
-
06/18/2021 at 2:02 PM #171993
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?
Code123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152defparam cumulateorders=false// --- strategy settingstpratio = 2.7 //2.7stoploss = 65 //65takeprofit = stoploss*tpratio// --- simulated trading settingsequityCurvePeriod = 20 //orders quantity for the equity curve averageactivateSimulatedTrading = 1 //(0= false / 1 =true)//strategiesbuysignal = average[7] crosses over average[21]//rsi[2] crosses over 30sellsignal = average[7] crosses under average[21]//rsi[2] crosses over 30if realtrading then //real tradingif not longonmarket and buysignal thenbuy 1 contracts at marketordercount=ordercount+1 //counting the order to know we have sufficient ones to activate the simulation laterendifset target pprofit takeprofitset stop ploss stoplosselsif not realtrading and ordercount>equityCurvePeriod then //fake tradingif not longontrading and buysignal thenopenprice=close //fake order open pricelongontrading=1 //we are now on market//reset MFE & MAE valuesmfe=0mae=0//fake orders countfakeorders=fakeorders+1endifendifif realtrading then //real tradingif not shortonmarket and sellsignal thensellshort 1 contract at marketordercount=ordercount+1 //counting the order to know we have sufficient ones to activate the simulation laterendifset target pprofit takeprofitset stop ploss stoplosselsif not realtrading and ordercount>equityCurvePeriod then //fake tradingif not shortontrading and sellsignal thenopenprice=close //fake order open priceshortontrading=1 //we are now on market//reset MFE & MAE valuesmfe=0mae=0//fake orders countfakeorders=fakeorders+1endifendif//check profit n loss of the fake orderif longontrading=1 thenmfe = max(high-openprice,mfe) //compute the MaxFavorableExcursionmae = min(low-openprice,mae) //compute the MaxAdverseExcursion//profit achievedif mfe>=takeprofit*pointsize then //testing the takeprofitorderPNL=((openprice+takeprofit*pointsize)/openprice)-1longontrading=0 //not on market anymoreendif//shit happens!if mae<=-stoploss*pointsize then //testing the stoplossorderPNL=-(((openprice-stoploss*pointsize)/openprice)-1)longontrading=0 //not on market anymoreendifendif//check profit n loss of the fake orderif shortontrading=1 thenmfe = max(high-openprice,mfe) //compute the MaxFavorableExcursionmae = min(low-openprice,mae) //compute the MaxAdverseExcursion//profit achievedif mfe>=takeprofit*pointsize then //testing the takeprofitorderPNL=((openprice+takeprofit*pointsize)/openprice)-1shortontrading=0 //not on market anymoreendif//shit happens!if mae<=-stoploss*pointsize then //testing the stoplossorderPNL=-(((openprice-stoploss*pointsize)/openprice)-1)shortontrading=0 //not on market anymoreendifendif//compute equity curve and its averageif ( (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 notlastcheck = barindexif(not longontrading[1]) then //if it was a real orderorderPNL = positionperf(1) //let's take the real position perf (otherwise the last orderPNL is kept (fake order)endifif(not shortontrading[1]) then //if it was a real orderorderPNL = positionperf(1) //let's take the real position perf (otherwise the last orderPNL is kept (fake order)endifstrategyPNL = strategyPNL+orderPNL //cumulate the strategy PnL//build a loop to make the equity curve averagecount=0sum=0lastPNL=0for i = 0 to barindex doif strategyPNL[i]<>lastPNL thenlastPNL=strategyPNL[i]count=count+1sum=sum+strategyPNL[i]if count=equityCurvePeriod+1 thensum=sum-lastlast=strategyPNL[i]break//endifendifnextif last<>last[1] thenavg = (sum/equityCurvePeriod)endifif strategyPNL>avg thenrealtrading=1 //activate real trading if the PnL is superior to its averageelserealtrading=0 //or desactivate real tradingendifif ordercount<=equityCurvePeriod or activateSimulatedTrading=0 thenrealtrading=1 //if not enough orders since the beginning or if simulated trading is force to false, we keep on real tradingendifendif//plot the fake orders activationif longontrading thenplotLong = 0.1 //this value might be changed, depending of the strategyelseplotLong = 0endif////plot the fake orders activationif shortontrading thenplotShort = -0.1 //this value might be changed, depending of the strategyelseplotShort = 0endif////plot valuesgraph strategyPNL //plot the cumulated PnLgraph avg coloured(0,0,255) //plot the average of the equity curvegraph plotLong coloured(0,155,0) //plot the fake orders activationgraph plotShort coloured(255,0,0) //plot the fake orders activation06/21/2021 at 10:58 AM #172076Your MAE/MFE calculation for short orders is wrong, it should be the inverse of the long orders! 🙂
Lines 72 & 73:
12mfe = max(openprice-low,mfe) //compute the MaxFavorableExcursionmae = min(openprice-high,mae) //compute the MaxAdverseExcursionthis is the first thing I spotted, I did not read all the code.
04/07/2022 at 2:33 PM #191383for 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.
-
AuthorPosts
Find exclusive trading pro-tools on