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

GetDayBar - Not working as expected. How to get last working day OHLC Data?

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

    GetDayBar - Not working as expected. How to get last working day OHLC Data?

    Hi,

    I want to get the OHLC data for last trading day. while searching Ninja Help Guide, i get to know Bars.GetDayBar() which suits my requirement.

    But i tired calling this functions with possible combinations, but it is raising exception. Below is the code which i tried using.

    Bars b = BarsArray[0];

    Print(b.Count.ToString()); //Prints 2556 bars which is approx 10 days of data

    Bar day = null;

    for (int i = 0; i < 10; i++)
    {
    try
    {
    day = b.GetDayBar(i); //Always raise exception
    }
    catch { }

    if (day != null)
    break;
    }

    if (day == null)
    Print("Day is null"); //Always prints null


    Please tell me the best way to get previous OHLC data?

    Thanks

    #2
    Hi raaxus, thanks for your note.

    You should check if the Bar return is null before assigning it. The index should also not go below 1. This method is used to get historical day bars. To get the current day bar use CurrentDayOHL() e.g. This will get the past 10 days:

    Code:
    protected override void OnBarUpdate()
            {
    
                Bars b = BarsArray[0];
    
                Bar d = null;
    
                for (int i = 1; i <= 10; i++)
                {
                    if (Bars.GetDayBar(i) != null)
                    {
                        d = Bars.GetDayBar(i);
                        Print(d.Close);
                    }
                }
            }
    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      Thanks it worked perfectly fine.

      But why my code wasn't working, because Bar is a class type and should be able to hold null values?

      CurrentDayOHL():
      Do you have any link for documentation of these kind of functions where i can reference? I hope this is an indocator?

      Thanks,

      Comment


        #4
        Hi,

        My apologies, the value can be null since Bar is nullable. I suspect the problem was from for (int i = 0; i < 10; i++) rather than for (int i = 1; i <= 10; i++). GetDayBar will only return prior daily bars. You can step through the code using Visual Studio debugging to find exactly when the return value is null.

        The CurrentDayOHL() indicator documentation can be found here:


        It is an indicator, so the source code can be viewed from the NinjaScript Editor. There is also the PriorDayOHLC indicator that might be of use.

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by bsbisme, Yesterday, 02:08 PM
        1 response
        15 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by prdecast, Today, 06:07 AM
        0 responses
        3 views
        0 likes
        Last Post prdecast  
        Started by i019945nj, 12-14-2023, 06:41 AM
        3 responses
        60 views
        0 likes
        Last Post i019945nj  
        Started by TraderBCL, Today, 04:38 AM
        2 responses
        18 views
        0 likes
        Last Post TraderBCL  
        Started by martin70, 03-24-2023, 04:58 AM
        14 responses
        106 views
        0 likes
        Last Post martin70  
        Working...
        X