SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Nov 21, 2018 3:24 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: OrderFilled problems
PostPosted: Fri Aug 10, 2012 10:35 am 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 221
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.


Top
 Profile  
 
 Post subject: Re: OrderFilled problems
PostPosted: Sat Aug 11, 2012 1:09 pm 
Offline

Joined: Wed Aug 08, 2007 6:32 pm
Posts: 221
Solved use AvgPrice for the fill


Top
 Profile  
 
 Post subject: Re: OrderFilled problems
PostPosted: Fri Aug 17, 2012 4:50 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6808
Good to hear you've solved it yourself! :wink:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group