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

multi-time frame error

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

    multi-time frame error

    Hi

    Suppose I have a strategy that I am developing on 30-minute bars, but I also include a daily bar in State.Configure . If I try to print the Times of both the bars together, I get an error, But if I only try to print the Times one at a time using BarsInProgress, then things work done.

    The main chart has 30 minute data, and 365 days of data has been loaded.

    I'm trying to understand why is this the case?


    -------------------------------

    THIS GIVES AN ERROR: Strategy 'MyTestStrategy': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


    public class MyTestStrategy : Strategy
    {

    protected override void OnStateChange()
    {
    ....
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    else if (State == State.DataLoaded)
    {
    ClearOutputWindow();
    }
    }

    protected override void OnBarUpdate()
    {
    string line = BarsInProgress.ToString() + ", ";
    if (Times[0].IsValidDataPoint(0))
    line = line + Times[0][0] + ", ";
    else
    line = line + "Not Available, ";
    if (Times[1].IsValidDataPoint(0))
    line = line + Times[1][0];
    else
    line = line + "Not Available";

    Print(line);

    }
    }

    -------------------------------
    THIS WORKS FINE

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    .....
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    else if (State == State.DataLoaded)
    {
    ClearOutputWindow();
    }
    }

    protected override void OnBarUpdate()
    {
    string line = BarsInProgress.ToString() + ", ";
    if (BarsInProgress == 0)
    {
    if (Times[0].IsValidDataPoint(0))
    line = line + Times[0][0] + ", ";
    else
    line = line + "Not Available, ";
    }
    if (BarsInProgress == 1)
    {
    if (Times[1].IsValidDataPoint(0))
    line = line + Times[1][0];
    else
    line = line + "Not Available";
    }

    Print(line);

    }
    }

    #2
    Hello uday12,

    Thank you for your post.

    The first snippet does not work because you are trying to access the daily bar index before checking that a value exists at that index. IsValidDataPoint will not perform an index check before it accesses the series value. If you need to access the Daily bars value while OnBarUpdate is being called for the 30 minute series, add a check for CurrentBars[1] > 1 then access the Times[][] array if that is true.

    e.g

    Code:
    protected override void OnBarUpdate()
    {
        if(BarsInProgress == 0)
        {
             if(CurrentBars[1] > 1)
            {
                //We have at least 1 daily bar, Times[1][0] is safe to access. 
            }
                //Additional 30 minute logic.
        }
    }
    Please let me know if I can assist further.
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Felix Reichert, 04-26-2024, 02:12 PM
    10 responses
    68 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by PaulMohn, 04-24-2024, 03:49 AM
    4 responses
    36 views
    0 likes
    Last Post PaulMohn  
    Started by lightsun47, Today, 11:37 AM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Zachary  
    Started by vitaly_p, Yesterday, 05:09 PM
    4 responses
    35 views
    0 likes
    Last Post vitaly_p  
    Started by bortz, 11-06-2023, 08:04 AM
    50 responses
    1,773 views
    0 likes
    Last Post carnitron  
    Working...
    X