Moving Stop Loss
Forums › ProRealTime English forum › ProOrder support › Moving Stop Loss
- This topic has 12 replies, 2 voices, and was last updated 6 months ago by SnorreDK.
-
-
06/23/2020 at 3:28 PM #136945
Please I have this code below which is running in auto trader, but the 0 stop loss never executes, neither does the trailing stop loss, how can I get my initial stop loss to move if I am in profit. I am assuming that tradeprice(1) is the market price the trade was entered at, hence if this was running for crude market via auto trader and there was another auto trader running for say ftse, then tradeprice(1) for each trade should point to their respective market entry price.
Any help will be appreciated, Many thanks
12345678910111213BUY 1 PERPOINT AT MARKET // Conditions to enter long positionsSET STOP LOSS 5IF LongOnMarket THENpricediff = Close – tradeprice(1)IF ( pricediff = 5 ) ThenSET STOP LOSS 0ELSIF (pricediff > 5) thenSET STOP TRAILING pricediffENDIFENDIF06/23/2020 at 4:05 PM #136953Always use the ‘Insert PRT Code’ button when putting code in your posts to make it easier for others to read.
Thank you 🙂
06/23/2020 at 4:09 PM #136955At line 6 it’s almost impossible that the difference be 5.0 pips, that’s why it never gets triggered.
SET STOP LOSS 0 simply removes your SL, leaving your trade WITHOUT any stop loss.
06/23/2020 at 4:22 PM #136957Moving Stop Loss12345678BUY 1 PERPOINT AT MARKET // Conditions to enter long positionsSET STOP LOSS 5IF LongOnMarket THENpricediff = Close – tradeprice(1)IF (pricediff > 5) thenSET STOP TRAILING pricediffENDIFPlease so this should move the initial stop loss to new level when the market moves beyond 5 pips? Many thanks
06/23/2020 at 4:24 PM #136958Yes, that’s correct!
06/23/2020 at 4:30 PM #136959Okay, I will try that okay, but I am sure I tried this before and it never worked hence introduced the 0 stop loss in between just to see what will happen and that never worked hence posted code here.
Many thanks. I am testing this in demo environment.
06/23/2020 at 4:37 PM #136960@tboafo, here’s 3 different trailing stops you can try. All are very good, each will give different results. The first, by Nicolas is a good place to start and it includes a break even function. The others, by Paul and Roberto are more sophisticated. You’ll have to play around with the variables, obviously.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227//%trailing stop functiontrailingPercent = tststepPercent = stif onmarket thentrailingstart = tradeprice(1)*(trailingpercent/100) //trailing will start @trailingstart points profittrailingstep = tradeprice(1)*(stepPercent/100) //% step to move the stoplossendif//reset the stoploss valueIF NOT ONMARKET THENnewSL=0ENDIF//manage long positionsIF LONGONMARKET THEN//first move (breakeven)IF newSL=0 AND close-tradeprice(1)>=trailingstart THENnewSL = tradeprice(1)+trailingstepENDIF//next movesIF newSL>0 AND close-newSL>trailingstep THENnewSL = newSL+trailingstepENDIFENDIF//manage short positionsIF SHORTONMARKET THEN//first move (breakeven)IF newSL=0 AND tradeprice(1)-close>=trailingstart THENnewSL = tradeprice(1)-trailingstepENDIF//next movesIF newSL>0 AND newSL-close>trailingstep THENnewSL = newSL-trailingstepENDIFENDIF//stop order to exit the positionsIF newSL>0 THENSELL AT newSL STOPEXITSHORT AT newSL STOPENDIF//****************************************************************************************// trailing stop atronce trailingstoptype = 1 // trailing stop - 0 off, 1 ononce tsincrements = st // typically between 0 and 0.25once tsminatrdist = tsm // typically between 1 and 4once tsatrperiod = 14 // ts atr parameteronce tsminstop = 5 // ts minimum stop distance, set to IG min valueonce tssensitivity = 1 // [0]close;[1]high/lowif trailingstoptype thenif barindex=tradeindex thentrailingstoplong = tst // ts atr distance, typically between 4 and 10trailingstopshort = tst // ts atr distance, typically between 4 and 10elseif longonmarket thenif tsnewsl>0 thenif trailingstoplong>tsminatrdist thenif tsnewsl>tsnewsl[1] thentrailingstoplong=trailingstoplongelsetrailingstoplong=trailingstoplong-tsincrementsendifelsetrailingstoplong=tsminatrdistendifendifendifif shortonmarket thenif tsnewsl>0 thenif trailingstopshort>tsminatrdist thenif tsnewsl<tsnewsl[1] thentrailingstopshort=trailingstopshortelsetrailingstopshort=trailingstopshort-tsincrementsendifelsetrailingstopshort=tsminatrdistendifendifendifendiftsatr=averagetruerange[tsatrperiod]((close/10)*pipsize)/1000//tsatr=averagetruerange[tsatrperiod]((close/1)*pipsize) // (forex)tgl=round(tsatr*trailingstoplong)tgs=round(tsatr*trailingstopshort)if not onmarket or ((longonmarket and shortonmarket[1]) or (longonmarket[1] and shortonmarket)) thentsmaxprice=0tsminprice=closetsnewsl=0endifif tssensitivity thentssensitivitylong=hightssensitivityshort=lowelsetssensitivitylong=closetssensitivityshort=closeendifif longonmarket thentsmaxprice=max(tsmaxprice,tssensitivitylong)if tsmaxprice-tradeprice(1)>=tgl*pointsize thenif tsmaxprice-tradeprice(1)>=tsminstop thentsnewsl=tsmaxprice-tgl*pointsizeelsetsnewsl=tsmaxprice-tsminstop*pointsizeendifendifendifif shortonmarket thentsminprice=min(tsminprice,tssensitivityshort)if tradeprice(1)-tsminprice>=tgs*pointsize thenif tradeprice(1)-tsminprice>=tsminstop thentsnewsl=tsminprice+tgs*pointsizeelsetsnewsl=tsminprice+tsminstop*pointsizeendifendifendifif longonmarket thenif tsnewsl>0 thensell at tsnewsl stopendifif tsnewsl>0 thenif low crosses under tsnewsl thensell at market // when stop is rejectedendifendifendifif shortonmarket thenif tsnewsl>0 thenexitshort at tsnewsl stopendifif tsnewsl>0 thenif high crosses over tsnewsl thenexitshort at market // when stop is rejectedendifendifendifendif//**********************************************************************************//Roberto TSIF Not OnMarket THEN//// when NOT OnMarket reset values to default values//TrailStart = 30 //30 Start trailing profits from this pointBasePerCent = 0.200 //20.0% Profit percentage to keep when setting BerakEvenStepSize = 10 //10 Pip chunks to increase PercentagePerCentInc = 0.100 //10.0% PerCent increment after each StepSize chunkRoundTO = -0.5 //-0.5 rounds always to Lower integer, +0.4 rounds always to Higher integer, 0 defaults PRT behaviourPriceDistance = 7 * pipsize //7 minimun distance from current pricey1 = 0 //reset to 0y2 = 0 //reset to 0ProfitPerCent = BasePerCent //reset to desired default valueELSIF LongOnMarket AND close > (TradePrice + (y1 * pipsize)) THEN //LONG positions//// compute the value of the Percentage of profits, if any, to lock in for LONG trades//x1 = (close - tradeprice) / pipsize //convert price to pipsIF x1 >= TrailStart THEN // go ahead only if N+ pipsDiff1 = abs(TrailStart - x1) //difference from current profit and TrailStartChunks1 = max(0,round((Diff1 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks1 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y1 = max(x1 * ProfitPerCent, y1) //y1 = % of max profitENDIFELSIF ShortOnMarket AND close < (TradePrice - (y2 * pipsize)) THEN //SHORT positions//// compute the value of the Percentage of profits, if any, to lock in for SHORT trades//x2 = (tradeprice - close) / pipsize //convert price to pipsIF x2 >= TrailStart THEN // go ahead only if N+ pipsDiff2 = abs(TrailStart - x2) //difference from current profit and TrailStartChunks2 = max(0,round((Diff2 / StepSize) + RoundTO)) //number of STEPSIZE chunksProfitPerCent = BasePerCent + (BasePerCent * (Chunks2 * PerCentInc)) //compute new size of ProfitPerCentProfitPerCent = max(ProfitPerCent[1],min(100,ProfitPerCent)) //make sure ProfitPerCent doess not exceed 100%y2 = max(x2 * ProfitPerCent, y2) //y2 = % of max profitENDIFENDIFIF y1 THEN //Place pending STOP order when y1 > 0 (LONG positions)SellPrice = Tradeprice + (y1 * pipsize) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - SellPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close >= SellPrice THENSELL AT SellPrice STOPELSESELL AT SellPrice LIMITENDIFELSE////sell AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//SELL AT MarketENDIFENDIFIF y2 THEN //Place pending STOP order when y2 > 0 (SHORT positions)ExitPrice = Tradeprice - (y2 * pipsize) //convert pips to price//// check the minimun distance between ExitPrice and current price//IF abs(close - ExitPrice) > PriceDistance THEN//// place either a LIMIT or STOP pending order according to current price positioning//IF close <= ExitPrice THENEXITSHORT AT ExitPrice STOPELSEEXITSHORT AT ExitPrice LIMITENDIFELSE////ExitShort AT MARKET when EXITPRICE does not meet the broker's minimun distance from current price//EXITSHORT AT MarketENDIFENDIF3 users thanked author for this post.
06/23/2020 at 4:48 PM #13696206/23/2020 at 4:52 PM #136964Yes, as nonetheless suggested, using software snippets for TRAILING STOP is always the best choice. It depends on how large your SL & TP are, in this case SL is very tight, so if you use a volatile instrument and a fairly high TF (5 minutes could be pretty high in this case) it could be very difficult to manage it!
06/23/2020 at 5:29 PM #136966Link to above 3 x TS added as Log 224 here …
I added a Note that Code Authors are Nicolas, Paul and Roberto
2 users thanked author for this post.
05/17/2024 at 1:38 PM #23276405/17/2024 at 7:44 PM #232775I always use this one, sometimes in percentages, other times in pips.
05/17/2024 at 7:49 PM #232776 -
AuthorPosts
Find exclusive trading pro-tools on