BuyPrice and Stop Orders

My doubt is regarding Stop orders and the price level.
Suppose I have a code like this:

SetTradeDelays(0, 0, 0, 0);
Buy = Ref(BuySig, -1) == 1;
BuyPrice = Ref(High, -1) + TickSize;

I assume that it will go Long when Buy is True and the BuyPrice falls somewhere in between High-Low range of the entry bar. But in my quick review I don't find it to be true. When I try the following modified code, I see that the entry prices are finally correct now:

SetTradeDelays(0, 0, 0, 0);
EntryPrice = Ref(High,-1) + TICKSIZE
Buy = Ref(BuySig, -1) == 1 AND Cross(High, EntryPrice);
BuyPrice = EntryPrice;

So does this mean that Amibroker doesn't check if BuyPrice is within the range and for stop orders, I have to specifically check if the High of the bar breaches the BuyPrice in the Buy condition?

Sorry for bothering @mradtke but can you confirm?

My question primarily is that when there is a Buy signal and I give a BuyPrice then the trade will ONLY happen if the price reaches the level of BuyPrice or more in the current bar. Is this implicitly taken care by AmiBroker?

So when writing code is it necessary that I add a check for Cross(High, BuyEntryPrice) in the Buy signal to confirm that a stop buy can happen?
Or is it implicit?

The Buy variable determines whether or not to enter a trade, while BuyPrice determines the price at which the trade is entered. If you do not explicitly disable AmiBroker's price bound checking, then AmiBroker will force the BuyPrice to be within the High-Low range of the entry bar.

In your example, you would need to add the Cross logic to the assignment of your Buy variable, as you have already discovered. Just keep in mind that when you do this as part of a portfolio test, you may be "placing orders" beyond your available capital because you are pre-selecting the trades that will actually meet the limit price rather than considering only the number of Setups that you can afford to have filled. See this thread (I suggest reading the entire thing) for more details: PositionScore / Ranking for trades taken “next day at limit”

2 Likes

Thanks for the reference. I will read it up.