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

Working with AddDataSeries - Using Two DataSeries in an Indicator

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

    Working with AddDataSeries - Using Two DataSeries in an Indicator

    I am having some issues implementing AddDataSeries to an indicator to pull data into my chart (same symbol different bar period) for calculation and plotting on existing chart. Before I get into my issue of concern, I would like to cover how I am implementing AddDataSeries, because I am doing something that documentation dose not advise.

    According to documentation located here: https://ninjatrader.com/support/help.../?barbrush.htm, I am not supposed to use variables or coded methods to pass values into this function. According to documentation, these values must be hardcoded because reliability of using variables in the (State == State.Configure) of the OnStateChange() event is not reliable.

    Unfortunately, I find myself in need of doing this very thing. In order to avoid writing a version of this indicator for every symbol and every timeframe of every symbol to hardcode the values into AddDataSeries, I am using variables, populated through code, so that only one indicator would be needed regardless of the charts period, bar type, or symbol.

    On the (State == State.Configure) of the OnChageState event, I have the following code:

    _DoubleBarPeriod = 2 * Bars.BarsPeriod.Value;
    BarsPeriodType bt = this.Bars.BarsPeriod.BarsPeriodType;
    AddDataSeries(bt, _DoubleBarPeriod);
    The goal is to add a data series, accessible to my indicator, that returns the close of each bar at double the bar period of the current chart to which this indicator is applied. As a side note, I am using a custom bartype that was developed by another individual that is locked, so I do not have access to the code. Since this is the case, the only way or me to obtain a reference to this bar type is by coding. In order to assure that my values passed into AddDataSeries are correct, I added the following lines of code after AddDataSeries was called:

    Print("Bar Type = " + bt.ToString());
    Print("Double Bar Period = " +_DoubleBarPeriod.ToString());
    Upon execution, the _DoubleBarPeriod returned the correct value in the output window. However, the bt.ToString() returned the value of 12345. I then changed my bar type from the custom, locked bar type to Range and ran the test again. On this second run bt.ToString() returned "Range" as expected. So, I am assuming that 12345 is the registered bat type that the developer assigned to the custom bar. Despite documentations recommendation to not take this approach, I find that this is working. - Or so I think.

    Now on to the issue...

    I am working on the understanding that the close value of each bar on a smaller timeframe will be different than the close value of larger timeframe of the same symbol. This is why the same indicator can plot with different values on different timeframes of the same chart. I need to obtain a LinearRegression of a series of closes on a time from twice as large as the chart to which the indicator is applied. I have tried using both Input and BarsArray[1]. of the Linear Regression function... Code as follows:

    (double) _myValue = LinReg(Input, _myPeriod)[0];
    and

    (double) _myValue = LinReg(BarsArray[1], _myPeriod)[0];
    Both options seem to be returning the same value. because the plot didn't change for both options. I believe that the LinReg function of each above is reading the close of the current chart and not the close of the AddDataSeries .. so, I did the following test....

    _myValue = LinReg(BarsArray[1], _myPeriod)[0];
    double myValue2 = LinReg(Input, _myPeriod)[0];
    Print ("myValue = " + _myValue.ToString() + " myValue2 = " + myValue2.ToString());
    The output revealed what I suspected … both are producing the same value …

    myValue = 1500.6 myValue2 = 1500.6
    myValue = 1500.90602150538 myValue2 = 1500.90602150538
    myValue = 1501.21677419355 myValue2 = 1501.21677419355
    myValue = 1501.53096774194 myValue2 = 1501.53096774194
    myValue = 1501.84731182796 myValue2 = 1501.84731182796
    myValue = 1502.16451612904 myValue2 = 1502.16451612904
    myValue = 1502.48129032258 myValue2 = 1502.48129032258
    myValue = 1502.79634408603 myValue2 = 1502.79634408603
    myValue = 1503.10838709678 myValue2 = 1503.10838709678
    myValue = 1503.41612903226 myValue2 = 1503.41612903226
    myValue = 1503.7182795699 myValue2 = 1503.7182795699
    myValue = 1504.0135483871 myValue2 = 1504.0135483871
    myValue = 1504.3006451613 myValue2 = 1504.3006451613
    myValue = 1504.5782795699 myValue2 = 1504.5782795699
    myValue = 1504.84516129033 myValue2 = 1504.84516129033
    myValue = 1505.10000000001 myValue2 = 1505.10000000001
    myValue = 1505.34150537635 myValue2 = 1505.34150537635
    myValue = 1505.56838709678 myValue2 = 1505.56838709678
    So, how do I obtain the close of the larger time frame for this calculation?

    Thanks in advance

    #2
    Hello jeliner,

    You are correct, dynamically adding series using variables is not supported. This will especially cause issues when optimizing in the Strategy Analyzer.

    "Warning:
    Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."



    I will submit a feature request on your behalf for the NinjaTrader Development to consider supporting this in a future version of NinjaTrader.

    Once I have a tracking ID for this request I will forward this to you for future reference.


    You can get the bar slot numbers. Below is a link to an indicator that prints these in the User App Share section of the NinjaTrader Ecosystem.
    This is just a simple script which prints the BarsTypes and their BarsPeriodType ID as an integer. This is to aid in finding BarsPeriodType ID’s for the use with AddDataSeries syntax. For more apps from this user, check out the Trendline Detection tool!



    As far as how the bar type should be called, there is a tip about this in the help guide:

    "2. You can add a custom BarsType which is installed on your system by casting the registered enum value for that BarsPeriodType. For example: AddDataSeries((BarsPeriodType)14, 10);

    3. You can specify optional BarsPeriod values (such as Value2) of a custom BarsType in the BarsPeriod object initializer. For example: AddDataSeries(new BarsPeriod() { BarsPeriodType =(BarsPeriodType)14, Value = 10, Value2 = 20 });"




    Regarding Input. This is typically the Close of the currently processing series or whatever was used as the input series for an indicator.


    Is your code being run during a specific BarsInProgress?

    (OnBarUpdate is called for each update for all series. BarsInProgress will be set to the index of that series.)


    Is Inputs[0][0] equal to Input or Closes[1][0]?




    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I will submit a feature request on your behalf for the NinjaTrader Development to consider supporting this in a future version of NinjaTrader. Once I have a tracking ID for this request I will forward this to you for future reference.
      Thanks! I hope the Ninja development team will consider changing this - I would much prefer to handle it the way I coded, independent of the PeriodType.

      "2. You can add a custom BarsType which is installed on your system by casting the registered enum value for that BarsPeriodType. For example: AddDataSeries((BarsPeriodType)14, 10);

      3. You can specify optional BarsPeriod values (such as Value2) of a custom BarsType in the BarsPeriod object initializer. For example: AddDataSeries(new BarsPeriod() { BarsPeriodType =(BarsPeriodType)14, Value = 10, Value2 = 20 });"
      I have actually considered this as an option. I have been trying to develop my own custom BarType for sometime now. That's the project that took me from a full set of hair to bald as CoJack. I have coded it, recoded it, deleted it, and started over on it several times - and I am no place close to understanding my its plotting so bad. The documentation developing custom bars is scant and doesn't provide much input. Until now, I put it on the back burner … I guess its time to revisit that monstrosity....

      Click image for larger version

Name:	jelQBar.png
Views:	275
Size:	261.1 KB
ID:	1047486

      Is your code being run during a specific BarsInProgress
      Nope, I didn't use that. I looked it up and tried it. I think its correcting my issue. The values I am getting though are not what I was expecting... but they are finally different than local values. Thanks for the input on that

      ..
      Attached Files

      Comment


        #4
        Hello jeliner,

        I've received tracking ID #SFT-882 for this request to dynamically add series with variables.

        Please note it is up to the NinjaTrader Development to decide if or when any request will be implemented.

        We appreciate your request on this.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Tim-c, Today, 03:54 AM
        0 responses
        3 views
        0 likes
        Last Post Tim-c
        by Tim-c
         
        Started by FrancisMorro, Today, 03:24 AM
        0 responses
        3 views
        0 likes
        Last Post FrancisMorro  
        Started by Segwin, 05-07-2018, 02:15 PM
        10 responses
        1,772 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
        945 views
        0 likes
        Last Post spwizard  
        Working...
        X