Orders and order management

I think a tutorial on order placement and management would be useful. It has taken me quite a while to work out how to do things. Most of the systems uploaded on the site simply place buy orders at market and nothing else. Also I am finding some of the terminology used a little unclear.

For example if I place an order to buy on stop just above the market using systemdata.openposition I will now have a working order in the broker. I only want to have this order placed once. Does the "Max open" field in the Position Management section of the system properties stop this order from being placed again when the next bar completes (assuming I set it to 1) ? There are currently no open positions but if it lets it go through there are going to be more than one.

How do I check what orders are currently being worked in the broker? I think it might be PositionManager.GetPendingOrdersForPosition or .GetPendingPositions. What is the difference between these two functions?

I cannot get stop orders to work at all. Whenever I use them in a system to try and stop into trades I come up with no trades being executed. I must be missing something or there is a problem. Maybe you could show me the code for a simple moving average system that buys on stop at the high of the day of crossover, and sells short on stop at the low of the day of crossover, always in the market (long or short). I couldn't even get any trades generated when I tried to set this up in drag and drop.

BigBerner (3/22/2007)I think a tutorial on order placement and management would be useful. It has taken me quite a while to work out how to do things. Most of the systems uploaded on the site simply place buy orders at market and nothing else. Also I am finding some of the terminology used a little unclear.

For example if I place an order to buy on stop just above the market using systemdata.openposition I will now have a working order in the broker. I only want to have this order placed once. Does the "Max open" field in the Position Management section of the system properties stop this order from being placed again when the next bar completes (assuming I set it to 1) ? There are currently no open positions but if it lets it go through there are going to be more than one.

How do I check what orders are currently being worked in the broker? I think it might be PositionManager.GetPendingOrdersForPosition or .GetPendingPositions. What is the difference between these two functions?

A pending order is an order that has been submitted, but not filled. Depending on the order it may or may not eventually be filled. A "pending" position is a position where the order to open the position is pending. It is not yet open. If the order is filled, it will then be an open position. If the order is canceled, the position will be discarded.

GetPendingPositions() returns the list of pending positions. GetPendingOrdersForPosition() returns the pending orders for a specific position. If the position is not open yet (pending), then it would return the order to open the position. If the position is open, then the pending orders would include the orders corresponding to the profit target, stop loss, etc., as well as any orders that were submitted by the user.

So to find all the orders being worked by the broker, you would call GetPendingOrdersForPosition() for all the positions returned by GetPendingPositons() and GetOpenPositions().

Looking at the code, the Max open positions setting only checks open positions, not pending positions. However, this is a bug, as it should also take into account the pending positions. I have added this as bug 888.

BigBerner (3/22/2007)Also what about cancelling a working order. If I use SystemData.Broker.CancelOrder(ID) then how do I get the ID if I used one of the overloads of openPosition that ignores the ID.

The position ID is different than the order ID, and it looks like you may be getting them confused. When you open a position, you have the option of specifying the position ID. If you don't, it will be created for you. You can get a list of the open or pending positions, and the PosID member will be the ID of each position.

The order ID is assigned by the broker when an order is submitted. You can get this from the OrderId member of the Order class. You can get a list of orders pending for a position with the GetPendingOrdersForPosition() method.

BigBerner (3/22/2007)I cannot get stop orders to work at all. Whenever I use them in a system to try and stop into trades I come up with no trades being executed. I must be missing something or there is a problem. Maybe you could show me the code for a simple moving average system that buys on stop at the high of the day of crossover, and sells short on stop at the low of the day of crossover, always in the market (long or short). I couldn't even get any trades generated when I tried to set this up in drag and drop.

The paper trader is not executing stop orders to buy (or short). I have added this as bug 889.

Bug 888 has been fixed. This fix will be in the next refresh. Also, the limits on the number of open positions were only applied if the OpenPosition() methods in SystemData were called. If PositionManager.OpenPosition() was called directly, they would not applied. This is also fixed.

BigBerner (3/22/2007)I think a tutorial on order placement and management would be useful. It has taken me quite a while to work out how to do things. Most of the systems uploaded on the site simply place buy orders at market and nothing else. Also I am finding some of the terminology used a little unclear.

For example if I place an order to buy on stop just above the market using systemdata.openposition I will now have a working order in the broker. I only want to have this order placed once. Does the "Max open" field in the Position Management section of the system properties stop this order from being placed again when the next bar completes (assuming I set it to 1) ? There are currently no open positions but if it lets it go through there are going to be more than one.

How do I check what orders are currently being worked in the broker? I think it might be PositionManager.GetPendingOrdersForPosition or .GetPendingPositions. What is the difference between these two functions?

A pending order is an order that has been submitted, but not filled. Depending on the order it may or may not eventually be filled. A "pending" position is a position where the order to open the position is pending. It is not yet open. If the order is filled, it will then be an open position. If the order is canceled, the position will be discarded.

GetPendingPositions() returns the list of pending positions. GetPendingOrdersForPosition() returns the pending orders for a specific position. If the position is not open yet (pending), then it would return the order to open the position. If the position is open, then the pending orders would include the orders corresponding to the profit target, stop loss, etc., as well as any orders that were submitted by the user.

So to find all the orders being worked by the broker, you would call GetPendingOrdersForPosition() for all the positions returned by GetPendingPositons() and GetOpenPositions().

Looking at the code, the Max open positions setting only checks open positions, not pending positions. However, this is a bug, as it should also take into account the pending positions. I have added this as bug 888.

Thanks Daniel, so this is what I think you mean.

Pending Order: This is an order that is working in the broker.

Pending Position: This is NOT an order that is working in the broker. It is an order that is being monitored by RE which may become a working order. (Correct me if I still haven't got it)

It seems to me that you have blurred the lines between an order and a position. You can place an order but you cannot place a position (which is what is seems to me you are doing with Pending Position). You place an order which will then have various states. A position either exists or it doesn't, you either have an exposure to the market or you don't. You can't have a pending position because this is really an order with a different orderstate. I think a pending position is an order with an orderstate that should be called "Monitoring" or something like that. I think you will have a lot of trouble if you use this terminology and suggest you change it.

Some Suggestions.

GetPendingPositions() should be GetMonitoringOrders() and should return a list of monitoring orders not positions.

GetPendingOrdersForPosition(String) should be GetMonitoringOrdersForPosition(String) and return the orders that are being monitored (Take profit etc) for a specific OPEN position.

dplaisted (3/22/2007)Bug 888 has been fixed. This fix will be in the next refresh. Also, the limits on the number of open positions were only applied if the OpenPosition() methods in SystemData were called. If PositionManager.OpenPosition() was called directly, they would not applied. This is also fixed.

Thanks,Daniel

Great, but just going to throw another scenario at you. Say I am running a vol breakout strategy where I wish to place a buy stop just above the market and a sell stop just below the market, but I only want to have one open position so set max open positions to 1. My guess is that you would not be able to place both orders as they would be treated as seperate positions, despite the fact that they would cancel each other out if they were both executed. So I guess the question becomes whether this feature is limiting the number of positions, or managing the potential market exposure. I'll leave it to you to decide.