{"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

The Chart Title shows IBc connection, OrderID, and Position status. Note that while Interactive Brokers calls all the messages ‘error’ messages, many are just harmless notifications. For more details on these error messages see API Error Messages<\/a>. <\/p>\n

<\/p>\n

When you click the Buy button, the Title will display the OrderID for the order placed, Order Status, and the position size if the order was filled. If you watch closely when you place an order, you may see order status change, for example from PreSubmitted<\/i> to Filled<\/i>: <\/p>\n

<\/p>\n

In this demo you clear the OrderIDs manually. In a real system it would be cleared when your system is ready to allow the next order to go out. <\/p>\n

RequestTimedRefresh<\/span>( <\/span>1 <\/span>); \r<\/span>ibc <\/span>= <\/span>GetTradingInterface<\/span>(<\/span>"IB"<\/span>); \r<\/span>Transmit <\/span>= <\/span>ParamToggle<\/span>(<\/span>"Transmit"<\/span>,<\/span>"OFF|ON"<\/span>,<\/span>0<\/span>); \r<\/span>BuyOrderTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Buy order"<\/span>,<\/span>"BUY"<\/span>); \r<\/span>SellOrderTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Sell order"<\/span>,<\/span>"SELL"<\/span>); \r<\/span>Reset <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Reset Program"<\/span>,<\/span>"RESET PROGRAM"<\/span>); \r<\/span>ClearBuyOrderID <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Clear Buy"<\/span>,<\/span>"CLEAR"<\/span>); \r<\/span>ClearSellOrderID <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Clear Sell"<\/span>,<\/span>"CLEAR"<\/span>); \r<\/span>BuyOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"BuyOrderID"<\/span>); \r<\/span>SellOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"SellOrderID"<\/span>); \r\r\r<\/span>BuyPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>BuyOrderID<\/span>); \r<\/span>SellPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>SellOrderID<\/span>); \rif( <\/span>BuyOrderTrigger <\/span>AND <\/span>BuyOrderID <\/span>== <\/span>"" <\/span>) \r{ \r<\/span>BuyOrderID<\/span>= <\/span>ibc<\/span>.<\/span>PlaceOrder<\/span>( <\/span>Name<\/span>(), <\/span>"Buy"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>BuyOrderID<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorBrightGreen <\/span>); \r} \rif( <\/span>SellOrderTrigger <\/span>AND <\/span>SellORderID <\/span>== <\/span>""<\/span>) \r{ \r<\/span>SellORderID <\/span>= <\/span>ibc<\/span>.<\/span>PlaceOrder<\/span>( <\/span>Name<\/span>(), <\/span>"Sell"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>SellOrderID<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorRed <\/span>); \r} \relse if( <\/span>Reset <\/span>) \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>BuyPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>BuyOrderID <\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>SellPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>SellOrderID <\/span>); \r<\/span>ibc<\/span>.<\/span>CloseAllOpenPositions<\/span>(); \r} \relse if( <\/span>ClearBuyOrderID <\/span>AND <\/span>BuyOrderID <\/span>!= <\/span>"" <\/span>)  \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>BuyPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>BuyOrderID <\/span>); \r} \relse if( <\/span>ClearSellOrderID <\/span>AND <\/span>SellOrderID <\/span>!= <\/span>"" <\/span>)  \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>SellPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>SellOrderID <\/span>); \r} \r<\/span>LastTWSMsg <\/span>= <\/span>ibc<\/span>.<\/span>getLastError<\/span>( <\/span>0 <\/span>); \r<\/span>Title <\/span>= <\/span>"\\n"<\/span>+ \r<\/span>"Last TWS Error Msg: "<\/span>+<\/span>LastTWSMsg<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" BuyOrder Status: "<\/span>+<\/span>WriteIf<\/span>( <\/span>BuyOrderID <\/span>!= <\/span>""<\/span>, <\/span>BuyOrderID<\/span>+<\/span>" "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>BuyORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>)+<\/span>"\\n"<\/span>+ \r<\/span>" SellOrder Status: "<\/span>+<\/span>WriteIf<\/span>( <\/span>SellOrderID <\/span>!= <\/span>""<\/span>, <\/span>SellOrderID<\/span>+<\/span>" "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>SellORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>)+<\/span>"\\n"<\/span>+ \r<\/span>" TWS Position Size: "<\/span>+<\/span>NumToStr<\/span>(<\/span>ibc<\/span>.<\/span>GetPositionSize<\/span>( <\/span>Name<\/span>() ),<\/span>1.0<\/span>,<\/span>False<\/span>); <\/span><\/pre>\n

Clearing OrderIDs at the Start of a New Bar<\/b> <\/p>\n

In this example the OrderIDs are automatically cleared at the start of a new bar. This limits each type of trade (Buy, Sell, short, or Cover) to one per bar. If you want to place a new order while an order is pending, you may either cancel it first or modify the pending order.
\nSince the start of a new bar can only be detected when its first quote arrives, it is impossible to get a fill at this time. The earliest time an order can be filled is on the second quote of the bar. How long this takes depends on market volume and whether you use eSignal (trades) or IB data (snapshots). Since the Opening quote can occur at any time during the bar interval, it may take from zero to the full bar interval before the OrderIDs are cleared. To prevent this delay, you can clear the orders with reference to your system’s clock. However, this requires that your system is perfectly synchronized with the market-time (see next example).
\nThe best way to test this example is to use a 1-minute database and set your chart to the 5-minute timeframe. Next, open the Bar-Replay<\/i> tool, select a range, and set the Step Interval<\/i> to 1 minute and the Step Rate<\/i> to 1\/Sec.
\nThe chart background will flash Green when a Buy order is placed, Red when a Sell order is placed, and White when a NewBar is started. You can test blocking of repeat orders by manually placing orders in rapid succession. The example code below will only place one Buy and\/or Sell order during each bar-period (between White flashes). This method does allow you to take quick profits when both your entry and exit price are hit within one bar period. However you can do this only once per bar.<\/b> <\/p>\n

RequestTimedRefresh<\/span>( <\/span>1 <\/span>); \r<\/span>ibc <\/span>= <\/span>GetTradingInterface<\/span>(<\/span>"IB"<\/span>); \r<\/span>Transmit <\/span>= <\/span>ParamToggle<\/span>(<\/span>"Transmit"<\/span>,<\/span>"OFF|ON"<\/span>,<\/span>0<\/span>); \r<\/span>BuyTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Buy order"<\/span>,<\/span>"BUY"<\/span>); \r<\/span>SellTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Sell order"<\/span>,<\/span>"SELL"<\/span>); \r<\/span>Reset <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Reset All"<\/span>,<\/span>"RESET"<\/span>); \r<\/span>PrevTN <\/span>= <\/span>StaticVarGet<\/span>(<\/span>"TimeNumber"<\/span>); \r<\/span>TN <\/span>= <\/span>LastValue<\/span>(<\/span>TimeNum<\/span>()); \r<\/span>NewBar <\/span>= <\/span>TN <\/span>!= <\/span>PrevTN<\/span>; \r<\/span>StaticVarSet<\/span>(<\/span>"TimeNumber"<\/span>,<\/span>TN<\/span>); \r<\/span>BuyOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"BuyOrderID"<\/span>); \r<\/span>SellOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"SellOrderID"<\/span>); \r<\/span>BuyPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>BuyOrderID<\/span>); \r<\/span>SellPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>SellOrderID<\/span>); \rif( <\/span>NewBar <\/span>) \r{ \rif( <\/span>NOT BuyPending <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>NOT SellPending <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorWhite <\/span>); \r} \rif( <\/span>BuyTrigger <\/span>AND <\/span>BuyOrderID <\/span>== <\/span>"" <\/span>) \r{ \r<\/span>BuyOrderID<\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>BuyOrderID<\/span>, <\/span>Name<\/span>(), <\/span>"Buy"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>BuyOrderID<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorBrightGreen <\/span>) ; \r} \relse if( <\/span>SellTrigger <\/span>AND <\/span>SellOrderID <\/span>== <\/span>"" <\/span>) \r{ \r<\/span>SellORderID <\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>SellORderID <\/span>, <\/span>Name<\/span>(), <\/span>"Sell"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>SellOrderID<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorRed <\/span>) ; \r} \relse if( <\/span>Reset <\/span>) \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>BuyPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>BuyOrderID <\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>SellPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>SellOrderID <\/span>); \r<\/span>ibc<\/span>.<\/span>CloseAllOpenPositions<\/span>(); \r} \r<\/span>LastTWSMsg <\/span>= <\/span>ibc<\/span>.<\/span>getLastError<\/span>( <\/span>0 <\/span>); \r<\/span>BuyStatus <\/span>= <\/span>WriteIf<\/span>( <\/span>BuyOrderID <\/span>!= <\/span>""<\/span>, <\/span>BuyOrderID<\/span>+<\/span>", Status: "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>BuyORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>); \r<\/span>SellStatus<\/span>= <\/span>WriteIf<\/span>( <\/span>SellOrderID <\/span>!= <\/span>""<\/span>, <\/span>SellOrderID<\/span>+<\/span>", Status: "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>SellORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>); \r<\/span>LastBuyTime<\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"LastBuyTime"<\/span>)); \r<\/span>LastSellTime<\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"LastSellTime"<\/span>)); \r<\/span>Title <\/span>= <\/span>"\\n"<\/span>+ \r<\/span>"Last TWS Error Msg: "<\/span>+<\/span>LastTWSMsg<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" BuyOrderID: "<\/span>+<\/span>BuyStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" SellOrderID: "<\/span>+<\/span>SellStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>"TWS Position Size: "<\/span>+<\/span>NumToStr<\/span>(<\/span>ibc<\/span>.<\/span>GetPositionSize<\/span>( <\/span>Name<\/span>() ),<\/span>1.0<\/span>,<\/span>False<\/span>); \r<\/span>Plot<\/span>(<\/span>C<\/span>,<\/span>"Close"<\/span>,<\/span>colorBlack<\/span>,<\/span>styleBar<\/span>); <\/span><\/pre>\n

Clearing OrderIDs after a Delay<\/strong> <\/p>\n

This example maintains a delay between same-type orders. It uses a delay-timer for each type of trade and clears the OrderIDs when the delay times out.
\nIf you set the delay to the bar-interval and if your system clock were synchronized with the market<\/i>, then this method would give similar results to the NewBar method discussed earlier. However, this method is better since it enables you to place your LMT orders before<\/i> the Open of the bar, thus giving you a one-quote timing advantage. This may not sound like much, but in fast trading, especially during moderate trading volume, this improves your chances of getting LMT fills.
\nAgain, the best way to test this example is to use a 1-minute database and set your chart to the 5-minute timeframe. Next, open the Bar-Replay<\/i> tool, select a range, and set the Step Interval<\/i> to 1 minute and the Step Rate<\/i> to 1\/Sec.
\nExperiment with the parameter options and observe that you cannot place a same-type order before the delay has timed out (see Chart Title). <\/b>
\n<\/b> <\/p>\n

RequestTimedRefresh<\/span>( <\/span>1 <\/span>); \r<\/span>ibc <\/span>= <\/span>GetTradingInterface<\/span>(<\/span>"IB"<\/span>); \r<\/span>Transmit <\/span>= <\/span>ParamToggle<\/span>(<\/span>"Transmit"<\/span>,<\/span>"OFF|ON"<\/span>,<\/span>0<\/span>); \r<\/span>BuyTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Buy order"<\/span>,<\/span>"BUY"<\/span>); \r<\/span>BuyLockoutPeriod <\/span>= <\/span>Param<\/span>(<\/span>"BuyLockoutPeriod"<\/span>,<\/span>10<\/span>,<\/span>1<\/span>,<\/span>300<\/span>,<\/span>1<\/span>); \r<\/span>SellLockoutPeriod <\/span>= <\/span>Param<\/span>(<\/span>"SellLockoutPeriod"<\/span>,<\/span>10<\/span>,<\/span>1<\/span>,<\/span>300<\/span>,<\/span>1<\/span>); \r<\/span>SellTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Sell order"<\/span>,<\/span>"SELL"<\/span>); \r<\/span>Reset <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Reset All"<\/span>,<\/span>"RESET"<\/span>); \r<\/span>BuyOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"BuyOrderID"<\/span>); \r<\/span>SellOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"SellOrderID"<\/span>); \r<\/span>BuyPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>BuyOrderID<\/span>); \r<\/span>SellPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>SellOrderID<\/span>); \r<\/span>RequestTimedRefresh<\/span>( <\/span>1 <\/span>); \r<\/span>NewSecond <\/span>= <\/span>Status<\/span>(<\/span>"redrawaction"<\/span>); \rif( <\/span>NewSecond <\/span>) \r{ \r<\/span>BuyCountDown <\/span>= <\/span>Max<\/span>( <\/span>0<\/span>, <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"BuyCountDown"<\/span>))-<\/span>1<\/span>); \r<\/span>StaticVarSet<\/span>(<\/span>"BuyCountDown"<\/span>, <\/span>BuyCountDown<\/span>); \rif( <\/span>BuyCountDown <\/span>== <\/span>0 <\/span>AND <\/span>NOT BuyPending <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>);  \r<\/span>SellCountDown <\/span>= <\/span>Max<\/span>( <\/span>0<\/span>, <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"SellCountDown"<\/span>))-<\/span>1<\/span>); \r<\/span>StaticVarSet<\/span>(<\/span>"SellCountDown"<\/span>, <\/span>SellCountDown<\/span>); \rif( <\/span>SellCountDown <\/span>== <\/span>0 <\/span>AND <\/span>NOT SellPending <\/span>) <\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \r} \r<\/span>BuyCountDown <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"BuyCountDown"<\/span>)); \r<\/span>SellCountDown <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"SellCountDown"<\/span>)); \rif( <\/span>BuyTrigger <\/span>AND ( <\/span>BuyOrderID <\/span>== <\/span>"" <\/span>AND <\/span>BuyCountDown <\/span>== <\/span>0 <\/span>) ) \r{ \r<\/span>BuyOrderID<\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>BuyOrderID<\/span>, <\/span>Name<\/span>(), <\/span>"Buy"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>BuyOrderID<\/span>); \r<\/span>StaticVarSet<\/span>(<\/span>"BuyCountDown"<\/span>, <\/span>BuyLockoutPeriod<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorBrightGreen <\/span>); \r} \relse if( <\/span>SellTrigger <\/span>AND ( <\/span>SellOrderID <\/span>== <\/span>"" <\/span>AND <\/span>SellCountDown <\/span>== <\/span>0 <\/span>) ) \r{ \r<\/span>SellORderID <\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>SellORderID <\/span>, <\/span>Name<\/span>(), <\/span>"Sell"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>SellOrderID<\/span>); \r<\/span>StaticVarSet<\/span>(<\/span>"SellCountDown"<\/span>, <\/span>SellLockoutPeriod<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorRed <\/span>); \r} \relse if( <\/span>Reset <\/span>) \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>BuyPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>BuyOrderID <\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>SellPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>SellOrderID <\/span>); \r<\/span>ibc<\/span>.<\/span>CloseAllOpenPositions<\/span>(); \r} \r<\/span>LastTWSMsg <\/span>= <\/span>ibc<\/span>.<\/span>getLastError<\/span>( <\/span>0 <\/span>); \r<\/span>BuyStatus <\/span>= <\/span>WriteIf<\/span>( <\/span>BuyOrderID <\/span>!= <\/span>""<\/span>, <\/span>BuyOrderID<\/span>+<\/span>", Status: "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>BuyORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>); \r<\/span>SellStatus<\/span>= <\/span>WriteIf<\/span>( <\/span>SellOrderID <\/span>!= <\/span>""<\/span>, <\/span>SellOrderID<\/span>+<\/span>", Status: "<\/span>+<\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>SellORderID<\/span>, <\/span>True <\/span>),<\/span>""<\/span>); \r<\/span>LastBuyTime<\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"LastBuyTime"<\/span>)); \r<\/span>LastSellTime<\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"LastSellTime"<\/span>)); \r<\/span>Title <\/span>= <\/span>"\\n"<\/span>+ \r<\/span>"Last TWS Error Msg: "<\/span>+<\/span>LastTWSMsg<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" BuyOrderID: "<\/span>+<\/span>BuyStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" BuyCountDown: "<\/span>+<\/span>NumToStr<\/span>(<\/span>BuyCountDown<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>)+<\/span>" Sec.\\n"<\/span>+ \r<\/span>" SellOrderID: "<\/span>+<\/span>SellStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" SellCountDown: "<\/span>+<\/span>NumToStr<\/span>(<\/span>SellCountDown<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>)+<\/span>" Sec."<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" TWS Position Size: "<\/span>+<\/span>NumToStr<\/span>(<\/span>ibc<\/span>.<\/span>GetPositionSize<\/span>( <\/span>Name<\/span>() ),<\/span>1.0<\/span>,<\/span>False<\/span>); \r<\/span>Plot<\/span>(<\/span>C<\/span>,<\/span>"Close"<\/span>,<\/span>colorBlack<\/span>,<\/span>styleBar<\/span>); <\/span><\/pre>\n

Alternating Trades<\/strong><\/p>\n

If your system trades LMT orders, you can design your orders to alternate between Long and Short without any limitations.
\nThis method is well suited for reversal systems and high speed trading.
\nIf your limit prices are set properly, this method will allow a fast sequence of reversal trades that can be very profitable. Systems like this may make several trades per minute, sometimes for several minutes, during high volatility.
\nIn the following example, Order Status is checked, and an opposite order is only allowed to pass if the previous order has been filled. Note that this demo does not work if the Transmit ParamToggle is turned Off because under that condition no orders are transmitted to the market and can thus never be filled.
\nAgain, the best way to test this example is to use a 1-minute database and set your chart to the 5-minute timeframe. Next open the Bar-Replay<\/i> tool, select a range, and set the Step Interval<\/i> to 1 minute and the Step Rate<\/i> to 1\/Sec. <\/p>\n

RequestTimedRefresh<\/span>( <\/span>1 <\/span>); \r<\/span>ibc <\/span>= <\/span>GetTradingInterface<\/span>(<\/span>"IB"<\/span>); \r<\/span>Transmit <\/span>= <\/span>ParamToggle<\/span>(<\/span>"Transmit"<\/span>,<\/span>"OFF|ON"<\/span>,<\/span>0<\/span>); \r<\/span>Reset <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Reset All"<\/span>,<\/span>"RESET"<\/span>); \r<\/span>BuyTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Buy order"<\/span>,<\/span>"BUY"<\/span>); \r<\/span>SellTrigger <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"Place Sell order"<\/span>,<\/span>"SELL"<\/span>); \r<\/span>LastTrade <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"LastTrade"<\/span>); \r<\/span>BuyOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"BuyOrderID"<\/span>); \r<\/span>SellOrderID <\/span>= <\/span>StaticVarGetText<\/span>(<\/span>"SellOrderID"<\/span>); \r<\/span>BuyPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>BuyOrderID<\/span>); \r<\/span>SellPending <\/span>= <\/span>ibc<\/span>.<\/span>IsOrderPending<\/span>(<\/span>SellOrderID<\/span>); \r<\/span>IBPosSize <\/span>= <\/span>ibc<\/span>.<\/span>GetPositionSize<\/span>( <\/span>Name<\/span>() ); \r<\/span>BuyStatus <\/span>= <\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>BuyORderID<\/span>, <\/span>True <\/span>); \r<\/span>SellStatus <\/span>= <\/span>ibc<\/span>.<\/span>GetStatus<\/span>( <\/span>SellORderID<\/span>, <\/span>True<\/span>); \rif( <\/span>BuyTrigger <\/span>) \r{  \rif( <\/span>LastTrade <\/span>== <\/span>"Sell" <\/span>OR <\/span>LastTrade <\/span>== <\/span>"" <\/span>) \r{ \rif( <\/span>SellStatus <\/span>== <\/span>"Filled" <\/span>OR <\/span>SellStatus <\/span>== <\/span>"" <\/span>) \r{ \r<\/span>BuyOrderID<\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>BuyOrderID<\/span>, <\/span>Name<\/span>(), <\/span>"Buy"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>BuyOrderID<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"LastTrade"<\/span>, <\/span>"Buy"<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorBrightGreen <\/span>) ; \r} \r} \r} \relse if( <\/span>SellTrigger <\/span>) \r{ \rif( <\/span>LastTrade <\/span>== <\/span>"Buy" <\/span>OR <\/span>LastTrade <\/span>== <\/span>"" <\/span>) \r{ \rif( <\/span>BuyStatus <\/span>== <\/span>"Filled" <\/span>OR <\/span>BuyStatus <\/span>== <\/span>"" <\/span>) \r{ \r<\/span>SellORderID <\/span>= <\/span>ibc<\/span>.<\/span>ModifyOrder<\/span>( <\/span>SellORderID <\/span>, <\/span>Name<\/span>(), <\/span>"Sell"<\/span>, <\/span>100<\/span>, <\/span>"MKT"<\/span>, <\/span>0<\/span>, <\/span>0<\/span>, <\/span>"Day"<\/span>, <\/span>Transmit<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>SellOrderID<\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"LastTrade"<\/span>, <\/span>"Sell"<\/span>); \r<\/span>SetChartBkColor<\/span>( <\/span>colorRed <\/span>) ; \r} \r} \r} \relse if( <\/span>Reset <\/span>) \r{ \r<\/span>StaticVarSetText<\/span>(<\/span>"BuyOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>BuyPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>BuyOrderID <\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"SellOrderID"<\/span>,<\/span>""<\/span>); \rif( <\/span>SellPending <\/span>) <\/span>ibc<\/span>.<\/span>CancelOrder<\/span>( <\/span>SellOrderID <\/span>); \r<\/span>StaticVarSetText<\/span>(<\/span>"LastTrade"<\/span>, <\/span>""<\/span>); \r<\/span>ibc<\/span>.<\/span>CloseAllOpenPositions<\/span>(); \r} \r<\/span>LastTWSMsg <\/span>= <\/span>ibc<\/span>.<\/span>getLastError<\/span>( <\/span>0 <\/span>); \r<\/span>\/\/BuyStatus = WriteIf( BuyPending, BuyOrderID+\", Status: \"+BuyStatus,\"\"); \r\/\/SellStatus= WriteIf( SellPending, SellOrderID+\", Status: \"+SellStatus,\"\"); \r<\/span>Title <\/span>= <\/span>"\\n"<\/span>+ \r<\/span>"Last TWS Error Msg: "<\/span>+<\/span>LastTWSMsg<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" BuyOrderID: "<\/span>+<\/span>BuyOrderID<\/span>+<\/span>" "<\/span>+<\/span>BuyStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" SellOrderID: "<\/span>+<\/span>SellOrderID<\/span>+<\/span>" "<\/span>+<\/span>SellStatus<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" Last Trade: "<\/span>+<\/span>LastTrade<\/span>+<\/span>"\\n"<\/span>+ \r<\/span>" TWS Position Size: "<\/span>+<\/span>NumToStr<\/span>(<\/span>IBPosSize<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>); \r<\/span>Plot<\/span>(<\/span>C<\/span>,<\/span>"Close"<\/span>,<\/span>colorBlack<\/span>,<\/span>styleBar<\/span>); <\/span><\/pre>\n

Edited by Al Venosa<\/p>\n","protected":false},"excerpt":{"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. Another ordering problem can […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/637"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=637"}],"version-history":[{"count":6,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/637\/revisions"}],"predecessor-version":[{"id":3509,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/637\/revisions\/3509"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=637"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}