Signal from for statement is not working

Hello!

I have been working in the following example code for a daytrading system that buys on a limit order and sells on market at the end of the day, if stoploss is not triggered before.

// 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;

// Backtest

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

for(i = 0; i < BarCount; i++)
{
	if(Setup1[i]) 
		{
		InTrade = 10;
		BuySignal[i] = 1;
		LimitLevel = -8;
		StopLevel = -10;
		}
	if(Setup2[i])
		{
		InTrade = 20; 
		BuySignal[i] = 1;
		LimitLevel = -5;
		StopLevel = -12;
		}
			
}

BuyLimitPrice = Ref(Close, -1) * (1 + (LimitLevel/100));
StopPrice     = BuyLimitPrice * (1 + (StopLevel) / 100);
SellCase1     = Open <= StopPrice;
SellCase2     = Low  <= StopPrice;

for(i = 0; i < BarCount; i++)
{
	switch(InTrade)
	{
	case 10:
		if(SellCase1[i]) 
			{
			InTrade = 0;
			SellSignal[i] = 11;
			}
		if(SellCase2[i])
			{
			InTrade = 0;
			SellSignal[i] = 12;
			}
		else
			{
			InTrade = 0;
			SellSignal[i] = 10;
			}
		break;
	case 20:
		if(SellCase1[i]) 
			{
			InTrade = 0;
			SellSignal[i] = 21;
			}
		if(SellCase2[i])
			{
			InTrade = 0;
			SellSignal[i] = 22;
			}
		else
			{
			InTrade = 0;
			SellSignal[i] = 20;
			}
		break;
}}

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

Buy   = Buy AND Low < BuyLimitPrice;

BuyPrice   = Min(Open, BuyLimitPrice);
SellPrice  = IIf(Open < StopPrice, Open, IIf(Low < StopPrice, StopPrice, Close));

The system takes into consideration two possible patterns (Setup1 and Setup2). I would like to identify in the backtest report the trades that belong to each pattern and what has caused the sell order, as this:

  • Setup1, Open <= Stopprice: Code 11
  • Setup1, Low <= Stopprice: Code 12
  • Setup1, Market order at the EOD: Code 10.

Idem for Setup2: 21, 22, 20

I guess I have done something wrong with the for statement, because I cannot get any Sell signal when I backtest

Would you know what is the mistake? I cannot figure it out.

Thanks for your help.

Regards.

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