Forums › ProRealTime English forum › ProOrder support › BreakEven & Trailing Profit: complete function › Reply To: BreakEven & Trailing Profit: complete function
a try to cover a position on the trailingstop of robertogozzi
Basically it uses the levels of Donchian if the trade is triggered. If the trailing stop is not triggered, it uses previous determined lows before the trade to exit, with in consideration to a minimum distance to the tradeprice.
Also has a second exit method if above is not triggered and the lines are narrow. If it crosses the top line for a long and retraces back to the lowest it exits too, again with in consideration of a minimum distance between max & min levels
With this, most likely with mistakes, I hope that if the market reverse there is an alternative way to exit the trade before going to the stoploss.
Instead of a fixed amount to start the trailingstop, it uses now a %. Pipsize is left out on the part I added. I’am unsure if it is * or /. What I added can be disabled.
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 |
once trailingstop=1 once useDonchian =1 // if breakeven is not triggered if trailingstop then if ts2sensitivity=1 then ts2sensitivitylong=close ts2sensitivityshort=close elsif ts2sensitivity=2 then ts2sensitivitylong=high ts2sensitivityshort=low endif once hdp1=close once ldp1=close once hdp2=close once ldp2=close once hdp3=close once ldp3=close // below; 0 = disable minimumdistance critera mdist=((tradeprice(1)/100)*0.09) mdist2=((tradeprice(1)/100)*0.09) if longonmarket and (shortonmarket[1] or not onmarket[1]) or shortonmarket and (longonmarket[1] or not onmarket[1]) then once flag=0 hdp1=highest[7](high)[1] ldp1=lowest[7](low)[1] hdp2=highest[14](high)[1] ldp2=lowest[14](low)[1] hdp3=highest[21](high)[1] ldp3=lowest[21](low)[1] maxrun=max(hdp1,hdp2) maxrun2=max(maxrun,hdp3) minrun=min(ldp1,ldp2) minrun2=min(minrun,ldp3) endif if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) then trailstart2 = 0.1 //% start trailing profits from this point basepercent = 0.2 //20.0% profit percentage to keep when setting berakeven stepsize = 2 //10 pip chunks to increase percentage percentinc = 0.1 //10.0% percent increment after each stepsize chunk barnumber = 10 //10 add further % so trades don't keep running too long barpercent = 0.1 //10% add this additional percentage every barnumber bars roundto = 0 //-0.5 rounds lower, +0.4 higher, 0 defaults prt behaviour pricedistance = 10 * pipsize //7 minimun distance from current price y1 = 0 //reset to 0 y2 = 0 //reset to 0 profitpercent = basepercent //reset to desired default value tradebar = barindex trailstart = ((close/100)*trailstart2) ts2sensitivity= 2 elsif longonmarket then if ts2sensitivitylong > (tradeprice(1) + (y1 * pipsize)) then x1 = (ts2sensitivitylong - tradeprice(1)) / pipsize if x1 >= trailstart then diff1 = abs(trailstart - x1) chunks1 = max(0,round((diff1 / stepsize) + roundto)) profitpercent = basepercent + (basepercent * (chunks1 * percentinc)) barcount = barindex - tradebar if barcount mod barnumber = 0 then profitpercent = profitpercent + barpercent endif profitpercent = max(profitpercent[1],min(100,profitpercent)) y1 = max(x1 * profitpercent, y1) endif else if useDonchian then if (tradeprice(1)-ldp1)>mdist and close crosses under ldp1 then sell at market endif if (tradeprice(1)-ldp2)>mdist and close crosses under ldp2 then sell at market endif if (tradeprice(1)-ldp3)>mdist and close crosses under ldp3 then sell at market endif if high crosses over maxrun2 then flag=1 endif if flag=1 and low crosses under minrun2 and maxrun2-minrun2>mdist2 then sell at market endif endif endif elsif shortonmarket then if ts2sensitivityshort < (tradeprice(1) - (y2 * pipsize)) then x2 = (tradeprice(1) - ts2sensitivityshort) / pipsize if x2 >= trailstart then diff2 = abs(trailstart - x2) chunks2 = max(0,round((diff2 / stepsize) + roundto)) profitpercent = basepercent + (basepercent * (chunks2 * percentinc)) barcount = barindex - tradebar if barcount mod barnumber = 0 then profitpercent = profitpercent + barpercent endif profitpercent = max(profitpercent[1],min(100,profitpercent)) y2 = max(x2 * profitpercent, y2) endif else if useDonchian then if hdp1-tradeprice(1)>mdist and close crosses over hdp1 then exitshort at market endif if hdp2-tradeprice(1)>mdist and close crosses over hdp2 then exitshort at market endif if hdp3-tradeprice(1)>mdist and close crosses over hdp3 then exitshort at market endif if low crosses under minrun2 then flag=1 endif if flag=1 and high crosses over maxrun2 and maxrun2-minrun2>mdist2 then exitshort at market endif endif endif endif if y1 then sellprice = tradeprice(1) + (y1 * pipsize) if abs(ts2sensitivitylong - sellprice) > pricedistance then if ts2sensitivitylong >= sellprice then sell at sellprice stop else sell at sellprice limit endif else sell at market endif endif if y2 then exitprice = tradeprice(1) - (y2 * pipsize) if abs(ts2sensitivityshort - exitprice) > pricedistance then if ts2sensitivityshort <= exitprice then exitshort at exitprice stop else exitshort at exitprice limit endif else exitshort at market endif endif endif graphonprice hdp1 graphonprice ldp1 graphonprice hdp2 graphonprice ldp2 graphonprice hdp3 graphonprice ldp3 |