Channel breakout system can't move stoploss level

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #32699 quote
    TraderC
    Participant
    New

    I have a problem with my code.

    My code is a channel breakout system with pyramiding.

    My channel breakout system for the short side is:

    When the price breaks through 20-day low, I sellshort 1 position. (Pos #1).

    My stoploss is the low of the previous day (SL #1).

     

    My rules for pyramiding (Position #2, #3, etc.) are :

    Condition: The 20-day low has to be flat or rising for at least five days.

    As soon as I enter the second position (breakout of 20-day low) (Pos. #2), the stoploss of Pos #2 is the high of the previous day. I also move the stoploss of the first position (SL #1) to the high of one day before my second position entry day =previous day. (SL #1 à SL #2).

    Now I have 2 positions (Pos. #1, Pos.#2) with the same stoploss (SL #2).

     

    I have two more rules to filter out false breakouts after pyramiding.

    Rule1: On the breakout day (=first day) the close must be lower than 20-day low of the day before the breakout day.

    Rule2: One day after the breakout day (=second day) the close also must be lower than the 20-low of the day before the breakout day.

    If rule1 OR rule2 are broken I exit all positions at the close of the day.

    This prevents false breakout signals.

    If first day and second day both close lower, then I’ll let the positions run until

    • There is another condition for pyramiding.
      OR
    • The market reverses and my stoploss (SL #2) is hit.
      OR
    • The 20-day high becomes lower than my (previous day low)stoploss (20-day high < SL #2)- in which case I adjust my stoploss to the 20-day high (SL #2 change toà 20-day high) and it is hit.
    period=20
    
    L20=lowest[period](low)
    fivel20 = L20>=L20[1] and L20>=L20[2] and L20>=L20[3] and L20>=L20[4]
    
    H20=highest[20](high)
    
    If countOfposition>0 then
    
    firststop=newstop
    
    Endif
    
    // no position
    
    If not shortonmarket then
    
    sellshort 1 shares at L20 stop
    
    firststop= high[0]
    
    if shortonmarket then
    
    exitshort at firststop stop
    
    endif
    
    else
    
    
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=0 then
    
    exitshort at market
    
    else
    
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=1 then
    
    exitshort at market
    
    else
    
    if firststop-newstop>0 then
    
    exitshort at firststop stop //Here is a problem //
    
    Endif
    
    Endif
    
    
    endif
    
    endif
    
    //when you have at least one position
    
    newstop = high[0]
    
    If shortonmarket then
    
    exitshort at firststop stop
    
    endif
    
    If shortonmarket and fivel20 then
    
    sellshort 1 shares at L20 stop
    
    newstop=high[0]
    
    exitshort at min(newstop,H20) stop   //there is a problem
    
    if high[0] - H20 < 0 and countofposition >= 1 then
    
    newstop= high[0]
    
    Endif
    
    //exitshort at new stop //
    
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=0 then
    
    exitshort at market
    
    else
    
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=1 then
    
    exitshort at market
    
    else
    
    if shortonmarket  then  //must add condition
    
    exitshort at min(high[0],H20) stop
    
    endif
    
    Endif
    
    Endif
    
    endif
                                                                                                                                                        
    

     

     

    Here is my code.

    It’s not working as my rules.

    I can’t move my stoploss level lower.

    Please help me to fix my code.

    Thank you very much.

    #34107 quote
    TraderC
    Participant
    New

    Could anyone help me?

    Please.

    Thank you very much.

    #34165 quote
    Nicolas
    Keymaster
    Master

    Sure, but I must say that your code is difficult to read because of all these blank lines. Could you please repost it without the blank lines please? You can separate every IF/THEN block of course. Many thanks 🙂

    #34194 quote
    TraderC
    Participant
    New

    Thank you Nicolas.

    I hope it is better now. 🙂

    I really look forward to your answer.

    I already struggle for a long with this code. Puuhhh

    period=20
    L20=lowest[period](low)
    fivel20 = L20>=L20[1] and L20>=L20[2] and L20>=L20[3] and L20>=L20[4]
    H20=highest[20](high)
    
    If countOfposition>0 then
    firststop=newstop
    Endif
    // no position
    If not shortonmarket then
    sellshort 1 shares at L20 stop
    firststop= high[0]
    
    if shortonmarket then
    exitshort at firststop stop
    endif
    else
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=0 then
    exitshort at market
    else
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=1 then
    exitshort at market
    else
    if firststop-newstop>0 then
    exitshort at firststop stop //Here is a problem //
    Endif
    Endif
    endif
    endif
    
    //when you have at least one position
    newstop = high[0]
    
    If shortonmarket then
    exitshort at firststop stop
    endif
    
    If shortonmarket and fivel20 then
    sellshort 1 shares at L20 stop
    newstop=high[0]
    exitshort at min(newstop,H20) stop   //there is a problem
    
    if high[0] - H20 < 0 and countofposition >= 1 then
    newstop= high[0]
    Endif
    
    //exitshort at new stop //
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=0 then
    exitshort at market
    else
    if shortonmarket and close > tradeprice and Barindex-Tradeindex=1 then
    exitshort at market
    else
    if shortonmarket  then  //must add condition
    exitshort at min(high[0],H20) stop
    endif
    Endif
    Endif
    endif
    #34542 quote
    Nicolas
    Keymaster
    Master

    Thanks, I don’t understand the pyramiding function with “Condition: The 20-day low has to be flat or rising for at least five days.”.  So you are adding more sellshort orders if the price is rising above the first order? But in this case the first stoploss should have already been triggered? I think I should recode the whole thing to get a better comprehension of the strategy 🙂 Could you please add some screenshots of how the strategy should operate on a price chart please? Thanks in advance.

    #34646 quote
    TraderC
    Participant
    New

    Thank you, Nicolas!
    I try to explain clearer. This is short-side pyramidding system.
    My first SL#1 is HIGH of previous bar (sorry – I wrote “low” in my desciption above).
    When the 20-day low channel broken to downside, I sellshort 1 position, and my SL is HIGH of last day (=previous day).
    I sellshort 2nd position if (20-day channel is flat or rising for at least 5 days (5-day Cond.)) AND (price breaks 20-day channel to DOWNSIDE).
    The SL for 2nd position is High of previous bar. I also want to automatically move the SL of 1st position to the 2nd pos. SL. (This is my main issue…)
    When the 20-day High channel become < SL#1, I want to switch SL#1 to follow 20-day High channel. Until enter 2nd pos., then move SL to SL#2.
    Very Sorry.!!.. My explanation was not clear for you!!

    I wait eveyr day for Nicolas brilliant code!! 🙂 Thank you! Thank you!!
    I make screenshot with all detail below. I hope you understand.

    CB-Nicolas-2017-05-05-下午11.56.41.png CB-Nicolas-2017-05-05-下午11.56.41.png
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Channel breakout system can't move stoploss level


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
TraderC @li_ting_chen Participant
Summary

This topic contains 5 replies,
has 2 voices, and was last updated by TraderC
8 years, 10 months ago.

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