How to code a “real life stop”
Forums › ProRealTime English forum › ProOrder support › How to code a “real life stop”
- This topic has 12 replies, 6 voices, and was last updated 3 years ago by robertogozzi.
-
-
04/01/2017 at 6:43 PM #30627
does anyone know how to code a stop loss as you would use it when you trade manually. For example, the system is placing a trade and the stop is set to be 5 points above or below latest high/low depending of long/short position. To simplify we could say that we want the stop below/above N periods max or min price.
04/02/2017 at 10:21 AM #3067804/02/2017 at 1:24 PM #30707Yes! thanks! But I think Highest and Lowest needs to change place? If I’m long I want the stop to be att lowest and if I’m short I want the stop to be at highest?
04/02/2017 at 1:29 PM #30708As the stop is tied to highs / lows then the stop level would auto-recalculate at the end of every bar?
When we get multi-timeframe I hope we will be able to have stops recalc on, for example, a 1 Min bar where the System may be running on a 1H bar?
GraHal
04/03/2017 at 7:36 PM #30882@Victor: Yep, the high and low in the stop loss should be reversed. Did it actually work or is Grahal’s asdumption correct? In this case you need to put the stop loss command inside your buy/sell if statement.
If it is already working you need to check what would happen if the market crashes in the very first bar you are in the market. Maybe my snippet above will leave you without a stop because “if longonmarket” is not true when they entry is triggered.
04/04/2017 at 8:30 AM #30944Thanks Derek and Grahal, but the code should be syntaxed like this instead:
12345678910111213//stoploss for long positionIf longonmarket then//calculate the long stoploss (difference between the open price of the trade and the lowest low)Slong = Tradeprice-Lowest [20](low)Set stop loss SlongEndif//stoploss for short positionif shortonmarket then//calculate the short order stoploss (difference between the open price of the trade and the highest high)Sshort = Highest [20](high) - TradepriceSet stop loss SshortEndif“Set stop loss” do not use price level, but points amount, so we need to calculate the distance between the order open price and the price level where we want the stoploss to be set.
2 users thanked author for this post.
10/14/2020 at 8:02 AM #147364Hallo, Weiß jemand, ob die SL hier mitziehen? Das sollten sie nicht tun. Aber wenn ich damit mein System berechne, ändern sich die Ergebnisse immer im Backtest. Ich berechne lange, kurze Änderungen. Wenn ich kurz berechne, ändern sich die Ergebnisse für lang wieder. obwohl mein System ein umgekehrtes System ist. was ist das? Wie kann ich das ändern?
Hello, does anyone know whether the SL will pull along? They shouldn’t do that. But when I use it to calculate my system, the results always change in the back test. I calculate long, short changes. If I calculate briefly, the results change again for a long time. although my system is an inverse system. What is it? How can I change this?10/14/2020 at 10:02 AM #147383Only post in the language of the forum that you are posting in. For example English only in the English speaking forums and French only in the French speaking forums.
Thank you 🙂
10/14/2020 at 10:05 AM #147384It’s not clear what you want.
Post your code to help further.
10/14/2020 at 10:31 AM #147387Excuse me please.
here is my simple code. If I change SL and TP in the long order, the results also change in the short in the backtest. and vice versa.
EurUsd H112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667//EurUsd H1DEFPARAM CumulateOrders = falsedefparam preloadbars = 1000//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// VALEURS A DEFINIRREINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gainsLEVIER = 1 // nombre de contrats par défautCAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gainsIF REINV = 0 THENn = LEVIERELSIF REINV = 1 THENCAPITAL = CAPITALinit + strategyprofitn = (CAPITAL / CAPITALinit)*levierIF n < 1 THENn=1ENDIFENDIFn = round(n)Nlong = nNshort = nmyEMA = ExponentialAverage[21](close) //45 15myEMA2 = ExponentialAverage[89](close)c1 = myema crosses over myEMA2c2 = myema crosses under myEMA2//IF Time >= 080000 AND Time <= 190000 THENIF c1 THEN // not onmarket andBUY Nlong shares at marketSlong = Tradeprice-Lowest [1](low) //5Set stop loss Slongset target profit slong*2ENDIFIF c2 THEN //not onmarket andsellshort Nshort shares at marketSshort = Highest [1](high) - Tradeprice //5Set stop loss Sshortset target profit sshort*2ENDIF//endifif longonmarket and c2 then //or c111sell at marketendifif shortonmarket and c1 then //or c222exitshort at marketendifif time = 220000 and dayofweek=5 then //and dayofweek=5sell at marketEXITSHORT at marketendifWhy? what do i have to change?
10/14/2020 at 3:45 PM #147413Replace line 38 with:
1IF c1 and not onmarket THENand line 46 with:
1IF c2 and not onmarket THENotherwise when C1 or C2 are true the block IF…ENDIF will be executed and values changed accordingly, despite no further trades would be opened, having accumulation been disabled at line 4.
1 user thanked author for this post.
10/14/2020 at 10:59 PM #147434Unfortunately, the values are still changing. When I calculate long, the short values change in the backtest and vice versa. Something is wrongly coded somewhere. Can someone help me?
EurUsd H112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667//EurUsd H1DEFPARAM CumulateOrders = falsedefparam preloadbars = 1000//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// VALEURS A DEFINIRREINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gainsLEVIER = 1 // nombre de contrats par défautCAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gainsIF REINV = 0 THENn = LEVIERELSIF REINV = 1 THENCAPITAL = CAPITALinit + strategyprofitn = (CAPITAL / CAPITALinit)*levierIF n < 1 THENn=1ENDIFENDIFn = round(n)Nlong = nNshort = nmyEMA = ExponentialAverage[21](close) //45 15myEMA2 = ExponentialAverage[89](close)c1 = myema crosses over myEMA2c2 = myema crosses under myEMA2//IF Time >= 080000 AND Time <= 190000 THENIF c1 and not onmarket THEN // not onmarket andBUY Nlong shares at marketSlong = (Tradeprice-Lowest [1](low)) //TradepriceSet stop loss Slongset target profit slong*1.5ENDIFIF c2 and not onmarket THEN //not onmarket andsellshort Nshort shares at marketSshort = (Highest [1](high) - Tradeprice) //TradepriceSet stop loss Sshortset target profit sshort*1.6ENDIF//endifif longonmarket and c2 thensell at marketendifif shortonmarket and c1 thenexitshort at marketendifif time = 220000 and dayofweek=5 thensell at marketEXITSHORT at marketendif10/15/2020 at 12:29 AM #147438The addition of And Not OnMarket is useless, it doesn’t change anything.
Slong never changes while LongOnMarket and Sshort never changes while ShortOnMarket.
There is an error in your code, in lines 40 and 48 you are using TRADEPRICE, but it’s not known yet, it’s the previous entry (or exit) price, since the current price will only be known when the candle closes. It must be replaced with CLOSE.
Lines 55-61 are actually not needed, since you code, as is, is doing Stop & Reverse. Anyway, if you want to use them, move them to line 34.
I added several GRAPH instruction at the end, so that you can check those values candle by candle in the variable window of the backtest.
This is the modified code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364//EurUsd H1DEFPARAM CumulateOrders = falsedefparam preloadbars = 1000//daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0// VALEURS A DEFINIRREINV = 0 // 0 = pas de réinvestissement / 1 = réinvestissement des gainsLEVIER = 1 // nombre de contrats par défautCAPITALinit = 3000 // Capital initial, à définir pour le réinvestissement des gainsIF REINV = 0 THENn = LEVIERELSIF REINV = 1 THENCAPITAL = CAPITALinit + strategyprofitn = (CAPITAL / CAPITALinit)*levierIF n < 1 THENn=1ENDIFENDIFn = round(n)Nlong = nNshort = nmyEMA = ExponentialAverage[21](close) //45 15myEMA2 = ExponentialAverage[89](close)c1 = myema crosses over myEMA2c2 = myema crosses under myEMA2//if longonmarket and c2 then//sell at market//endif////if shortonmarket and c1 then//exitshort at market//endif//IF Time >= 080000 AND Time <= 190000 THENIF c1 THEN // not onmarket andBUY Nlong shares at marketSlong = (Close - Lowest [1](low)) //TradepriceSet stop loss Slongset target profit slong*1.5ENDIFIF c2 THEN //not onmarket andsellshort Nshort shares at marketSshort = (Highest [1](high) - Close) //TradepriceSet stop loss Sshortset target profit sshort*1.6ENDIF//endifif time = 220000 and dayofweek=5 thensell at marketEXITSHORT at marketendifgraph Slonggraph Sshortgraph c1graph c2graph OnMarket1 user thanked author for this post.
-
AuthorPosts