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.