How to code a “real life stop”

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #30627 quote
    victormork
    Participant
    Veteran

    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.

    #30678 quote
    Derek
    Participant
    Veteran

    Hi!

    Like this?

    Slong = Highest [20](high)

    Sshort = Lowest [20](low)

    If longonmarket then

    Set stop loss Slong

    Endif

    I did not test it since I am mobile. It’s justa Donchian Channel that saves the recent highs and lows.

    #30707 quote
    victormork
    Participant
    Veteran

    Yes! 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?

    #30708 quote
    GraHal
    Participant
    Master

    As 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

    #30882 quote
    Derek
    Participant
    Veteran

    @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.

    #30944 quote
    Nicolas
    Keymaster
    Master

    Thanks Derek and Grahal, but the code should be syntaxed like this instead:

    //stoploss for long position 
    If 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 Slong
    Endif
    
    //stoploss for short position 
    if shortonmarket then 
     //calculate the short order stoploss (difference between the open price of the trade and the highest high)
     Sshort = Highest [20](high) - Tradeprice
     Set stop loss Sshort
    Endif

    “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.

    victormork and AlgoAlex thanked this post
    #147364 quote
    phoentzs
    Participant
    Master

    Hallo, 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?

    #147383 quote
    robertogozzi
    Moderator
    Master
    @phoentzs Only 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 🙂
    #147384 quote
    robertogozzi
    Moderator
    Master
    It’s not clear what you want. Post your code to help further.
    #147387 quote
    phoentzs
    Participant
    Master

    Excuse 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 H1
    
    
    DEFPARAM CumulateOrders = false
    defparam preloadbars = 1000
    //daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // VALEURS A DEFINIR
    REINV = 0    // 0 = pas de réinvestissement / 1 = réinvestissement des gains
    LEVIER = 1   // nombre de contrats par défaut
    CAPITALinit = 3000  // Capital initial, à définir pour le réinvestissement des gains
    
    
    IF REINV = 0 THEN
    n = LEVIER
    ELSIF REINV = 1 THEN
    CAPITAL = CAPITALinit + strategyprofit
    n = (CAPITAL / CAPITALinit)*levier
    IF n < 1 THEN
    n=1
    ENDIF
    ENDIF
    n = round(n)
    
    Nlong = n
    Nshort = n
    
    
    myEMA = ExponentialAverage[21](close)   //45 15
    myEMA2 = ExponentialAverage[89](close)
    c1 = myema crosses over myEMA2
    c2 = myema crosses under myEMA2
    
    
    
    //IF Time >= 080000 AND Time <= 190000 THEN
    
    IF c1 THEN  // not onmarket and
    BUY Nlong shares at market
    Slong = Tradeprice-Lowest [1](low)  //5
    Set stop loss Slong
    set target profit slong*2
    
    ENDIF
    
    IF c2 THEN     //not onmarket and
    sellshort Nshort shares at market
    Sshort = Highest [1](high) - Tradeprice  //5
    Set stop loss Sshort
    set target profit sshort*2
    
    ENDIF
    //endif
    
    if longonmarket and c2 then  //or c111
    sell at market
    endif
    
    if shortonmarket and c1 then  //or c222
    exitshort at market
    endif
    
    
    if time = 220000 and dayofweek=5 then //and dayofweek=5
    sell at market
    EXITSHORT at market
    endif
    
     

    Why? what do i have to change?

    #147413 quote
    robertogozzi
    Moderator
    Master
    Replace line 38 with:
    IF c1 and not onmarket THEN
    and line 46 with:
    IF c2 and not onmarket THEN
    otherwise 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.
    phoentzs thanked this post
    #147434 quote
    phoentzs
    Participant
    Master

    Unfortunately, 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 H1
    
    
    DEFPARAM CumulateOrders = false
    defparam preloadbars = 1000
    //daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
    
    // VALEURS A DEFINIR
    REINV = 0    // 0 = pas de réinvestissement / 1 = réinvestissement des gains
    LEVIER = 1   // nombre de contrats par défaut
    CAPITALinit = 3000  // Capital initial, à définir pour le réinvestissement des gains
    
    
    IF REINV = 0 THEN
    n = LEVIER
    ELSIF REINV = 1 THEN
    CAPITAL = CAPITALinit + strategyprofit
    n = (CAPITAL / CAPITALinit)*levier
    IF n < 1 THEN
    n=1
    ENDIF
    ENDIF
    n = round(n)
    
    Nlong = n
    Nshort = n
    
    
    myEMA = ExponentialAverage[21](close)   //45 15
    myEMA2 = ExponentialAverage[89](close)
    c1 = myema crosses over myEMA2
    c2 = myema crosses under myEMA2
    
    
    
    //IF Time >= 080000 AND Time <= 190000 THEN
    
    IF c1 and not onmarket THEN  // not onmarket and
    BUY Nlong shares at market
    Slong = (Tradeprice-Lowest [1](low))  //Tradeprice
    Set stop loss Slong
    set target profit slong*1.5
    
    ENDIF
    
    IF c2 and not onmarket THEN     //not onmarket and
    sellshort Nshort shares at market
    Sshort = (Highest [1](high) - Tradeprice)  //Tradeprice
    Set stop loss Sshort
    set target profit sshort*1.6
    
    ENDIF
    //endif
    
    if longonmarket and c2 then  
    sell at market
    endif
    
    if shortonmarket and c1 then  
    exitshort at market
    endif
    
    
    if time = 220000 and dayofweek=5 then 
    sell at market
    EXITSHORT at market
    endif
    #147438 quote
    robertogozzi
    Moderator
    Master
    The 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:
    //EurUsd H1
    
    DEFPARAM CumulateOrders = false
    defparam preloadbars = 1000
    //daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
     
    // VALEURS A DEFINIR
    REINV = 0    // 0 = pas de réinvestissement / 1 = réinvestissement des gains
    LEVIER = 1   // nombre de contrats par défaut
    CAPITALinit = 3000  // Capital initial, à définir pour le réinvestissement des gains
     
    IF REINV = 0 THEN
    n = LEVIER
    ELSIF REINV = 1 THEN
    CAPITAL = CAPITALinit + strategyprofit
    n = (CAPITAL / CAPITALinit)*levier
    IF n < 1 THEN
    n=1
    ENDIF
    ENDIF
    n = round(n)
     
    Nlong = n
    Nshort = n
     
    myEMA = ExponentialAverage[21](close)   //45 15
    myEMA2 = ExponentialAverage[89](close)
    c1 = myema crosses over myEMA2
    c2 = 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 THEN
     
    IF c1 THEN  // not onmarket and
    BUY Nlong shares at market
    Slong = (Close - Lowest [1](low))  //Tradeprice
    Set stop loss Slong
    set target profit slong*1.5
    ENDIF
     
    IF c2 THEN     //not onmarket and
    sellshort Nshort shares at market
    Sshort = (Highest [1](high) - Close)  //Tradeprice
    Set stop loss Sshort
    set target profit sshort*1.6
    ENDIF
    //endif
     
    if time = 220000 and dayofweek=5 then
    sell at market
    EXITSHORT at market
    endif
    graph Slong
    graph Sshort
    graph c1
    graph c2
    graph OnMarket
    phoentzs thanked this post
Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.

How to code a “real life stop”


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
victormork @victormork Participant
Summary

This topic contains 12 replies,
has 6 voices, and was last updated by robertogozzi
5 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/01/2017
Status: Active
Attachments: No files
Logo Logo
Loading...