BuyPrice on gap up

If I have a buy setup as in code below, and price gaps up the next day, the entry price is incorrect.

If it gaps up over the buy price, entry should be at the open.
But entry is occurring at the low of the day.

Can anyone explain what is happening and help me get it right?


SetTradeDelays( 0, 0, 0, 0 );  

BuySig = xyz;

Buy = Ref(BuySig, -1)               // setup occurs yesterday
      AND High > Ref(H, -1 )       // high breaks above stop trigger today
BuyPrice = Ref(H, -1 );

You just need to do something like this instead:

BuyPrice = max(Ref(H,-1, O);

The reason you're seeing entry at the low is because unless you specify otherwise, AmiBroker will try to confine your entry and exit prices to the actual high and low for the day. If yesterday's high is lower than today's low, then AmiBroker will "fix" your entry price to be today's low.

It should also be noted that if you are running against a portfolio of symbols then this backtest will generate overly optimistic results. You have essentially "cheated" by assuming that you can place an unlimited number of orders. For a full discussion of this topic, see this post: PositionScore / Ranking for trades taken “next day at limit”

Also, solutions involving ranking with StaticVarGenerateRanks will only partially solve the problem, as that still allows you to place more "orders" than you actually have capital for, i.e. they assume the use of an account with margin.

1 Like

Thanks very much for the assistance.

Unfortunately, this code doesn't produce the correct result either.

The entry price is supposed to be the high of the previous bar.

But I am getting entries at the high of the entry bar when it never reached the high of the previous bar.

IOW, I am getting entries when the entry was not triggered.

If I add the second line form my original:

AND High > Ref(H, -1 )

-- I'm back to entry at the low.

Sorry, I see that I missed a parenthesis on my previous post. The BuyPrice should be:

BuyPrice = max(Ref(H,-1), O);

This does not change your entry signal logic, so you shouldn't be getting any entries that you weren't getting before. It simply corrects the price at which the entry occurs. If you're still having problems, please post your updated code.

1 Like

I did pick up on the missing parenthesis, but I must have done something else wrong. It is working correctly now.

Thanks again for your help.