Code for limited orders

Hello,

I am using the following code extracted from here to create a limited order ( AmiBroker Knowledge Base » Handling limit orders in the backtester):

BuySignal = cond1 AND cond2;
// buy on the next bar
Buy = Ref( BuySignal, -1);
BuyLimitPrice = Ref( Close, -1) - 0.5*ATR(10);;
// now we check if limit was hit
Buy = Buy AND L < BuyLimitPrice;
// if Open price is below the limit, then we use Open for entry
BuyPrice = Min( Open, BuyLimitPrice );

With this code, the buy signal and the trade are triggered in the same bar, which is not possible. Hoy can I have the trade the day after the signal without establishing delays (SetTradeDelays(1,1,1,1);)?

Thanks
Miguel

You should use Ref() to move signals. Ref( Array, -1 ) would give you one bar delay

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Is this not equivalent to use settradedelays( 1, 1, 1, 1 )?
Thanks

SetTradeDelays adds delay to Buy/Sell/Short/Cover variables AFTER your entire formula is evaluated. You should delay signals AT START because logic given in KB refers to actual buy point (Buy AND L < BuyLimitPrice;) not delayed one.

In fact the code in the Knowledge Base is CORRECT and you should NOT CHANGE IT, because it ALREADY includes delay vs original condition.

BuySignal = Cross( Close, MA(Close, 100 ) );

// buy on the next bar
Buy = Ref( BuySignal, -1);  // THIS IS WHERE DELAY IS ALREADY APPLIED
BuyLimitPrice = Ref( Close, -1) * 0.99;

Simply don't change it. It is already correct and DO NOT add SetTradeDelays as it is REDUNDANT and causes double delay.

You need to use code exactly as it is shown in the KB without adding any extra delays

I already did it. I used it as it is, and it is wrong because the signal and the trade are on the same bar.
This is why I am asking for help.

What makes you believe the signal and the entry are occurring on the same bar? I suggest using an Exploration to confirm this.

It should be noted that the example code will work for a single symbol or for a portfolio test where you have sufficient capital to simultaneously enter trades for every symbol in the trading universe. However, if there is the possibility of generating more entry signals than you have capital to accommodate, then this code will generate unrealistic resilts unless you implement additional logic. You can find many posts in the forum by searching on "limit order", including an extensive discussion here: PositionScore / Ranking for trades taken “next day at limit” - #28 by hokchun

1 Like

THanks
yes I am using positionscore as this is a portfolio backtesting.
I just checked again with an exploration.
For a particular symbol, exploration shows condition triggered on the 20th, but scan and backtest show the trade on the 21th. So is this correct?
Thanks

That's good that you have confirmed that the signal and entry are not occurring on the same bar as you originally thought.

Only you can say whether your logic is "correct". If you do not have enough capital to potentially enter trades for every symbol in your universe (which is usually the case), then i strongly advise you to read the post i linked to until you understand everything written there. In my opinion, you cannot properly implement a portfolio backtest with realistic limit orders without using a CBT.

I have an old amibroker version, 6.2, so I cannot use CBT, right?
THanks

No, that is not correct.

Again, please use advice given here. Carefully read all recommendations and use them to understand what is actually happening, not what you think is happening.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

Hi
I did not had the time to read it, I will do it as soon as I can.
but what I have realized is that the limit orders are not working ok. Why? the trade prices are sometimes at exactly the low of the bar, so the strategy is too optimistic.
Why is that?
Thanks

The program obeys the formula rules. If you formula says to use low, it will use low. If limit is placed at given level at it just happens to be low and you are not really a bank or so, so you orders are relatively small compared to liquidity of given instrument, you can get such fill in reality, if you are big guy trading millions and If you want to add slippage - add it. It is up to you. The computers simply do whatever you tell them to do.

Any examples given in KB are just that - EXAMPLES - they are intended to show you some programming tips and that’s it. It is up to you to 1. Understand the code 2. Decide what you want 3. Adjust the code according to your needs and understanding of examples

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.