{"id":637,"date":"2007-07-14T00:03:18","date_gmt":"2007-07-14T00:03:18","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/07\/14\/preventing-repeat-orders-and-whipsaws\/"},"modified":"2015-10-03T00:33:33","modified_gmt":"2015-10-03T00:33:33","slug":"preventing-repeat-orders-and-whipsaws","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/07\/14\/preventing-repeat-orders-and-whipsaws\/","title":{"rendered":"Preventing Repeat Orders and Whipsaws"},"content":{"rendered":"
When you are developing a Real-Time trading system, often the first problem you have to solve is to prevent repeat orders. Repeat orders can occur when a signal remains true over several bars or chart refreshes, and each new bar\/refresh sends out a new order. This can result in unintentional pyramiding. <\/p>\n
Another ordering problem can occur when your entry and exit prices are too close together with respect to price volatility. In this case, the price volatility can whipsaw you in and out of positions in rapid succession. When this happens, slippage and commissions will quickly erode any profits you may have had. Also, if your system cannot keep up with each signal, you may end up in an opposing trade. <\/p>\n
A common mistake when attempting to prevent repeat orders is to wait for order confirmation from the TWS after each partial fill. The problem here is that confirmations from the TWS are always subject to significant delay, and they will often let several repeat orders slip through before the acknowledgment is received. <\/p>\n
Another flawed method is to filter or smooth the price arrays. Although this may prevent repeat orders, the lag introduced by this technique will kill most systems. <\/p>\n
There is no single best way to prevent repeat orders. Your solution will depend on your personal preferences and the principle of your trading system. Under certain conditions you may want to combine several of the methods shown below. The examples presented here are intended to make you aware of the problems involved and suggest some possible solutions. It is your responsibility to modify the code to suit your particular system’s requirements. <\/p>\n
To test the demo codes below, you will need to have the TWS running and connected to the IB eDemo or your paper trading account. To keep the programs below as simple as possible, you have to reset the programs after changing the Transmit ParamToggle from Off to On. <\/p>\n
Using OrderIDs to prevent repeat orders<\/strong><\/p>\n When your program calls the PlaceOrder() or ModifyOrder() function, the IBc returns a unique OrderID. The OrderID uniquely identifies the order you placed so that you can refer to it later to modify, cancel, or check order status. The OrderID only acknowledges that the order was placed; it does not mean that the order was transmitted to the market or was filled. <\/p>\n If an OrderID has been received, this means that the order was placed. You can prevent order repeats by checking whether the OrderID has a value. Then, if you only place orders when the OrderID is empty, you cannot place repeat orders. The first example below lets you explore this procedure. <\/p>\n After an order has been filled or cancelled, you may eventually want to transmit a new order. In this case, you will have to define a rule<\/i> for clearing the OrderID. This rule can be based on order status, a contrary signal, a time delay, the start of a new bar, or any number of things. Note that after you have cleared an OrderID, you can no longer modify or cancel the order it referred to. Hence, you should only clear an OrderID if you are sure that the OrderID is no longer needed, i.e., the order was confirmed cancelled, filled, or rejected. <\/p>\n The method below demonstrates how to use the OrderID, and because it still allows you to enter and exit (whipsaw) a position within milliseconds, it is only a first step to preventing repeats and whipsaws. To run this demo, Insert the code into a chart pane. When you open the Param window you will see these options: <\/p>\n <\/p>\n