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

AddDataSeries breaks strategy

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

    AddDataSeries breaks strategy

    Hi,

    I have a strategy and its working fine. However when I add a data series to the strategy without any other code changes the strategy starts placing random trades continuously. The strategy that I have written uses the EURUSD 1 min data. When I add the line below to AddDataSeries then the strategy stops working correctly. This is the only additional line of code I have added. I haven't added any code to access the 2nd data series yet.

    else if (State == State.Configure)
    {
    AddDataSeries("AUDUSD", Data.BarsPeriodType.Minute, 1);
    }

    Now what is strange is if I add the line below to add the EURUSD it starts working again however the strategy output is slightly different then if I had the two lines of code below commented out. Note in the strategy analyzer I have the instrument selected as EURUSD at 1 min.

    else if (State == State.Configure)
    {
    AddDataSeries("AUDUSD", Data.BarsPeriodType.Minute, 1);
    AddDataSeries("EURUSD", Data.BarsPeriodType.Minute, 1);
    }

    My question is am I missing something when wanting to use another data series. I'm using NinjaTrader 8.0.0.10. Why when I add the line 'AddDataSeries("AUDUSD", Data.BarsPeriodType.Minute, 1);' it completely changes the output of my strategy without any other code changes?

    I also found that when getting the value of the highest bar it gets the value of the AUDUSD data series and then the value of the EURUSD data series in random order with the code below.

    int highestBarsAgo = HighestBar(High, 100);
    double highestPrice = High[highestBarsAgo];
    Print(highestPrice);

    I have been using the link below as reference for correct usage of AddDataSeries.


    Cheers,
    Mark

    #2
    BarsInProgress check?

    protected override void OnBarUpdate()
    {
    // Ignore bar update events for the supplementary - Bars object added above
    if (BarsInProgress == 1 || BarsInProgress == 2)
    return;

    Comment


      #3
      Hello Mark
      When Adding a another dataseries to a strategy also means you have to add additional code that will control what gets executed using the which dataseries.

      The OnBarUpdate event will trigger once for every occurrence of data for both dataseries.

      When you are only using 1 primary dataseries, OnBarUpdate will trigger once for every bar.

      When you add a secondary dataseries, OnBarUpdate will trigger once for every bar in the primary AND secondary dataseries.

      So if you apply your strategy to a 5min chart, your primary dataseries will be 5 min. and your OnBarUpdate events will trigger once for every 5 min bar

      If you add a secondary 10min dataseries to your strategy and apply it to a 5 min chart, your OnBarUpdate events will be the same as before, but will also include an additional event for a 10 min bar.

      To control what gets executed on the primary dataseries versus what gets executed on the secondary dataseries, you must use a something that looks like this in your OnBarUpdate event.


      protected override void OnBarUpdate()
      {
      // Checks to ensure all Bars objects contain enough bars before beginning
      // If this is an indicator, use BarsRequiredToPlot instead of BarsRequiredToTrade
      if (CurrentBars[0] <= BarsRequiredToTrade || CurrentBars[1] <= BarsRequiredToTrade)
      return;

      // Primary Bar Series
      if (BarsInProgress == 0)
      {
      // code to execute using primary data series
      }

      // Secondary Bar Series
      if (BarsInProgress == 1)
      {
      // code to execute using secondary data series
      }
      }


      In addition, methods Open High Low Close and Time are designed to only work with the primary dataseries.
      You will need to instead use Opens[0][0] Highs[0][0] Lows[0][0] Closes[0][0] and Times[0][0] so that you specify what dataseries and barsago you want

      For example, when your strategy uses 2 dataseries, and you want to compare if the close of the primary dataseries is greater that the close of the secondary series you would use

      if(Closes[0][0] > Closes[1][0])
      Last edited by Lucius; 03-26-2016, 09:42 AM.

      Comment


        #4
        Thanks sledge and Lucius. I didn't realise the onbarupdate triggers once for each dataseries. The BarsInProgress should fix my issue.

        Thank you for the quick reply.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by FrancisMorro, Today, 03:24 AM
        0 responses
        1 view
        0 likes
        Last Post FrancisMorro  
        Started by Segwin, 05-07-2018, 02:15 PM
        10 responses
        1,770 views
        0 likes
        Last Post Leafcutter  
        Started by Rapine Heihei, 04-23-2024, 07:51 PM
        2 responses
        31 views
        0 likes
        Last Post Max238
        by Max238
         
        Started by Shansen, 08-30-2019, 10:18 PM
        24 responses
        944 views
        0 likes
        Last Post spwizard  
        Started by Max238, Today, 01:28 AM
        0 responses
        11 views
        0 likes
        Last Post Max238
        by Max238
         
        Working...
        X