Ho trovato questo articolo di Nicolas che in alcune strategie è migliorata non come performance ma come Max Drawdown ……però è solo per operazioni Long, ho cercato purtroppo invano di metterla anche per lo Short.
Se qualcuno è in grado di aiutarmi
Grazie Mauro
defparam cumulateorders=false
// --- strategy settings
tpratio = 2.7
stoploss = 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
if realtrading then //real trading
if not longonmarket and buysignal then
buy 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
//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
//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]) ) 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
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.01 //this value might be changed, depending of the strategy
else
plotLong = 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
Ho provato a modificarla, ma non sono sicuro funzioni tutto correttamente, almeno per la parte di simulazione.
Fai delle prove:
defparam cumulateorders=false
once longontrading=0
once shortontrading=0
// --- strategy settings
tpratio = 2.7
stoploss = 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 under 30
if realtrading then //real trading
if not longonmarket and buysignal then
buy at market
ordercount=ordercount+1 //counting the order to know we have sufficient ones to activate the simulation later
endif
if not shortonmarket and sellsignal then
sellshort 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
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
elsif 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]) and (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.01 //this value might be changed, depending of the strategy
else
plotLong = 0
endif
if shortontrading then
plotShort = 0.01 //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
Come al solito grazie…. Provo se funziona.