Help with Amibroker Coding

#1
I was trying to code a simple system in AFL, but the resulting trades don't conform with the code I have written.

Requirement -
System buys on Close of a day, if that day is a second consecutive down day. i.e. Go Long at the Close of every 2nd Consecutive down day. (though not on 3rd, 4th or any other kind of down day)

The system keeps a money management stop of 1.5%. If the stop is not hit on the immediate next day of Buying, then the Long position should be exited at the Close of next day.

Code written -
Buy=C<Ref(C,-1) AND Ref(C,-1)<Ref(C,-2);
BuyPrice=C;
ApplyStop(stopTypeLoss,stopModePercent,1.5,True);
ApplyStop(stopTypeNBar,stopModeBars,1);
// also tried - Sell=IIf(BarsSince(Buy=1)>0,1,0); - instead of this NBar stop.
shape=Buy*shapeUpArrow + Sell*shapeDownArrow;

Error -
When this code was run, it buys everyday irrespective of whether that day's close was or wasn't less than the previous day's close. Also, the Money Management stop gets hit on the same day as the purchase was made. (i.e. When the low of a particular day happens to be < 1.5% of the closing price of that day, the system buys at the closing price, and exits at the low price of the very same day, which is obviously a coding error)

I tried coding it through AFL Wizard, but got the same code for the Buy condition at least, which again yielded trade on every single day.

Another problem is that I want the system to go long only on the 2nd Down day, and not on the third, fourth or fifth down day, if they occur. (Because even for a fourth down day, the condition of its Close being less than Previous close, and that previous close being less than the close of the day before will be true)
 

Similar threads