|
Hi I am getting some strange results from my system so wrote a test system to find out the problem. Think I have isolated it can anyone explain how this it is possible to get an exit target order or stop order filled before the entry order when using OnOrderFilled:
using System.Drawing;
using OpenQuant.API; using OpenQuant.API.Indicators; using Indicators; using Portfolio;
public class MyStrategy : Strategy { [Parameter("Quantity", "Strategy Parameters")] double quantity;
Order targetOrder, stopOrder;
SMA sma;
public override void OnStrategyStart() { sma = new SMA(Bars, 10, Color.Green); Draw(sma, 0); }
public override void OnBar(Bar bar) { if (sma.Contains(bar.DateTime) && sma.Count>1) { if(!(HasPosition)) { if (sma.CrossesBelow(Bars, bar, BarData.Close)) { Buy(quantity, "Buy Entry"); }
if (sma.CrossesAbove(Bars, bar, BarData.Close)) { Sell(quantity, "Sell Entry"); } } } }
public override void OnOrderFilled(Order order) { //Cancel Position if (order.Type == OrderType.Stop) { CancelExitOrders(); } if (order.Type == OrderType.Limit) { CancelExitOrders(); }
//Target and Stop
if(order.Text == "Buy Entry") { targetOrder = SellLimitOrder(order.Qty, 10 + order.Price, "Target Sell"); stopOrder = SellStopOrder(order.Qty, order.Price - 10, "Stop Sell"); targetOrder.Send(); stopOrder.Send(); } if (order.Text == "Sell Entry") { targetOrder = BuyLimitOrder(order.Qty, order.Price - 10, "Target Buy"); stopOrder = BuyStopOrder(order.Qty, 10 + order.Price, "Stop Buy"); targetOrder.Send(); stopOrder.Send(); } } private void CancelExitOrders() { if(!(targetOrder.IsFilled || targetOrder.IsCancelled)) { targetOrder.Cancel(); } if (!(stopOrder.IsFilled || stopOrder.IsCancelled)) { stopOrder.Cancel(); } } }
Here are the results for this code based on QQQQ 5 min: This shouldn't be possible because an entry order has to be filled first. Also this system should only use OnOrderFilled so that we can be certain that the order has traded.
DateTime Symbol Side Price Qty Value Cost PnL Currency Comment 07/02/2002 22:30 QQQQ Sell 1378.44 1 1378.44 0 0 GBP Target Sell 07/02/2002 22:30 QQQQ Buy 1378.44 1 1378.44 0 0 GBP Buy Entry 07/02/2002 23:00 QQQQ Buy 1367.66 1 1367.66 0 0 GBP Stop Buy 07/02/2002 23:00 QQQQ Sell 1367.66 1 1367.66 0 0 GBP Sell Entry 07/03/2002 21:30 QQQQ Sell 1350.78 1 1350.78 0 0 GBP Target Sell 07/03/2002 21:30 QQQQ Buy 1350.78 1 1350.78 0 0 GBP Buy Entry 07/08/2002 18:00 QQQQ Buy 1431.11 1 1431.11 0 0 GBP Stop Buy 07/08/2002 18:00 QQQQ Sell 1431.11 1 1431.11 0 0 GBP Sell Entry 07/08/2002 22:30 QQQQ Sell 1425.05 1 1425.05 0 0 GBP Target Sell 07/08/2002 22:30 QQQQ Buy 1425.05 1 1425.05 0 0 GBP Buy Entry 07/08/2002 23:00 QQQQ Buy 1415.36 1 1415.36 0 0 GBP Stop Buy 07/08/2002 23:00 QQQQ Sell 1415.36 1 1415.36 0 0 GBP Sell Entry 07/09/2002 19:30 QQQQ Sell 1411.41 1 1411.41 0 0 GBP Target Sell 07/09/2002 19:30 QQQQ Buy 1411.41 1 1411.41 0 0 GBP Buy Entry 07/09/2002 20:00 QQQQ Buy 1401.05 1 1401.05 0 0 GBP Stop Buy 07/09/2002 20:00 QQQQ Sell 1401.05 1 1401.05 0 0 GBP Sell Entry 07/09/2002 21:30 QQQQ Sell 1406.43 1 1406.43 0 0 GBP Target Sell 07/09/2002 21:30 QQQQ Buy 1406.43 1 1406.43 0 0 GBP Buy Entry 07/09/2002 22:00 QQQQ Buy 1401.17 1 1401.17 0 0 GBP Stop Buy 07/09/2002 22:00 QQQQ Sell 1401.17 1 1401.17 0 0 GBP Sell Entry 07/11/2002 21:00 QQQQ Sell 1344.99 1 1344.99 0 0 GBP Target Sell 07/11/2002 21:00 QQQQ Buy 1344.99 1 1344.99 0 0 GBP Buy Entry 07/12/2002 22:30 QQQQ Buy 1378.71 1 1378.71 0 0 GBP Stop Buy 07/12/2002 22:30 QQQQ Sell 1378.71 1 1378.71 0 0 GBP Sell Entry
Thanks.
|