Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Assign Order variable in OnOrderUpdade()

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

    Assign Order variable in OnOrderUpdade()

    private Order entryOrder = null;

    .........
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "DoubleLimitOrders";
    Calculate = Calculate.OnEachTick;

    ................

    protected override void OnBarUpdate()
    {
    if(State == State.Historical)
    {
    return;
    }
    //Add your custom strategy logic here.
    if(Position.MarketPosition == MarketPosition.Flat && entryOrder == null)


    {

    EnterLongStopMarket(0, true, 1, Close[0] + 8 * TickSize, "long limit entry");

    }

    Ok simple enough i have a stop market order 8 ticks above current price. As current price is changing so is my stop market order accordingly .
    But then i add ,this to the code above:

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

    if(order.Name == "long limit entry")
    entryOrder = order;
    }

    My stop market order has a fix price now. Why? I just assigned a stop market order named "long limit entry" to the variable entryOrder. What is going on?
    ​​​

    #2
    Hello cosmin1ke,

    Thank you for your post.

    It looks like the behavior has to do with the logic in your condition:

    if(Position.MarketPosition == MarketPosition.Flat && entryOrder == null)

    Your condition in OnBarUpdate is checking if entryOrder == null. As soon as entryOrder = order inside of OnOrderUpdate, it is no longer null. This means that the EnterLongStopMarket is not going to be hit inside of OnBarUpdate anymore because the condition is no longer true. If you want the order to be updated, you would have to add a new condition such as:
    if (entryOrder != null && // other condition(s) here)
    {
    // action you want such as ChangeOrder() to modify the price
    }

    For more information about using ChangeOrder() method:


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

    Comment


      #3
      From the response I understand that when OnBarUpdate() gets executed i get an EnterLongStopMarket in a working state. Then OnOrderUpdate gets executed which assigns my EnterLongStopMarket to a variable of type Order which was initially set to null. But since my EnterLongStopMarket was in an working state my variable of type Order is no longer null.
      So , a variable of type order is not null if the order assigned to it is in a state : working, cancelled, rejected, filled, pending, accepted and so on. Is that correct?

      Comment


        #4
        Hello cosmin1ke,

        Thank you for your reply.

        Your understanding is close, however the order object having a value assigned to it is not related to the order state.

        The object starts out as null when you declare it here:
        Code:
        private Order entryOrder = null;
        This block of code will only run when entryOrder is null:
        Code:
        if(Position.MarketPosition == MarketPosition.Flat && entryOrder == null)
        
        
        {
        
        EnterLongStopMarket(0, true, 1, Close[0] + 8 * TickSize, "long limit entry");
        
        }
        Inside of OnOrderUpdate, if there is an order update with the name "long limit entry" then the entryOrder is assigned the order object as its value:
        Code:
        if(order.Name == "long limit entry")
        entryOrder = order;
        Now, entryOrder will retain that value until you tell your script for entryOrder to be null again. Even if the order changes to a state of cancelled or filled, it does not automatically become null. We have a reference sample that demonstrates how to set your order object to null when it changes to one of these states. That sample may be found here:


        I recommend reviewing the reference sample for additional explanation and demonstration of working with order objects within your script, as this may help to illustrate what is happening in the script.

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

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        52 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        44 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        48 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X