SmartQuant Discussion
http://www.smartquant.com/forums/

Order Management with Multiple Instruments
http://www.smartquant.com/forums/viewtopic.php?f=42&t=7298
Page 1 of 1

Author:  steveo311 [ Thu Feb 12, 2009 5:23 pm ]
Post subject:  Order Management with Multiple Instruments

Hi. I posted to the help desk forum but thought it might be more appropriate here...

I have been working with OQ for a week or so and I really like it but I am a little confused on how to manage orders (and hence risk) when I have a strategy running with multiple Instruments. For my strategy, on any new bar it is possible that multiple new orders (from different instruments) will be triggered and I would like to ensure that I don't place more orders than I can afford.

Is there a global list of all orders (for all Instruments) that have been initiated (like the one in the All tab in the Order Manager) that I can get access to? If so, would it immediately list an order after order.Send()? I tried a simple test with 15 Instruments in live mode (IB paper trade acct).

public override void OnStrategyStart()
{
Order marketOrder;
marketOrder = MarketOrder(OrderSide.Buy, 100, "Enter Long");
marketOrder.Send();
Console.WriteLine("Total Positions = " + Portfolio.Transactions.Count);
}

Which result in 15 orders being placed and Total Positions = 0 displayed 15 times.

I feel like I am missing something since my total exposure is based on the my current open positions plus any orders in the que but I am not seeing how to figure out how many orders (total for all Instruments) are currently qued. From a number of posts in this forum it feels like others have written their own order management utilities to deal with this problem but I wanted to check first before I do the same.

Thanks in advance.

Steve

Author:  dwil [ Thu Feb 19, 2009 5:11 am ]
Post subject: 

Hello steveo311,

I check all positions and live orders. I'll paste the code I use here as an example. It has some things in it specific to what I do, but I hope it helps. Some of those specific things in there are: breaking out positions by sector or total (by sending indexCode = 0); breaking out long and short positions; ignoring orders over a certain age (orderViableMinutes defined elsewhere). I do not claim to be the best C# coder around so I am sure there are improvements which could be made. Good luck.

Code:
    void CalculatePositions(ref double totPositions, ref double totPositionsShort, ref double totPositionsLong, int indexCode)
    {
        //   PositionList[] positions = Portfolio.Positions;
        totPositions = 0;
        totPositionsLong = 0;
        totPositionsShort = 0;
        foreach (Position positionCheck in Portfolio.Positions)
        {
            if (int.Parse(positionCheck.Instrument.Sector) == indexCode || indexCode == 0)
            {
                totPositions += Math.Abs(positionCheck.Amount * positionCheck.EntryPrice);
                if (positionCheck.Side == PositionSide.Long)
                {
                    totPositionsLong += positionCheck.Amount * positionCheck.EntryPrice;
                }
                else
                {
                    totPositionsShort += Math.Abs(positionCheck.Amount * positionCheck.EntryPrice);
                }
                //      Console.WriteLine(acctID + " " + "position " + " " + positionCheck.Amount + " Tot " + totPositions + " " + " totPositionsLong " + totPositionsLong + " " + " totPositionsShort " + totPositionsShort);
            }
        }

        foreach (Order orderCheck in Orders)
        {
            if ((orderCheck.Account == acctID) && (orderCheck.IsDone == false) && (orderCheck.DateTime > Clock.Now.AddMinutes(-1 * orderViableMinutes)))
            {
                if (int.Parse(orderCheck.Instrument.Sector) == indexCode || indexCode == 0)
                {
                    totPositions += Math.Abs(orderCheck.Qty * orderCheck.Price);
                    //add amount to long side if order is a buy and we have no position or are long
                    // do not want to add to totPositionsLong if we are exiting a short position
                    if ((orderCheck.Side == OrderSide.Buy) && ((Portfolio.Positions[orderCheck.Instrument] == null) ||
                        Portfolio.Positions[orderCheck.Instrument].Side == PositionSide.Long))
                    {
                        totPositions += Math.Abs(orderCheck.Qty * orderCheck.Price);
                        totPositionsLong += orderCheck.Qty * orderCheck.Price;
                    }
                    if ((orderCheck.Side == OrderSide.Sell) && ((Portfolio.Positions[orderCheck.Instrument] == null) ||
                        Portfolio.Positions[orderCheck.Instrument].Side == PositionSide.Short))
                    {
                        totPositions += Math.Abs(orderCheck.Qty * orderCheck.Price);
                        totPositionsShort += Math.Abs(orderCheck.Qty * orderCheck.Price);
                    }
                }
            }
        }
    }

Author:  steveo311 [ Fri Feb 20, 2009 7:44 am ]
Post subject: 

Wow. Thanks a lot for posting that dwil! Its very helpful to see what how someone else is handling this. My main issue was tracking orders globally across instruments. Is your Orders variable a static that is shared across instruments?

Author:  dwil [ Sat Feb 21, 2009 7:58 pm ]
Post subject: 

Your welcome.

I run that to calculate the positions and potential positions each time the system checks to see if it has enough purchasing power to enter a position.

Orders itself is an OQ object.

Author:  steveo311 [ Sun Feb 22, 2009 4:45 am ]
Post subject: 

Thats good info. I was under the assumption that Strategy.Orders was just for orders for Strategy.Symbol.

Steve

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/