How to remove Buy and Short signals in excess

Hello there;
I've written the code illustrated below but it does'nt works as I'm expecting to do.
I need an exit when the highest low value from the first Buy signal retrace of an optimized value /and vice versa for short trades). It seems that the exit signal happens from the latest Buy signal instead of the very first Buy signal of my trade (I see this with the Exploration tool). Maybe I might use Exrem but I haven't understood how it works.
Here my code:

Buy = 0;
Sell = 0;
Short = 0;
Cover = 0;
PriceAtBuy = 0;
PriceAtShort = 0;

OnMarket = 0;

cs = 0;
bar = BarIndex();

//Versione Future
SetOption("FuturesMode",True);

PositionSize = MarginDeposit = 1;   //Acquista/vende sempre solo 1 contratto

TRL_Par = Param("TRL",150,100,250,50);															
TRL_Opt = Optimize("TRL",TRL_Par,100,250,50);

ValueDiffMACD_Par = Param("Diff MACD",50,50,100,10);
ValueDiffMACD_Opt = Optimize("Diff MACD",ValueDiffMACD_Par,50,100,10);

MACD_Rif = MACD(fast = 12, slow = 26);
MACDSIGN_Rif = Signal(12,26,9);

//======================= End Variables ==================================================================================================================

Flag_Long = Flip(Cross(MACD_Rif,MACDSIGN_Rif),Cross(MACDSIGN_Rif,MACD_Rif));

Buy = (MACD_Rif - MACDSIGN_Rif > ValueDiffMACD_Opt AND Flag_Long == 1);
PriceAtBuy = ValueWhen(Buy,C,1);


Flag_Short = Flip(Cross(MACDSIGN_Rif,MACD_Rif),Cross(MACD_Rif,MACDSIGN_Rif));

Short = (MACDSIGN_Rif - MACD_Rif > ValueDiffMACD_Opt AND Flag_Short == 1);

PriceAtShort = ValueWhen(Short,C,1);


//ApplyStop(0,2,SL_Opt,1);																					//Stop Loss

//Debug trap
//cs = IIf(bar > 1170,1,0);



//===========	EXIT 1   ===========

Trailing_Par_Long = HighestSince(Buy,L);

Sell = IIf(L < Trailing_Par_Long - TRL_Opt,True,False);							
SellPrice = (Trailing_Par_Long - TRL_Opt);

Trailing_Par_Short = LowestSince(Short,H);

Cover = IIf(H > Trailing_Par_Short + TRL_Opt,True,False);
CoverPrice = (Trailing_Par_Short + TRL_Opt);

//Monitoring section
BI = BarIndex();

Filter = 1;

AddColumn(BI,"BI",1.0);
AddColumn(Flag_Long,"Flag_Long",1.0);
AddColumn(Flag_Short,"Flag_Short",1.0);
AddColumn(Trailing_Par_Long,"Trailing_Par_L",1.0);
AddColumn(Buy,"Buy",1.0);
AddColumn(BuyPrice,"BuyPrice",1.2);
AddColumn(PriceAtBuy,"PriceAtBuy",1.2);
AddColumn(Short,"Short",1.0);
AddColumn(ShortPrice,"ShortPrice",1.2);
AddColumn(PriceAtShort,"PriceAtShort",1.2);
AddColumn(Cover,"Cover",1.0);
AddColumn(CoverPrice,"CoverPrice",1.2);
AddColumn(Sell,"Sell",1.0);
AddColumn(SellPrice,"SellPrice",1.2);
AddColumn(MACD_Rif,"MACD(12)",1.4);
AddColumn(MACDSIGN_Rif,"MACD(12)Sign",1.4);

Thanks in advance to everybody.

I think ExRem should work fine with your code
it is easy to use.
Basically, it asks the program to remove any excess signal until something happens.
So, If you want to remove buy signal until sell of that position occur the code will be like this:

Buy = ExRem(Buy,Sell);

When you are using the applystop, this will not work and you have to figure out another condition to help remove the excess signals. I usually use something simple like checking that buy signal didn't exist in the last few bars. so if we assumed that my buy signal is defined as BuyCond the code will look like this:

RemoveJunk = Sum(BuyCond,3)==0;
Buy = ExRem(Buy,RemoveJunk);

So, if there is a buy signal in the last 3 bars, the sum will be more than 0 and the extra buy signal is going to be removed.

There is a more complicated way by using looping, but this method works fine for me.

2 Likes

hi @RockerGab, How are you?

i am having a similar problem to you with regards to using trailing stop loss only based on a first buy signal. any luck with the solution? could you guide me if you do have one?

thanks in advance.
Gautam

Backtester removes excess signals internally as described in the manual:
Portfolio-level back testing (scroll down to "Backtest modes" and look at the picture), so you don't need really need to remove them "by hand" at all.

See also this post that explains why fundamental differences between Exploration and backtest:

Scan vs Exploration vs Backtest - summary of differences

@Tomasz,

thanks once again for your help. i was sure about the backtest mode "regular" removing redundant extra signals but as you pointed out it could be an exploration issue. good summary on Scan vs Exploration vs Backtest - summary of differences think i have not come across this page earlier.

1 Like

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