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

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.
    Brandon H.NinjaTrader Customer Service

    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.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Excellent, thanks for your assistance.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          59 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          36 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          7 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          21 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          10 views
          0 likes
          Last Post cre8able  
          Working...
          X