Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Kate W.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by martin70, 03-24-2023, 04:58 AM
    15 responses
    114 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by The_Sec, Today, 02:29 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by jeronymite, 04-12-2024, 04:26 PM
    2 responses
    31 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by Mindset, 05-06-2023, 09:03 PM
    10 responses
    265 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by michi08, 10-05-2018, 09:31 AM
    5 responses
    743 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X