set stop loss in profit region when in profit

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #162496 quote
    kevin12345
    Participant
    Average

    hi,

    anyone know how to code set stop loss in profit region when in profit?

    thank you

    #162498 quote
    robertogozzi
    Moderator
    Master

    You need to use pending a STOP order.

    Example to set SL at breakeven + 10 pips, after profits have reached 50 pips:

    If not OnMarket then
       StopLoss = 0
    Endif
    If StopLoss = 0 and OnMarket then
       Profit = PositionPerf * PositionPrice
       If Profit > 50 * pipsize then
          If LongOnMarket then
             StopLoss = PositionPrice + 10 * pipsize
          Else
             StopLoss = PositionPrice - 10 * pipsize
          Endif
       Endif
    Endif
    If StopLoss > 0 then
       Sell      at StopLoss STOP
       Exitshort at StopLoss STOP
    Endif

    (not tested)

    icharttop thanked this post
    #162505 quote
    kevin12345
    Participant
    Average

    can someone explain why I exit there?

    IF  c1  and uptrend and not onmarket THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    
    If OnMarket then
    totalProfit = PositionPerf * PositionPrice
    if totalprofit > 10*pipsize then
    stoploss = positionprice + 5*pipsize
    sell at stoploss stop
    exitshort at stoploss stop
    Endif
    Endif
    
    If longonmarket and  totalProfit > 10 * pipsize then
    if habearcandle5mn then
    Sell at market
    Endif
    endif

    I’m not suppose to have a stop loss in the loss region

     

    thanks

    icharttop thanked this post
    3.png 3.png
    #162507 quote
    kevin12345
    Participant
    Average

    sorry problem solved. the reason why it stopped because the backtest period ended where it stopped.

    #162514 quote
    robertogozzi
    Moderator
    Master

    Be warned that pending orders expire each bar and you have to place them again and again, if needed.

    Lines 9-10 will only be executed the first time.

    You should place them outside that check (like my example), otherwise you SL will placed correctly when 10 pips are reached but, if not hit and the next bar profit decreses to 6 pips, it won’t be placed at all!

    #162645 quote
    kevin12345
    Participant
    Average

    hi, i want to put stoploss at 0 when profit is 5 pips above the positionprice and move to 5 pips in profit when it is 10pips above the positionprice. can you check my codes if i have done that? thank you

    If longOnMarket then
    totalProfit = PositionPerf * PositionPrice
    if totalprofit > 5*pipsize then
    stoploss = 0
    endif
    if totalprofit > 10*pipsize then
    stoploss = positionprice + 5*pipsize
    Endif
    Endif
    
    if stoploss=0 then
    sell at stoploss stop
    endif
    
    if stoploss > 0 then
    sell at stoploss stop
    endif
    #162647 quote
    kevin12345
    Participant
    Average
    if not onmarket then
    stoploss = 0
    endif 
    
    If longOnMarket then
    totalProfit = PositionPerf * PositionPrice
    if totalprofit > 10*pipsize then
    stoploss = positionprice + 5*pipsize
    Endif
    Endif
    
    if longonmarket  and close <10*pipsize then
    sell at stoploss stop
    endif
    
    If longonmarket and  totalProfit > 10 * pipsize then
    if habearcandle5mn then
    Sell at market
    Endif
    endif
    
    set target pprofit 100
    graph stoploss
    
    6.png 6.png
    #162649 quote
    kevin12345
    Participant
    Average

    does not seem to trigger the stop loss i have on 5 pips above position price.

    #162652 quote
    kevin12345
    Participant
    Average

    solved it

    #162659 quote
    GraHal
    Participant
    Master

    Tell us then please … stories without endings are not good for sleeping!? 🙂

    #162704 quote
    kevin12345
    Participant
    Average

    change line 12 to

    if longonmarket  and ((close - positionprice )< 10*pipsize) then
    #162705 quote
    kevin12345
    Participant
    Average

    but for going short it does not work. it opens and closes immediately and the stoploss variable is always zero.

    if not onmarket then
    stoploss = 0
    endif
    If shortOnMarket then
    totalProfit = PositionPerf * PositionPrice
    if totalprofit > 10*pipsize then
    stoploss = positionprice - 5*pipsize
    Endif
    Endif
    
    if shortonmarket  and (( positionprice - close )< 10*pipsize) then
    exitshort at stoploss stop
    endif
    
    If shortonmarket and  totalProfit > 10 * pipsize then
    if habullcandle5mn then
    exitshort at market
    Endif
    endif

    anyone got any ideas?

    icharttop thanked this post
    #162706 quote
    kevin12345
    Participant
    Average

    seems like it worked when i changed line 2 to stoploss=2 because i have spread =2 when i backtested

    SkippyTrader thanked this post
    #162707 quote
    Vonasi
    Moderator
    Master

    Obviously when going short prices and stop losses are reversed. So the calculations in lines 7 and 11 need to be reversed.

    stoploss = positionprice + 5*pipsize
    
    
    if shortonmarket  and (( close - positionprice)< 10*pipsize) then
    icharttop and SkippyTrader thanked this post
    #179088 quote
    SkippyTrader
    Participant
    New

    Thanks – very useful to code this to minimise losing any initial capital.

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

set stop loss in profit region when in profit


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
kevin12345 @kevin12345 Participant
Summary

This topic contains 15 replies,
has 6 voices, and was last updated by murre87
4 years, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 02/24/2021
Status: Active
Attachments: 2 files
Logo Logo
Loading...