Composite for counting day trading setups

Hello,

I am trying to code two composites for the following day trading example system:

// Setup 1

Setup11 = Close > MA(Close, 200);
Setup12 = RSI(2) < 20;
Setup1  = Setup11 AND Setup12;

// Setup 2

Setup21 = Close < MA(Close, 200);
Setup22 = RSI(2) < 20;
Setup2  = Setup21 AND Setup22;

// Backtester

LimitLevel = 0;
InTrade    = 0;
BuySignal  = 0;
SellSignal = 0;

for(i = 0; i < BarCount; i++)
{
	switch(InTrade)
	{
	case 0:
		if(Setup1[i]) 
			InTrade = BuySignal[i] = 21;
		if(Setup2[i])
			InTrade = BuySignal[i] = 22;
		break;
		
	case 21:
		SellSignal[i] = 21;
		LimitLevel = -8;
		InTrade = 0;
		break; 

	case 22:
		SellSignal[i] = 22;
		LimitLevel    = -5;
		InTrade = 0; 
		break;
	}
}

BuyLimitPrice = Ref(Close, -1) * (1 + (LimitLevel/100));

Buy   = Ref(BuySignal, -1);
Sell  = SellSignal; 

Buy   = Buy AND Low < BuyLimitPrice;

BuyPrice   = Min(Open, BuyLimitPrice);
SellPrice  = Close; 

The first composite has to count the number of executed trades and apparently it works as intended.

AddToComposite(Buy, "~Example_Trades", "V", atcFlagDeleteValues + atcFlagEnableInBacktest);

image

The second composite has to count the number of setups, that is to say, all the limit orders sent to the broker the previous day, even if they were not executed because the price did not exceed the limit price.

AddToComposite(Ref(BuySignal, -1), "~Example_Setups", "V", atcFlagDeleteValues + atcFlagEnableInBacktest);

image

The last composite is not working as intended, it displays many more events than it should. For this test I have chosen a Nasdaq 100 watchlist. However, the composite registers more setups than symbols in the watchlist for most of the days.

I have tried different variations but I have been unable to figure out what is wrong with the code. Would you know what is causing this issue?

Regards.

Hello,

Finally I could figure it out. I have added the following line:

Setup   = Setup1 OR Setup2;

I use it for counting the daily setups with the composite:

AddToComposite(Ref(Setup, -1), "~Example_Setups", "V", atcFlagDeleteValues + atcFlagEnableInBacktest);

Now the code works as intended, the number displayed by the composite matches the quantity of setups from the exploration

image

image

This is the complete code:

// Setup 1

Setup11 = Close > MA(Close, 200);
Setup12 = RSI(2) < 20;
Setup1  = Setup11 AND Setup12;

// Setup 2

Setup21 = Close < MA(Close, 200);
Setup22 = RSI(2) < 20;
Setup2  = Setup21 AND Setup22;

Setup   = Setup1 OR Setup2;

// Backtester

LimitLevel = 0;
InTrade    = 0;
BuySignal  = 0;
SellSignal = 0;

for(i = 0; i < BarCount; i++)
{
	switch(InTrade)
	{
	case 0:
		if(Setup1[i]) 
			InTrade = 21;
			BuySignal[i] = 1;
		if(Setup2[i])
			InTrade = 22; 
			BuySignal[i] = 1;
		break;
		
	case 21:
		SellSignal[i] = 21;
		LimitLevel = -8;
		InTrade = 0;
		break; 

	case 22:
		SellSignal[i] = 22;
		LimitLevel    = -5;
		InTrade = 0; 
		break;
	}
}

BuyLimitPrice = Ref(Close, -1) * (1 + (LimitLevel/100));

Buy   = Ref(BuySignal, -1);
Sell  = SellSignal; 

Buy   = Buy AND Low < BuyLimitPrice;

BuyPrice   = Min(Open, BuyLimitPrice);
SellPrice  = Close; 

// Composites

AddToComposite(Buy, "~Example_Trades", "V", atcFlagDeleteValues + atcFlagEnableInBacktest);
AddToComposite(Ref(Setup, -1), "~Example_Setups", "V", atcFlagDeleteValues + atcFlagEnableInBacktest);

// Exploration

Filter = Ref(Setup, -1);

Thank you for your attention.

Best regards.