Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why is method returning different values?

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

    Why is method returning different values?

    HI
    This is a method to split an order into 2.
    I am getting different values out - when the method is called - to those worked out inside the method.
    If I call a second time I get the correct value.
    The values a and b inside the method are always correct!!
    Its set to OnEachTick and I have printed what the method produces which is correct. But the "Out" part seems to hang on to the prior value?

    Here's an example
    Quantity Selector = 50
    Q0 output = 16 - called outside the method
    Q1 output = 17
    a = 25
    b = 25

    Quantity Selector = 29
    Q0 output = 25
    Q1 output = 25
    a = 15
    b = 14​
    Any help appreciated.

    Code:
                #region SetOrderQuantity()
                
                        private void SetOrderQuantity(out int Q0, out int Q1)
        {    
                ChartControl.Dispatcher.InvokeAsync((Action)(() =>
                {            
                        if(quantitySelector.Value >=2)
                    {
                        if(quantitySelector.Value % 2 ==1)
                        {
                         a = (quantitySelector.Value * 0.5 + 0.5);
                        
                        }
                        else
                        {
                        a = (quantitySelector.Value* 0.5);
                        }
                        b =quantitySelector.Value - a;    
                    }
                    else
                    {
                        a = 1;
                        b = 0;    
                    }
                    Print("a =  "+a.ToString());
                    Print("b =  "+b.ToString());
                }));    
                Q0 = (int)b;
                Q1 = (int)a;
    
                        
        
        }
            #endregion​
    Last edited by Mindset; 01-09-2025, 11:14 AM. Reason: correct print statements

    #2
    Hello Mindset,

    Thank you for your post.

    You are using a dispatcher which is executed at a later time, so you won't be able to use a and b outside the dispatcher to get the correct value. Any code that needs to reference these values needs to be within the dispatcher.

    Otherwise, the only time you can use the value would be at a later time - which is what you are seeing occur, the value isn't updated until a few bars later.

    Additionally, you cannot return anything from a dispatcher so your return method will not work. The dispatcher is running asynchronously to the rest of the script's processing.

    Comment


      #3
      Thanks Gaby
      I resolved the issue by taking the async out. Am i likely to run into any issues doing it this way?
      I have tested on simulated Data and it seems to work as I would want.
      Code:
      ChartControl.Dispatcher.Invoke((Action)(() =>
      ...

      Comment


        #4
        Hello,

        We generally recommend using InvokeAsync() to avoid potential threading issues. From the Help Guide:

        "Note: As a best practice, you should always make sure to use Dispatcher.InvokeAsync() to ensure your action is done asynchronously to any internal NinjaTrader actions. Calling the synchronous Dispatcher.Invoke() method can potentially result in a deadlock scenarios as your script is loaded.​"

        Comment


          #5
          thanks Gaby
          It didn't work anyway so I gave in and threw the function away.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          558 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          324 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          545 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          547 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X