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);
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);
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.