Forums › ProRealTime forum Italiano › Supporto ProOrder › Migliorare una strategia con trades simulati › Reply To: Migliorare una strategia con trades simulati
02/23/2023 at 3:43 PM
#210286
Le condizioni di uscita cis sono già, lo Stop Loss, il Take Profit e lo Stop & Reverse, oltre al trailing stop. Non ho idea a quali condizioni ti riferisci.
Per il trailing stop basta aggiungere, alla fine (prima di GRAPH), le righe che hai postato. Questo è il codice completo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
// Simulated Tradesx // // https://www.prorealcode.com/blog/learning/how-to-improve-a-strategy-with-simulated-trades-1/ // 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 shortsignal = average[7] crosses under 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 elsif not shortonmarket and shortsignal 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 shortontrading=0 //we are now on market //reset MFE & MAE values mfe=0 mae=0 //fake orders count fakeorders=fakeorders+1 elsif not shortontrading and shortsignal then openprice=close //fake order open price shortontrading=1 //we are now on market longontrading=0 //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(openprice-low,mfe) //compute the MaxFavorableExcursion mae = min(openprice-high,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 plotShort = 0 else plotShort = 0.01 //this value might be changed, depending of the strategy plotLong = 0 endif // //****************************************************************************** //trailing stop function IF NOT ONMARKET THEN TrailingStart = 20//sp //20 trailing will start @trailinstart points profit TrailingStep = 5 //ts //5 trailing step to move the "stoploss" Distance = 7 //pk //7 pips Distance from caurrent price (if required by the broker) PointsToKeep = 1 //1 pips to be gained when breakeven is set //reset the stoploss value newSL=0 ENDIF IF (BarIndex - TradeIndex) >= 0 THEN //0 //manage long positions IF LONGONMARKET THEN //first move (breakeven) IF newSL=0 AND high-TradePrice(1)>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN newSL = TradePrice(1)+TrailingStep*PipSize+PointsToKeep*PipSize ENDIF //next moves IF newSL>0 AND close-newSL>=TrailingStep*PipSize THEN newSL = newSL+TrailingStep*PipSize ENDIF ENDIF //manage short positions IF SHORTONMARKET THEN //first move (breakeven) IF newSL=0 AND TradePrice(1)-low>=(TrailingStart*PipSize+PointsToKeep*PipSize) THEN newSL = TradePrice(1)-TrailingStep*PipSize+PointsToKeep*PipSize ENDIF //next moves IF newSL>0 AND newSL-close>=TrailingStep*PipSize THEN newSL = newSL-TrailingStep*PipSize ENDIF ENDIF //stop order to exit the positions IF newSL>0 THEN IF LongOnMarket THEN IF (close + Distance) > newSL THEN SELL AT newSL STOP ELSIF (close - Distance) < newSL THEN SELL AT newSL LIMIT ELSE SELL AT Market ENDIF ELSIF ShortOnmarket THEN IF (close + Distance) < newSL THEN EXITSHORT AT newSL STOP ELSIF (close - Distance) > newSL THEN EXITSHORT AT newSL LIMIT ELSE EXITSHORT AT Market ENDIF ENDIF ENDIF 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(0,155,0) //plot the fake orders activation |