How to identify the signal that triggers entry and the sellprice case

Hello,

I have used the following sites to code an example system for daytrading. It is supposed to enter on a limit order according to some conditions to be met on the previous day and exit on the same day at a price that depends on open, low, stop and close levels. Apparently it is working fine.

AmiBroker Knowledge Base » How to identify signal that triggered entry/exit if multiple signals are used
AmiBroker Knowledge Base » Handling limit orders in the backtester

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

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

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

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

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 next step would be identifying not only the entry signal but also the sell case. Since there are no sell rules, by sell case I mean: 0) Bearish open gap (Open < Stopprice) 1) Stop triggered (Low < Stopprice) or 2) Close at the end of the day.

SellCase  = IIf(Open < StopPrice, 0, IIf(Low < StopPrice, 1, 2));

I have been trying to integrate this condition into the loop without getting any possitive result.

for(i = 0; i < BarCount; i++)
{
	switch(InTrade)
	{
	case 0:
		if(Setup1[i]) 
			{
			InTrade = 10;
			BuySignal[i] = 1;
			LimitLevel = -8;
			StopLevel = -10;
			}
		if(Setup2[i])
			{
			InTrade = 20; 
			BuySignal[i] = 1;
			LimitLevel = -5;
			StopLevel = -12;
			}
		break;
	BuyLimitPrice[i] = Ref(Close[i], -1) * (1 + (LimitLevel/100));
	StopPrice [i] = BuyLimitPrice[i] * (1 + (StopLevel) / 100);
	SellCase = IIf(Open[i] < StopPrice[i], 0, IIf(Low[i] < StopPrice[i], 1, 2));
	
	case 10:
		SellSignal[i] = InTrade + SellCase;
		InTrade = 0;
		break; 
		
	case 20:
		SellSignal[i] = InTrade + SellCase;
		InTrade = 0;
		break; 
	}
}

My intention was getting signals such as the following ones:

  • Long (10) - Setup 1 + Sellprice = Open
  • Long (11) - Setup 1 + Sellprice = Stopprice
  • Long (12) - Setup 1 + Sellprice = Close
  • Etcetera

Could you give me some guidance to keep working on it?. I have tried differente variations, but I was unable to find the correct one.

Thank you.

Regards.