Issue with Sell Signal

Hi,

First time posting query here, I'm not sure how to search for similar issue here,

I have a simple buy and sell strategy, just that when sell signal generated, if it's a flat day, i.e, High == Low
then don't sell, check the next day.

Attached the afl and data,
As per the data, say Buy signal generated on Jan 04 2018, then the Sell signal to be triggered on Jan 5th 2018, Jan 5th was a flat day, so i want the position to be open and goto next day, Jan 8th, was not a flat day, then Sell signal to be triggered.


SetFormulaName("Sample System v0.1");

SetOption( "MinShares", 1 );
SetOption( "UsePrevBarEquityForPosSizing", True );
SetOption( "MaxOpenpositions", 10);
SetOption( "AllowSameBarExit", false );

SetPositionSize(10,spsPercentOfEquity);

Short = Cover = 0;

MinPrice = 5;

ROCTrend = ROC(C, 10);

NotFlat = !(Ref(H, -1) == Ref(L, -1) OR Ref(H, -2) == Ref(L, -2)  OR Ref(H, -3) == Ref(L, -3));

TodayNotFlat = !(H == L);

Buy = O > MinPrice AND NotFlat;

Buy = Buy AND TodayNotFlat;

BuyPrice = O;

SellPrice = O;

InTrade = False;

//Loop through all the bars
for(i=0; i< BarCount; i++){

	if(Buy[i] == 1)	{
		InTrade = True;
		continue;
	}
	else if(InTrade && H[i] != L[i])
	{
		Sell[i] = 1;
		InTrade = False;
	}
}

PositionScore = 100 - ROCTrend;

Data:

Ticker,Date/Time,Open,High,Low,Close,Volume
534748,04-01-2018,46.25,50.65,46.25,46.25,116414
534748,05-01-2018,43.95,43.95,43.95,43.95,17095
534748,08-01-2018,41.80,44.15,41.80,42.15,205410
534748,09-01-2018,40.80,43.20,40.05,41.80,340953
534748,10-01-2018,41.80,42.50,41.40,41.70,120428

When I back test this afl with the attached data, the result aren't consistent,

Result2

Result1

Not sure whats wrong, I did test this with the 6.28 beta and 6.20.1, the results are same!

Any help would be really appreciated

thanks

Something to try:
Instead of !(Ref(H, -1) == Ref(L, -1) OR [...])
Try Ref(H, -1) != Ref(L, -1) OR [...]

Regardless of why your code is not producing consistent results, you have a bigger problem. Inside the for() loop (which should be replaced with array logic) you are setting your Sell signal based on the current bar's high and low, but your exit price is set to the open price. That means you're looking into the future with your exit.

Regarding your Buy and Sell logic, try something similar to this untested snippet instead:

isFlat = H == L;

BuyPrice = O;
Buy = O > MinPrice AND 
          BarsSince(isFlat) > 3;

// This is just to avoid your future leak. 
// If you want to exit at the open, you need different exit logic
SellPrice = C;    

// Generate Sell signal without looping 
Sell = !Buy AND !isFlat;
Sell = ExRem(Sell, Buy);

Adding some more data for calculating ROCTrend

Ticker,Date/Time,Open,High,Low,Close,Volume
534748,15-12-2017,40.55,40.55,40.55,40.55,13003
534748,18-12-2017,38.55,38.55,38.55,38.55,4226
534748,19-12-2017,36.65,36.65,36.65,36.65,5465
534748,20-12-2017,34.85,34.85,34.85,34.85,10814
534748,21-12-2017,33.15,33.15,33.15,33.15,5159
534748,22-12-2017,32.25,34.80,32.25,34.80,738889
534748,26-12-2017,36.50,36.50,36.50,36.50,19958
534748,27-12-2017,38.30,38.30,38.30,38.30,28146
534748,28-12-2017,39.60,40.20,37.65,40.20,543957
534748,29-12-2017,42.20,42.20,40.30,42.15,754202
534748,01-01-2018,42.10,44.25,42.10,44.15,193289
534748,02-01-2018,45.90,46.35,44.25,46.35,91129
534748,03-01-2018,48.40,48.65,48.20,48.65,195689
534748,04-01-2018,46.25,50.65,46.25,46.25,116414
534748,05-01-2018,43.95,43.95,43.95,43.95,17095
534748,08-01-2018,41.80,44.15,41.80,42.15,205410
534748,09-01-2018,40.80,43.20,40.05,41.80,340953
534748,10-01-2018,41.80,42.50,41.40,41.70,120428

When I re-run the back test repeatedly, results weren't consistent!

Now sure if that's a bug in amibroker or my afl!

thanks, they are similar block of code, I'm just checking for last 3 days weren't flat.

Good catch, i'm testing this to close on on open.

Sell = !Buy AND !isFlat;
Sell = ExRem(Sell, Buy);

thanks, I'll try above above logic and post the findings

Not likely to be a bug with AmiBroker. I would suggest that you add some Exploration code and see if you can find a pattern to the inconsistency. For example, it is only the first run after a restart of AmiBroker that produces different results?

I just noticed another flaw in your AFL: you never initialize the Sell array, and you only assign the array positions that you want to set to true. If you are continuing to use your looping code, you should initialize Sell to 0 before starting. However, I encourage you to use a non-looping solution similar to the one I gave you.