Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to store entry orders in strategy class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to store entry orders in strategy class

    My Strategy class has private Order references to entry orders as follows:

    public class MyStrategy {
    private Order shortEntry = null;
    private Order longEntry = null;

    ....
    }

    Now suppose I want to enter a BUYSTOP or a SELLSTOP order to enter a position. It looks like there are at least two ways of keeping track of these orders:

    Method 1:

    this.longEntry = SubmitOrderUnmanaged(DATA_SERIES_ORDERS, OrderAction.Buy, OrderType.StopMarket, quantity, 0, entryStopPrice);
    OR
    thisshortEntry = SubmitOrderUnmanaged(DATA_SERIES_ORDERS, OrderAction.SellShort, OrderType.StopMarket, quantity, 0, entryStopPrice);

    This method uses the Order object returned from SubmitOrderUnmanaged. But note that it does not provide a value for "oco" or signalName

    Method 2:

    SubmitOrderUnmanaged(DATA_SERIES_ORDERS, OrderAction.Buy, OrderType.StopMarket, quantity, 0, entryStopPrice, "", "MY_LONG_ENTRY");
    OR
    SubmitOrderUnmanaged(DATA_SERIES_ORDERS, OrderAction.SellShort, OrderType.StopMarket, quantity, 0, entryStopPrice, "", "MY_SHORT_ENTRY");

    and then implement OnOrderUpdate:

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice,
    int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment) {

    if (order.Name == "MY_LONG_ENTRY") {
    this.longEntry = order;
    } else if (order.Name == "MY_SHORT_ENTRY") {
    this.shortEntry = order;
    }
    }

    Ninjatrader support has an example Strategy that uses approach 2, and the comments inside OnOrderUpdate say this:
    // Assign Order objects here
    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting

    So why is method 2 better than method 1 for entry orders? Is it really the case that you should not use the Order object returned from SubmitOrderUnmanaged?

    Also, if we just want to submit a basic BuyStop or SellStop (or BuyLimit or SellLimit) order and we don't want to set any OCO, do we just use a blank string "" for oco as in the example above?

    #2
    Hello westofpluto,

    Thank you for your post.

    The big reason to use approach #2 is that although assigning the Order object in OnBarUpdate will likely work in many instances, assigning Order objects outside of OnOrderUpdate() is not guaranteed to be completed if you were to immediately reference the Order object after assigning it due to the fact that OnBarUpdate and OnOrderUpdate are completely asynchronous. The order is only guaranteed to be completed when OnOrderUpdate completes it.

    And yes, if you do not wish orders to be OCO linked, you'd just use a blank string like in that example.

    Please let us know if we may be of further assistance to you.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    113 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    60 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    43 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    81 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X