Special trailing stop – hope it is possible!

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #118868 quote
    kmilo20000
    Participant
    New

    Hello,

    I am trying to create some sort of a trailing stop in which once I set the parameters for the initial stop loss (ATR*3) and everytime the price of the asset increases the equivalent of that stop loss I would risk a percentage  of the profits (trailing the stop). Percentage that would be changing everytime the price increases ‘N’ times the initial value of the initial stop loss;

    i.e. when the price of the asset bought has increased 1 time the ATR*3 (initial amount of the stop loss) I would risk the 100% of the profit, so the stop should be trailed to a break even position. Next if the price increases 2 times the ATR*3 (initial amount of stop loss), I would risk the 80% of the profit, meaning that I should trail the stop, but just enough to lock in a 20% profit and let the market take the other 80%, and so on…

     

    So far I have the following and I am out of ideas to code it and wondering if is even possible to do it:

     

    IF not longonmarket then
    BUY 1 CONTRACTS AT MARKET
    SET STOP LOSS averagetruerange[10](close[0])*3
    Endif

    Would appreciate any help!

    #118977 quote
    kmilo20000
    Participant
    New

    Well, so far I have been trying out this code (I think I am close to do it), but still not sure why it is not working yet:

     

    Defparam cumulateorders = false
    
    
    Timeframe (1 week)
    c1 = close[1]
    c2 = close[2]
    c3 = close[3]
    
    
    a1= open[1]
    a2= open[2]
    a3= open[3]
    
    
    MyADX18 = ADX[24]
    ADXperiods = 2
    
    MySMA24=average[24](c1)
    MySMA200= average[200](c1)
    
    Efficiency = call "Efficiency"
    
    Condition1 = c2>c3 and c1>c2
    
    Condition2= a2>a3 and a1>a2
    
    Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20
    
    Condition4 =  c1 > MySMA24
    
    Condition5 = c1 > MySMA200
    
    Condition6 = Efficiency>0.7
    
    
    Timeframe (default)
    
    Mystop = (averagetruerange[10](close[0]))*3
    
    IF not longonmarket and Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6 THEN
    BUY 1 CONTRACTS AT MARKET 
    SET STOP LOSS Mystop
    
    Endif
    
    IF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THEN
    
    SELL 1 CONTRACTS AT MARKET
    ENDIF
    
    //Stops
    
    IF NOT ONMARKET THEN
    newSL=0
    ENDIF
    
    Close0 = Tradeprice(1)
    
    Trailingstart = Close[0] + Mystop
    
    R3=((close0+(mystop*3)-Close0))*0.8
    
    R4=((close0+(mystop*4)-Close0))*0.75
    
    R5=((close0+(mystop*5)-Close0))*0.45
    
    R6=((close0+(mystop*6)-Close0))*0.45
    
    R7=((close0+(mystop*7)-Close0))*0.4
    
    R8=((close0+(mystop*8)-Close0))*0.3
    
    R9=((close0+(mystop*9)-Close0))*0.25
    
    R10=((close0+(mystop*10)-Close0))*0.25
    
    R11=((close0+(mystop*11)-Close0))*0.25
    
    R12=((close0+(mystop*12)-Close0))*0.12
    
    R13=((close0+(mystop*13)-Close0))*0.10
    
    R14=((close0+(mystop*14)-Close0))*0.10
    
    R15=((close0+(mystop*15)-Close0))*0.09
    
    R16=((close0+(mystop*16)-Close0))*0.09
    
    R17=((close0+(mystop*17)-Close0))*0.09
    
    R18=((close0+(mystop*18)-Close0))*0.08
    
    R19=((close0+(mystop*19)-Close0))*0.08
    
    R20=((close0+(mystop*20)-Close0))*0.08
    
    R21=((close0+(mystop*21)-Close0))*0.08
    
    R22=((close0+(mystop*22)-Close0))*0.05
    
    
    IF newSL>0 THEN
    SELL AT newSL STOP
    ENDIF
    
    
    IF LONGONMARKET THEN
    //first move (breakeven)
    IF newSL=0 AND close-Close0>=trailingstart and Close<(close0+(Mystop*2)) THEN
    newSL = (close0+mystop)
    ENDIF
    
    //next moves (trailing stop)
    IF newSL>0 AND close-newSL>=R3 AND close-newSL<R4 THEN
    newSL = newSL+R3
    ENDIF
    
    IF newSL>newSL+R3 AND close-newSL>=R4 AND close-newSL<R5 THEN
    newSL = newSL+R4
    ENDIF
    
    IF newSL>newSL+R4 AND close-newSL>=R5 AND close-newSL<R6 THEN
    newSL = newSL+R5
    ENDIF
    
    IF newSL>newSL+R5 AND close-newSL>=R6 AND close-newSL<R7 THEN
    newSL = newSL+R6
    ENDIF
    
    IF newSL>newSL+R6 AND close-newSL>=R7 AND close-newSL<R8 THEN
    newSL = newSL+R7
    ENDIF
    
    IF newSL>newSL+R7 AND close-newSL>=R8 AND close-newSL<R9 THEN
    newSL = newSL+R8
    ENDIF
    
    IF newSL>newSL+R8 AND close-newSL>=R9 AND close-newSL<R10 THEN
    newSL = newSL+R9
    ENDIF
    
    IF newSL>newSL+R9 AND close-newSL>=R10 AND close-newSL<R11 THEN
    newSL = newSL+R10
    ENDIF
    
    IF newSL>newSL+R10 AND close-newSL>=R11 AND close-newSL<R12 THEN
    newSL = newSL+R11
    ENDIF
    
    IF newSL>newSL+R11 AND close-newSL>=R12 AND close-newSL<R13 THEN
    newSL = newSL+R12
    ENDIF
    
    IF newSL>newSL+R12 AND close-newSL>=R13 AND close-newSL<R14 THEN
    newSL = newSL+R13
    ENDIF
    
    IF newSL>newSL+R13 AND close-newSL>=R14 AND close-newSL<R15 THEN
    newSL = newSL+R14
    ENDIF
    
    IF newSL>newSL+R14 AND close-newSL>=R15 AND close-newSL<R16 THEN
    newSL = newSL+R15
    ENDIF
    
    IF newSL>newSL+R15 AND close-newSL>=R16 AND close-newSL<R17 THEN
    newSL = newSL+R16
    ENDIF
    
    IF newSL>newSL+R16 AND close-newSL>=R17 AND close-newSL<R18 THEN
    newSL = newSL+R17
    ENDIF
    
    IF newSL>newSL+R17 AND close-newSL>=R18 AND close-newSL<R19 THEN
    newSL = newSL+R18
    ENDIF
    
    IF newSL>newSL+R18 AND close-newSL>=R19 AND close-newSL<R20 THEN
    newSL = newSL+R19
    ENDIF
    
    IF newSL>newSL+R19 AND close-newSL>=R20 AND close-newSL<R21 THEN
    newSL = newSL+R20
    ENDIF
    
    IF newSL>newSL+R20 AND close-newSL>=R21 AND close-newSL<R22 THEN
    newSL = newSL+R21
    ENDIF
    
    IF newSL>newSL+R21 AND close-newSL>=R22 THEN
    newSL = newSL+R22
    ENDIF
    
    ENDIF
    

    Would appreciate some help!

    #118986 quote
    robertogozzi
    Moderator
    Master

    Try this:

    IF Not OnMarket THEN
       MyProfit   = 0
       PrevProfit = 0
    ENDIF
    MyLongConditions  = close CROSSES OVER  average[200,0](close)
    MyShortConditions = close CROSSES UNDER average[200,0](close)
    IF MyLongConditions AND Not LongOnMarket then
       BUY       1 CONTRACTS AT MARKET
       MySL     = averagetruerange[10](close[0])*3
       SET STOP LOSS MySL
    Endif
    IF MyShortConditions AND Not ShortOnMarket then
       SELLSHORT 1 CONTRACTS AT MARKET
       MySL     = averagetruerange[10](close[0])*3
       SET STOP LOSS MySL
    Endif
    IF LongOnMarket THEN
       xProfit = close - TradePrice
       IF xProfit >= (MyProfit + MySL) THEN
          PrevProfit = MyProfit
          MyProfit   = MyProfit + MySL
       ENDIF
       IF MyProfit > 0 THEN
          SELL AT TradePrice + PrevProfit STOP
       ENDIF
    ELSIF ShortOnMarket THEN
       xProfit = TradePrice - close
       IF xProfit >= (MyProfit + MySL) THEN
          PrevProfit = MyProfit
          MyProfit   = MyProfit + MySL
       ENDIF
       IF MyProfit > 0 THEN
          SELL AT TradePrice - PrevProfit STOP
       ENDIF
    ENDIF
    #118991 quote
    kmilo20000
    Participant
    New

    Thank you Roberto! I will try it and let you know.

    #119032 quote
    kmilo20000
    Participant
    New

    Hi Roberto, I tried the code you suggested, with some changes for adapting it to what I need, however it stills not doing exactly wat I need. It does trail the first stop (thank you for that! I couldn’t even that work before), however the next trailings are not done.

    I need not only the stop to trail the initial ATR*3 when the profits increase, but to trail in different proportions.

    Basically, ATR*3 = 1R, so if profit>= 1R then the stop should trail to Tradeprice;  if profits >= 2R then the stop should trail to Tradeprice+2R*80%; if profits >= 3R then the stop should trail to Tradeprice+2R*75%, and so on…

    I tried for that pourpose these codes and neither of them work:

    // Condiciones para entrada de posiciones largas
    
    Defparam cumulateorders = false
    
    
    Timeframe (1 week)
    c1 = close[1]
    c2 = close[2]
    c3 = close[3]
    
    
    a1= open[1]
    a2= open[2]
    a3= open[3]
    
    
    MyADX18 = ADX[18]
    ADXperiods = 2
    
    MySMA24=average[24](c1)
    MySMA200= average[200](c1)
    
    Efficiency = call "Efficiency"
    
    Condition1 = c2>c3 and c1>c2
    
    Condition2= a2>a3 and a1>a2
    
    Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20
    
    Condition4 =  c1 > MySMA24
    
    Condition5 = c1 > MySMA200
    
    Condition6 = Efficiency>0.7
    
    
    Timeframe (default)
    
    IF Not OnMarket THEN
    MyProfit   = 0
    PrevProfit = 0
    ENDIF
    
    MyLongConditions  = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6
    
    IF MyLongConditions and not longonmarket THEN
    BUY 1 CONTRACTS AT MARKET
    Mystop = (averagetruerange[10](close[0]))*3
    SET STOP LOSS Mystop
    Endif
    
    IF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THEN
    
    SELL 1 CONTRACTS AT MARKET
    ENDIF
    
    //Stops
    
    Close0 = Tradeprice
    
    R3=0.8
    
    R4=0.75
    
    R5=0.45
    
    R6=0.45
    
    R7=0.4
    
    R8=0.3
    
    R9=0.25
    
    R10=0.25
    
    R11=0.25
    
    R12=0.12
    
    R13=0.10
    
    R14=0.10
    
    R15=0.09
    
    R16=0.09
    
    R17=0.09
    
    R18=0.08
    
    R19=0.08
    
    R20=0.08
    
    R21=0.08
    
    R22=0.05
    
    IF LongOnMarket THEN
    
    xProfit = close - Close0
    
    IF xProfit >= (MyProfit + Mystop) AND xProfit<(MyProfit + (Mystop*2)) THEN
    PrevProfit = MyProfit
    MyProfit = MyProfit + Mystop
    ENDIF
    IF MyProfit > 0 and Myprofit < Mystop*2 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*2 AND xProfit<Mystop*3 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R3
    ENDIF
    IF MyProfit => Mystop and Myprofit < Mystop*3 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*3 AND xProfit<Mystop*4 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R4
    ENDIF
    IF MyProfit => Mystop*2 and Myprofit < Mystop*4 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*4 AND xProfit<Mystop*5 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R5
    ENDIF
    IF MyProfit => Mystop*3 and Myprofit < Mystop*5 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*5 AND xProfit<Mystop*6 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R6
    ENDIF
    IF MyProfit => Mystop*4 and Myprofit < Mystop*6 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*6 AND xProfit<Mystop*7 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R7
    ENDIF
    IF MyProfit => Mystop*5 and Myprofit < Mystop*7 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*7 AND xProfit<Mystop*8 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R8
    ENDIF
    IF MyProfit => Mystop*6 and Myprofit < Mystop*8 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*8 AND xProfit<Mystop*9 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R9
    ENDIF
    IF MyProfit => Mystop*7 and Myprofit < Mystop*9 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*9 AND xProfit<Mystop*10 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R10
    ENDIF
    IF MyProfit => Mystop*8 and Myprofit < Mystop*10 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*10 AND xProfit<Mystop*11 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R11
    ENDIF
    IF MyProfit => Mystop*9 and Myprofit < Mystop*11 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*11 AND xProfit<Mystop*12 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R12
    ENDIF
    IF MyProfit => Mystop*10 and Myprofit < Mystop*12 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*12 AND xProfit<Mystop*13 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R13
    ENDIF
    IF MyProfit => Mystop*11 and Myprofit < Mystop*13 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*13 AND xProfit<Mystop*14 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R14
    ENDIF
    IF MyProfit => Mystop*12 and Myprofit < Mystop*14 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*14 AND xProfit<Mystop*15 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R15
    ENDIF
    IF MyProfit => Mystop*13 and Myprofit < Mystop*15 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*15 AND xProfit<Mystop*16 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R16
    ENDIF
    IF MyProfit => Mystop*14 and Myprofit < Mystop*16 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*16 AND xProfit<Mystop*17 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R17
    ENDIF
    IF MyProfit => Mystop*15 and Myprofit < Mystop*17 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*17 AND xProfit<Mystop*18 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R18
    ENDIF
    IF MyProfit => Mystop*16 and Myprofit < Mystop*18 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*18 AND xProfit<Mystop*19 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R19
    ENDIF
    IF MyProfit => Mystop*17 and Myprofit < Mystop*19 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*19 AND xProfit<Mystop*20 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R20
    ENDIF
    IF MyProfit => Mystop*18 and Myprofit < Mystop*20 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*20 AND xProfit<Mystop*21 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R21
    ENDIF
    IF MyProfit => Mystop*19 and Myprofit < Mystop*21 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*21 AND xProfit<Mystop*22 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R22
    ENDIF
    IF MyProfit => Mystop*20 and Myprofit < Mystop*22 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    IF xProfit >= Mystop*22 THEN
    PrevProfit = MyProfit
    MyProfit = xProfit*R22
    ENDIF
    IF MyProfit => Mystop*21 THEN
    SELL AT Close0 + PrevProfit STOP
    ENDIF
    
    Endif
    

    And this one:

    Defparam cumulateorders = false
    
    
    Timeframe (1 week)
    c1 = close[1]
    c2 = close[2]
    c3 = close[3]
    
    
    a1= open[1]
    a2= open[2]
    a3= open[3]
    
    
    MyADX18 = ADX[18]
    ADXperiods = 2
    
    MySMA24=average[24](c1)
    MySMA200= average[200](c1)
    
    Efficiency = call "Efficiency"
    
    Condition1 = c2>c3 and c1>c2
    
    Condition2= a2>a3 and a1>a2
    
    Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20
    
    Condition4 =  c1 > MySMA24
    
    Condition5 = c1 > MySMA200
    
    Condition6 = Efficiency>0.7
    
    
    Timeframe (default)
    
    IF Not OnMarket THEN
    MyProfit   = 0
    PrevProfit = 0
    ENDIF
    
    MyLongConditions  = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6
    
    IF MyLongConditions and not longonmarket THEN
    BUY 1 CONTRACTS AT MARKET
    Mystop = (averagetruerange[10](close[0]))*3
    SET STOP LOSS Mystop
    Endif
    
    IF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THEN
    
    SELL 1 CONTRACTS AT MARKET
    ENDIF
    
    //Stops
    
    R3=0.8
    
    R4=0.75
    
    R5=0.45
    
    R6=0.45
    
    R7=0.4
    
    R8=0.3
    
    R9=0.25
    
    R10=0.25
    
    R11=0.25
    
    R12=0.12
    
    R13=0.10
    
    R14=0.10
    
    R15=0.09
    
    R16=0.09
    
    R17=0.09
    
    R18=0.08
    
    R19=0.08
    
    R20=0.08
    
    R21=0.08
    
    R22 = 0.05
    
    IF LongOnMarket THEN
    
    xProfit = close - TradePrice
    
    IF LongOnMarket THEN
    xProfit = close - TradePrice
    IF xProfit >= (MyProfit + Mystop) THEN
    PrevProfit = MyProfit
    MyProfit   = MyProfit + Mystop
    ENDIF
    IF MyProfit > 0 AND MyProfit<Mystop*2 THEN
    SELL AT TradePrice + PrevProfit STOP
    
    ELSIF MyProfit >= Mystop*2 AND MyProfit<Mystop*3 THEN
    SELL AT TradePrice + PrevProfit*R3 STOP
    
    ELSIF MyProfit >= Mystop*3 AND MyProfit<Mystop*4 THEN
    SELL AT TradePrice + PrevProfit*R4 STOP
    
    ELSIF MyProfit >= Mystop*4 AND MyProfit<Mystop*5 THEN
    SELL AT TradePrice + PrevProfit*R5 STOP
    
    ELSIF MyProfit >= Mystop*5 AND MyProfit<Mystop*6 THEN
    SELL AT TradePrice + PrevProfit*R6 STOP
    
    ELSIF MyProfit >= Mystop*6 AND MyProfit<Mystop*7 THEN
    SELL AT TradePrice + PrevProfit*R7 STOP
    
    ELSIF MyProfit >= Mystop*7 AND MyProfit<Mystop*8 THEN
    SELL AT TradePrice + PrevProfit*R8 STOP
    
    ELSIF MyProfit >= Mystop*8 AND MyProfit<Mystop*9 THEN
    SELL AT TradePrice + PrevProfit*R9 STOP
    
    ELSIF MyProfit >= Mystop*9 AND MyProfit<Mystop*10 THEN
    SELL AT TradePrice + PrevProfit*R10 STOP
    
    ELSIF MyProfit >= Mystop*10 AND MyProfit<Mystop*11 THEN
    SELL AT TradePrice + PrevProfit*R11 STOP
    
    ELSIF MyProfit >= Mystop*11 AND MyProfit<Mystop*12 THEN
    SELL AT TradePrice + PrevProfit*R12 STOP
    
    ELSIF MyProfit >= Mystop*12 AND MyProfit<Mystop*13 THEN
    SELL AT TradePrice + PrevProfit*R13 STOP
    
    ELSIF MyProfit >= Mystop*13 AND MyProfit<Mystop*14 THEN
    SELL AT TradePrice + PrevProfit*R14 STOP
    
    ELSIF MyProfit >= Mystop*14 AND MyProfit<Mystop*15 THEN
    SELL AT TradePrice + PrevProfit*R15 STOP
    
    ELSIF MyProfit >= Mystop*15 AND MyProfit<Mystop*16 THEN
    SELL AT TradePrice + PrevProfit*R16 STOP
    
    ELSIF MyProfit >= Mystop*16 AND MyProfit<Mystop*17 THEN
    SELL AT TradePrice + PrevProfit*R17 STOP
    
    ELSIF MyProfit >= Mystop*17 AND MyProfit<Mystop*18 THEN
    SELL AT TradePrice + PrevProfit*R18 STOP
    
    ELSIF MyProfit >= Mystop*18 AND MyProfit<Mystop*19 THEN
    SELL AT TradePrice + PrevProfit*R19 STOP
    
    ELSIF MyProfit >= Mystop*19 AND MyProfit<Mystop*20 THEN
    SELL AT TradePrice + PrevProfit*R20 STOP
    
    ELSIF MyProfit >= Mystop*20 AND MyProfit<Mystop*21 THEN
    SELL AT TradePrice + PrevProfit*R21 STOP
    
    ELSIF MyProfit >= Mystop*21 THEN
    SELL AT TradePrice + PrevProfit*R22 STOP
    ENDIF
    ENDIF
    ENDIF
    

     

    Do you have any inputs on how could I make the code work for what I atempt to do?

     

    Thank you!

    #119034 quote
    robertogozzi
    Moderator
    Master

    You don’t need all those lines!

    My example after moving to breakeven trails SL so that it increases each time leaving your profit at risk equal to your initial SL.

    So when your profit reaches your SL, it is set to breakeven, then when it increases of another SL (your profit is SL*2) your stop loss is set to SL*1 snd so on… so that your stop loss is always set to initial SL*(n-1)  where n is the number of multiple times of SL that your profit grew.

    Use GRAPH to monitor your profits and your stop loss.

    #119036 quote
    kmilo20000
    Participant
    New

    Right. But the SL is not suposed to trail equally. It should vary according to the increase of profit. Is there a posibility to do that?

    #119048 quote
    robertogozzi
    Moderator
    Master

    You can do (almost) anything, just tell us what you want to achieve.

    #119054 quote
    kmilo20000
    Participant
    New

    Thank you Roberto.

    I will try to explain what I am aming to achieve:

    I need to trail a stop which starts (as yo know) at ATR*3. You actually posted a code that helps me trail that stop every time the price goes up the equivalent of the ATR*3. However, that is nos exactly what I need.

     

    What I need is to trail the stop based on a pre-fixed percentage of the profits, everytime the price goes up a ATR*3 of the entry (I would call the ATR*3 of the entry “R”).

    Exactly would be like this:

     

    1. Price increases 1R, then I would risk a 100% of the profits (1R). Therefore, the stop should trail to a breakeven position (“Tradeprice”).
    2. Price increases 2R, then I would risk an 80% of the profits, meaning that the stop  should trail to a price = Tradeprice + 20% of the profits (20%*2R). Locking in 20% of the profits and letting the market take the other 80%.
    3. Price increases 3R, then I would risk a 75% of the profits, meaning that the stop  should trail to a price = Tradeprice + 25% of the profits (25%*3R). Locking in 25% of the profits and letting the market take the other 75%.
    4. Price increases 4R, then I would risk a 45% of the profits, meaning that the stop  should trail to a price = Tradeprice + 55% of the profits (55%*4R). Locking in 55% of the profits and letting the market take the other 45%.
    5. Price increases 5R, then I would risk a 45% of the profits, meaning that the stop  should trail to a price = Tradeprice + 55% of the profits (55%*5R). Locking in 55% of the profits and letting the market take the other 45%.
    6. Price increases 6R, then I would risk a 40% of the profits, meaning that the stop  should trail to a price = Tradeprice + 60% of the profits (60%*6R). Locking in 60% of the profits and letting the market take the other 40%.
    7. Price increases 7R, then I would risk a 30% of the profits, meaning that the stop  should trail to a price = Tradeprice + 70% of the profits (70%*7R). Locking in 70% of the profits and letting the market take the other 30%.

    That is namely what I need.

     

    I have been trying to code it for several days, but I have been strugling to do it. Would be amazing if you could help me!

     

    Thank you.

    #119070 quote
    robertogozzi
    Moderator
    Master

    Ok, now I understand why you need many IF’s.

    I won’t be able to take a look at it until Monday.

    #119071 quote
    kmilo20000
    Participant
    New

    Got it. I’ll be attentive to your thoughts.

    #119227 quote
    kmilo20000
    Participant
    New

    Hi Roberto! Were you able to have a look at it? I am still trying to figure out how… I have been working in the code all weekend and today and still nothing 🙁

    #119239 quote
    kmilo20000
    Participant
    New

    Well. Think I did it!

     

    Just for anyone that may be looking for something like it:

     

    // Condiciones para entrada de posiciones largas
    
    Defparam cumulateorders = false
    
    
    Timeframe (1 week)
    c1 = close[1]
    c2 = close[2]
    c3 = close[3]
    
    
    a1= open[1]
    a2= open[2]
    a3= open[3]
    
    
    MyADX18 = ADX[18]
    ADXperiods = 2
    
    MySMA24=average[24](c1)
    MySMA200= average[200](c1)
    
    Efficiency = call "Efficiency"
    
    Condition1 = c2>c3 and c1>c2
    
    Condition2= a2>a3 and a1>a2
    
    Condition3 = LOWEST[ADXperiods + 1](MyADX18) > 20
    
    Condition4 =  c1 > MySMA24
    
    Condition5 = c1 > MySMA200
    
    Condition6 = Efficiency>0.7
    
    
    Timeframe (default)
    
    
    
    MyLongConditions  = Condition1 and Condition2 and Condition3 and Condition4 and Condition5 and Condition6
    
    IF MyLongConditions and not longonmarket THEN
    BUY 1 CONTRACTS AT MARKET
    Mystop = averagetruerange[10](Close)*3
    SET STOP LOSS Mystop
    Endif
    
    IF LONGONMARKET AND (Not Condition4 and not Condition5) or (Not Condition3 and Not Condition4) or (Not Condition3 and Not Condition5) THEN
    
    SELL 1 CONTRACTS AT MARKET
    ENDIF
    
    //Stops
    
    Close0 = Tradeprice
    
    R3=0.2
     
    R4=0.25
     
    R5=0.55
     
    R6=0.55
     
    R7=0.6
     
    R8=0.7
     
    R9=0.75
     
    R10=0.75
     
    R11=0.75
     
    R12=0.88
     
    R13=0.9
     
    R14=0.9
     
    R15=0.91
     
    R16=0.91
     
    R17=0.91
     
    R18=0.92
     
    R19=0.92
     
    R20=0.92
     
    R21=0.92
     
    R22=0.95
     
    
    IF LongOnMarket THEN
    
    Xprofit = HIGHEST[barindex-tradeindex](Close) - close0
    
    If xProfit >= Mystop AND xProfit < Mystop*2 then
    Sell at Close0 Stop
    
    ELSIf xProfit >= Mystop*2 AND xProfit < Mystop*3 then
    Sell at Close0+(xProfit*R3) Stop
    
    ELSIf xProfit >= Mystop*3 AND xProfit < Mystop*4 then
    Sell at Close0+(xProfit*R4) Stop
    
    ELSIf xProfit >= Mystop*4 AND xProfit < Mystop*5 then
    Sell at Close0+(xProfit*R5) Stop
    
    ELSIf xProfit >= Mystop*5 AND xProfit < Mystop*6 then
    Sell at Close0+(xProfit*R6) Stop
    
    ELSIf xProfit >= Mystop*6 AND xProfit < Mystop*7 then
    Sell at Close0+(xProfit*R7) Stop
    
    ELSIf xProfit >= Mystop*7 AND xProfit < Mystop*8 then
    Sell at Close0+(xProfit*R8) Stop
    
    ELSIf xProfit >= Mystop*8 AND xProfit < Mystop*9 then
    Sell at Close0+(xProfit*R9) Stop
    
    ELSIf xProfit >= Mystop*9 AND xProfit < Mystop*10 then
    Sell at Close0+(xProfit*R10) Stop
    
    ELSIf xProfit >= Mystop*10 AND xProfit < Mystop*11 then
    Sell at Close0+(xProfit*R11) Stop
    
    ELSIf xProfit >= Mystop*11 AND xProfit < Mystop*12 then
    Sell at Close0+(xProfit*R12) Stop
    
    ELSIf xProfit >= Mystop*12 AND xProfit < Mystop*13 then
    Sell at Close0+(xProfit*R13) Stop
    
    ELSIf xProfit >= Mystop*13 AND xProfit < Mystop*14 then
    Sell at Close0+(xProfit*R14) Stop
    
    ELSIf xProfit >= Mystop*14 AND xProfit < Mystop*15 then
    Sell at Close0+(xProfit*R15) Stop
    
    ELSIf xProfit >= Mystop*15 AND xProfit < Mystop*16 then
    Sell at Close0+(xProfit*R16) Stop
    
    ELSIf xProfit >= Mystop*16 AND xProfit < Mystop*17 then
    Sell at Close0+(xProfit*R17) Stop
    
    ELSIf xProfit >= Mystop*17 AND xProfit < Mystop*18 then
    Sell at Close0+(xProfit*R18) Stop
    
    ELSIf xProfit >= Mystop*18 AND xProfit < Mystop*19 then
    Sell at Close0+(xProfit*R19) Stop
    
    ELSIf xProfit >= Mystop*19 AND xProfit < Mystop*20 then
    Sell at Close0+(xProfit*R20) Stop
    
    ELSIf xProfit >= Mystop*20 AND xProfit < Mystop*21 then
    Sell at Close0+(xProfit*R21) Stop
    
    ELSIf xProfit >= Mystop*21 then
    Sell at Close0+(xProfit*R22) Stop
    
    ENDIF
    
    
    
    Endif
    
    #119243 quote
    robertogozzi
    Moderator
    Master

    Great achievement!

    I had no time today to take a look at it.

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.

Special trailing stop – hope it is possible!


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kmilo20000 @kmilo20000 Participant
Summary

This topic contains 13 replies,
has 2 voices, and was last updated by robertogozzi
6 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/06/2020
Status: Active
Attachments: No files
Logo Logo
Loading...