Adding second Traling Stop Loss condition causes countertrend trade. Why?

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #97004 quote
    SaschaSascha
    Participant
    Average

    Hi,

    So far I had one trailing stop condition which worked fine. Then I added a second condition which works partly.

    The short trade was exited after the second Trailing Stop condition was met. However, it created instantly a new long trade which should not happen.

    I guess something is not right with the order of the code.

    Can anyone help?

    Thanks.

    Sascha

    //if not order on market, reset the breakeven status
    if not onmarket then
    breakeven=0
    endif
     
    //check if the current order has made 2*ATR of signal day
    if shortonmarket and tradeprice-close >=2*ATR then
    breakeven=1
    endif
    
    //put stoploss at open price - 3 pip
    if breakeven=1 then
    buy at tradeprice-3*pointsize stop
    endif
    
    
    //------When adding this second set of Trailing Stop condition it causes a new long trade. Why?
    
    //if not order on market, reset the breakeven status
    if not onmarket then
    breakeven=0
    endif
    
    //check if the ATR>=39
    if shortonmarket and ADX[14]>=39 then
    breakeven=1
    endif
    
    //put stoploss at open price
    if breakeven=1 then
    buy at tradeprice-1*pointsize stop
    endif
    //------
    
    // Conditions to exit short positions
    ignored, indicator3 = CALL "PowerX 2"
    c6 = (indicator3 > -3)
    
    c7 = (close > SMA50)
    
    IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) and ONMARKET THEN
    EXITSHORT AT MARKET
    barorder=0
    ENDIF
    
    #97005 quote
    NicolasNicolas
    Keymaster
    Legend

    To close a short order, you must use EXITSHORT, not BUY instruction.

    #97015 quote
    SaschaSascha
    Participant
    Average

    Thanks Nicolas.

    However, I don’t just want to close the short position but I only want to adjust the stop loss to breakeven – 3 pip when CONDITION 1 is met and to breakeven – 1 pip when CONDITION 2 is met.

    The first part of the code (which contains CONDITION 1) you helped me to create and it works fine in ProBacktest.

    //if not order on market, reset the breakeven status
    if not onmarket then
    breakeven=0
    endif
     
    //  (CONDITION 1)
    //check if the current order has made 2*ATR of signal day 
    if shortonmarket and tradeprice-close >=2*ATR then
    breakeven=1
    endif
     
    //put stoploss at open price - 3 pip    
    if breakeven=1 then
    buy at tradeprice-3*pointsize stop
    endif
     
     
    // Conditions to exit short positions
    ignored, indicator3 = CALL "PowerX 2"
    c6 = (indicator3 > -3)
     
    c7 = (close > SMA50)
     
    IF c6 OR c7 and (barindex-barorder>=1 and barorder>0) and ONMARKET THEN
    EXITSHORT AT MARKET
    barorder=0
    ENDIF

    I then added the code for CONDITION 2 to it and was hoping it would work. But it now creates a new long position after it closed a trade that met CONDITION 2.

    I just want it to adjust the Stop loss.

    //   CONDITION 2
    
    //if not order on market, reset the breakeven status
    if not onmarket then
    breakeven=0
    endif
    
    //check if the ATR>=39
    if shortonmarket and ADX[14]>=39 then
    breakeven=1
    endif
    
    //put stoploss at open price
    if breakeven=1 then
    buy at tradeprice-1*pointsize stop
    endif
    #97020 quote
    SaschaSascha
    Participant
    Average

    I have to correct myself. I thought that the first part of the code (CONDITION 1) was working, but now when I checked again I realized it is not working.

    It produces the same mistake that after closing the short trade when the condition is met, it creates a new long trade.

    So I guess the command     buy at tradeprice-3*pointsize stop     doesn’t work and somehow creates this countertrend trade.

    I am confused now. How can I write the code that it  changes the Stop Loss to  entry price – 3 pips?

    Thanks so much for your help.

    #97022 quote
    SaschaSascha
    Participant
    Average

    Problem solved:

    I read through everything again and then replaced the BUY instruction with EXITSHORT and now it works.

    Somehow I assumed that EXITSHORT would exit the position at market (that was my confusion) but as I use STOP it adjusts the Stop Loss as I wanted.

    Thank you again Nicolas.

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

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

Adding second Traling Stop Loss condition causes countertrend trade. Why?


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Sascha @goedelsa Participant
Summary

This topic contains 4 replies,
has 2 voices, and was last updated by SaschaSascha
7 years, 5 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 04/23/2019
Status: Active
Attachments: No files
ProRealCode ProRealCode
Loading...