Forums › ProRealTime English forum › ProOrder support › Hard coded Stop Loss becoming Trailing Stop Loss › Reply To: Hard coded Stop Loss becoming Trailing Stop Loss
05/25/2019 at 2:17 PM
#99427
Grahal, on paper yes first I had the same backtest as you, and I thought, waoou nice for a so simple code, then I realized that he is using the ptrailing function… Here the same code with a true breakeven function, and a true trailing function. Backtests are less inspiring…
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 |
DEFPARAM CumulateOrders = False // Cumulating positions deactivated positionsize=1 // Prevents the system from creating new orders to enter the market or increase position size before the specified time noEntryBeforeTime = 090000 timeEnterBefore = time >= noEntryBeforeTime // Conditions to enter long positions indicator1 = SuperTrend[4,90] c1 = (close CROSSES OVER indicator1) IF c1 AND timeEnterBefore THEN BUY positionsize contract AT MARKET ENDIF // Conditions to exit long positions indicator2 = SuperTrend[4,90] c2 = (close CROSSES UNDER indicator2) IF c2 THEN SELL AT MARKET ENDIF // Conditions to enter short positions indicator3 = SuperTrend[4,90] c3 = (close CROSSES UNDER indicator3) IF c3 AND timeEnterBefore THEN SELLSHORT positionsize contract AT MARKET ENDIF // Conditions to exit short positions indicator4 = SuperTrend[4,90] c4 = (close CROSSES OVER indicator4) IF c4 THEN EXITSHORT AT MARKET ENDIF enableBreakeven=1 BESG = 0.1// % BE kick start BESL = 0.02 // % BE level underlaying=100 //100 for index if enableBreakeven then if not onmarket then newsl=0 endif if ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then newsl=0 endif if longonmarket then if close-tradeprice(1)>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then newsl=tradeprice(1)+(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize endif endif if shortonmarket then if tradeprice(1)-close>=(((tradeprice(1)/100)*BESG)/(underlaying/100))*pointsize then newsl=tradeprice(1)-(((tradeprice(1)/100)*BESL)/(underlaying/100))*pointsize endif endif if longonmarket and newsl>0 then sell at newsl stop endif if shortonmarket and newsl>0 then exitshort at newsl stop endif endif //trailing stop trailingon=1 MFEPerc=0.05 If trailingon then trailingstop = (close * MFEPerc/100)/pointsize //14 //resetting variables when no trades are on market if not onmarket then MAXPRICE = 0 MINPRICE = close priceexit = 0 endif //case SHORT order if shortonmarket then MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade if tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level endif endif //case LONG order if longonmarket then MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level endif endif //exit on trailing stop price levels if onmarket and priceexit>0 then EXITSHORT AT priceexit STOP SELL AT priceexit STOP endif endif //reset the breakevenLevel when no trade are on market //IF NOT ONMARKET THEN // //breakevenLevel=0 // //ENDIF // Stops and targets //SET STOP pTRAILING 10 //place the new stop orders on market at breakevenLevel //IF breakevenLevel>0 THEN // //SELL AT breakevenLevel STOP // //ENDIF |