Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A synchronous barsRequest.Request

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

    A synchronous barsRequest.Request

    Hello,

    I am using a BarsRequest to call on specific exchange rates. I have used the BarsREquest within a method so that I can call it whenever I want. I have a problem in that the BarsREquest code provided in the help guide does not perform the BarsREquest in a syncronised fashion and therefore the method returns zero because the method returns before the BarsRequest has had time to complete. I can work around this by using initiating a while loop however this is bad practice and I am aware of all the negative issues sleeping the ui thread brings so I am looking for a way to code the the barsRequest in a synchronous fashion so that the method will not complete until the BarsRequest has completed and in a way that I do not have to rely on the while loop to hold up the thread.

    My code is as follows:

    private double GetGBPUSD()
    {
    double gbpUsdClose=0;
    barsRequest2= new BarsRequest(Instrument.GetInstrument("GBPUSD"), 1);
    barsRequest2.BarsPeriod.BarsPeriodType = BarsPeriodType.Day;
    barsRequest2.BarsPeriod.Value = 1;
    barsRequest2.TradingHours = TradingHours.Get(Bars.Instrument.MasterInstrument. TradingHours.ToString());

    // Request the bars
    barsRequest2.Request(new Action<BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) =>
    {
    if (errorCode != ErrorCode.NoError)
    {
    // Handle any errors in requesting bars here
    Print(string.Format("Error on requesting bars: {0}, {1}",errorCode, errorMessage));
    return;
    }
    for (int i = 0; i < bars.Bars.Count; i++)
    {
    // Output the bars
    gbpUsdClose=bars.Bars.GetClose(i);
    //Print("GBPInsodemethod "+ gbpUsdClose);
    if(gbpUsdClose!=0) break;
    }
    }));

    while(gbpUsdClose==0)
    {
    Print("Getting the rate...");
    }
    barsRequest2.Dispose();
    return gbpUsdClose;
    }

    Many thanks

    #2
    Hello b16_aln,

    Thank you for your post.

    BarsRequest is an asynchronous method that accesses another thread and the callback is the function it runs when it is finished. The code written after the callback is not going to wait as it is currently written. You would need to move the following code into the callback so that it works asynchronously without relying on the BarsRequest to finish.

    Code:
    while(gbpUsdClose==0)
    {
    Print("Getting the rate...");
    }
    barsRequest2.Dispose();
    return gbpUsdClose;
    }
    If you need to work with series such as plots, drawing objects, or data series, you would use TriggerCustomEvent() before trying to access the series objects.

    Alternatively, you could loop a timer that checks every few hundred milliseconds to see if the callback from the BarsRequest has completed.

    TriggerCustomEvent() - https://ninjatrader.com/support/help...ustomevent.htm

    Please note that blocking/sleeping threads is not advised and will cause undesired behavior.

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks Brandon.

      I'm not exactly sure where to move that part of the code intot he callback to ensure it it correct. Could you please copy and paste the code into the correct palce and post it here?

      Many thanks

      Comment


        #4
        Hello b16_aln,

        Thank you for your note.

        The previously mentioned code could be moved to the end of the callback area as shown below.

        Code:
        // Request the bars
        barsRequest2.Request(new Action<BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) =>
        {[INDENT]if (errorCode != ErrorCode.NoError)
        {
        // Handle any errors in requesting bars here
        Print(string.Format("Error on requesting bars: {0}, {1}",errorCode, errorMessage));
        return;
        }
        for (int i = 0; i < bars.Bars.Count; i++)
        {
        // Output the bars
        gbpUsdClose=bars.Bars.GetClose(i);
        //Print("GBPInsodemethod "+ gbpUsdClose);
        if(gbpUsdClose!=0) break;[/INDENT][INDENT]while(gbpUsdClose==0)
        {
        Print("Getting the rate...");
        }
        barsRequest2.Dispose();
        return gbpUsdClose;
        }[/INDENT]
          }));
        Let us know if we may further assist.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Excellent, thanks for your assistance.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          587 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          341 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          103 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          555 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          552 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X