Sell signal is not registered by forcing it to sell earlier

I am developing a system where the average length of trades is about 5 or 6 days, and after examining the results, I found those trades that last longer than 5 or 6 days will be usually sold at a loss, so I would like to force those longer trades to exit at the Open of the 5th bar, ie.when barssince(buy) == 4. However it doesn't work, many Sell signals that are forced to be on the 5th bar don't get registered (with a few exceptions). And those Sell signals that are not forced earlier have no issues.

See the exploration of ALU where the Sell signal is not registered when forced to exit on 2020 Feb 27 instead of 2020 Mar 2 (when isSellTooLate is true), and this represents the majority of the case. However, there are a handful of exceptions see the result of ALQ. I am wondering why? See code excerpt and some results from running "Explore".


MaxNumBars = 4; //to set max length of trade by bars. 

....................
....................

Sell0 = EXITTRIGGER; //the temporary sell signal according to the original rules. 

Buy = ExRem(Buy,Sell0);
Sell0 = ExRem(Sell0,Buy);

SellPrice0 = ValueWhen(Sell0, IIf(O >= LIMITSELLPRICE,O,LIMITSELLPRICE)) ;  
OnTheMaxBar = BarsSince(Buy)==MaxNumBars;
OpenPriceOfTheMaxBar = ValueWhen(OnTheMaxBar,O);
  
isSellTooLate = ValueWhen(Sell0, BarsSince(Buy) >= MaxNumBars);
Sell = IIf(isSellTooLate,OnTheMaxBar,sell0); //not sure why sell signal is not registered when before maxnumbars
SellPrice = IIf(isSellTooLate, OpenPriceOfTheMaxBar, SellPrice0); 

LongInitialStop = ValueWhen(Buy,IIf(buyprice - Ref(6 * ATR(10),-1) <= 0, 0.01, BuyPrice - Ref(6 * ATR(10),-1)));
stopAmount=BuyPrice-LongInitialStop;
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, 1);


//the following part is for exploration 
Filter=Buy OR Sell OR Sell0 OR OnTheMaxBar; 

AddColumn(IIf(Buy,1,null),"Buy");
AddColumn(IIf(Sell,1,Null), "Sell");
AddColumn(IIf(Sell0,1,Null), "Sell0");
AddColumn(IIf(OntheMaxBar,1,Null), "OnTheMaxBar");

AddColumn(IIf(Buy,Ref(LimitBuyPrice,-1),Null),"Limit Buy Price");
AddColumn(IIf(Buy,BuyPrice,Null),"Buy Price");
AddColumn(IIF(Sell0,LimitSellprice,Null),"Limit Sell Price");
AddColumn(IIf(Sell0,SellPrice0,Null),"Sell0 Price");
AddColumn(IIf(Sell0,BarsSince(Buy),Null), "Bars Since Buy");
AddColumn(IIf(Sell0 AND isSellTooLate, 1,Null), "sold too late");
AddColumn(OpenPriceOfTheMaxBar, "Open Price of Max Day");
AddColumn(SellPrice,"Final sold price");
SetSortColumns(1,2);


@riverculture, without going into the merits of your code, I wonder why you don't use the stopTypeNBar modality of applyStop()?
https://www.amibroker.com/guide/afl/applystop.html

2 Likes

Thank you for your suggestion.

If I use the NBarStop, Could you let me know how I can set the stop price for exit at Open if the stop is triggered? (Note: if the NBarStop is not triggered, then I have a different exit strategy --- not exit at Open)

finally I figured out the reason why the Sell signal is not registered for ALU.au while ALQ.au was registered, I failed to see AFL's array processing inner-working when I code the isSellTooLate variable, usually when isSellTooLate is true, it's already passed the 5th bar, so this condition is not actually able to control the previous bars. While ALQ.au happens to have isSellTooLate happen earlier than my date range, and it gets carried down to another trade.

While I got the issue figured out, I am still very interested in knowing how to set the stop price to be Open when StopTypeNBar is triggered.

Thank you

@riverculture, I suggest to examine again the ApplyStop() function documentation.

It describes 4 scenarios that address different trade delays and trade prices.

Although the function offers some advanced features, I usually prefer to use a loop where I can apply additional logic to manage all types of stops.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.