View Full Version : Proper Order Entry at Bar Close
AresBowman
06-23-2010, 03:12 PM
Hi,
I'm sure this question has been addressed but I couldn't seem to find anything helpful searching the forum.
I am using the following order entry line of code
entryOrder = EnterLongLimit(NumStocks, Close[0] + 1 * TickSize, entrySignal);
I have found that the order fills on the next bar regardless of whether or not the price range of the next bar encompasses the limit price. For example if the current bar closes at $10.00 the limit price would be set at $10.01. If the following bar (where the order would be placed) gaps down to $10.00 at open and the bar range never goes higher than $10.00, then the buy order still fills at $10.01 and then closes out at the appropriate profit target. The problem is that during back testing this situation causes an error which results in no trades being entered after the above situation occurs.
Any advice?
Thanks in advance ;)
NinjaTrader_RyanM
06-23-2010, 03:36 PM
Hello AresBowman,
The scenario you describe is what limit orders will do in a live environment and this is then simulated in a backtest. When you're placing a limit order, you're saying "I'd like to be filled at the specified price or better". If you place a buy limit order above where the market is currently trading, then your limit order is marketable and you will get a fill "better" than the specified price.
The backtesting engine will use the same principles to determine if there should be a fill. The fill price is then determined by the fill type you have selected. There are two fill types you can use or you can program your own. Reference to these two fill types is included at the bottom of this post.
The problem is that during back testing this situation causes an error which results in no trades being entered after the above situation occurs.
I'm not sure why this scenario would cause a problem with subsequent trades. What is the error you're seeing here?
=======
Default
An algorithm that takes a conservative and more realistic approach to filling limit and stop limit orders.
Limit orders only fill if the limit price was penetrated
Limit orders are always filled at the limit price specified never better (for example, if a limit order is submitted on bar n, NT will check if the order is filled on bar n+1, if this bar gaps down and the limit order was a buy, the order would be filled at the limit price and NOT the high of bar n+1)
Liberal
An algorithm that takes a liberal approach to filling limit and stop limit orders.
Limit orders fill if the limit price was touched
On gap down bars, buy limit orders will fill at the high of the gap down bar
On gap up bars, sell limit orders will fill at the low of the gap up bar
http://www.ninjatrader-support.com/HelpGuideV6/BacktestAStrategy.html
AresBowman
06-23-2010, 04:47 PM
Hi, thanks for the quick reply.
After your comments I was able to determine the true cause of the error while back testing. Here is what is happening
1) EnterLongLimit is called but the next bar does not penetrate the limit.
2) Follow on bars, e.g. 10 bars later, which would pierce the limit do not trigger the order.
3) Follow on bars which would trigger a buy condiiton based on the strategy do not trigger a buy order (Entries per Direction = 1)
My strategy only enters the "if" statement to EnterLongLimit if entryOrder == null. I also call a OnPositionUpdate() to set entryOrder = null, if MarketPosition.Flat. I'm guessing my problem has something to do with the null condition, but I'm not sure what I'm doing wrong.
Thanks
AresBowman
06-23-2010, 05:30 PM
Was able to fix the problem. The solution was to use an order.OrderState == Order.Cancelled condition rather than a position.MarketPosition == Market.Position.Flat to set entryOrder = null.
NinjaTrader_RyanM
06-24-2010, 08:00 AM
Great to hear. Thanks for the update, AresBowman.