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

Accessing indicator properties inside another indicator

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

    Accessing indicator properties inside another indicator

    Hello,
    I've written a simple indicator that sets price highs and lows. I'd like to reuse those values as input for other indicators. Unfortunately when I try to access those values I'm only getting back the Close[0]. What could cause that?
    Here's the code:
    Code:
    public class A1PNV : Indicator
        {
            #region Variables
    
                private DataSeries pricePeak;
                private DataSeries priceValley;
    
            #endregion
    
            protected override void Initialize()
            {
                Overlay                = true;
    
                pricePeak            = new DataSeries(this);
                priceValley            = new DataSeries(this);
            }
    
            protected override void OnBarUpdate()
            {
                pricePeak.Set(Peak_Value(High,Open,Close));
                priceValley.Set(Valley_Value(Low, Open, Close));
            }
    
            #region Properties
    
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Price_Peak
            {
                get { return Values[0]; }
            }
            [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Price_Valley
            {
                get { return Values[1]; }
            }
            #endregion
    Inside the second indicator I have:
    Code:
    protected override void OnBarUpdate()
    {
                A1PNV pnv = A1PNV();
                double pp = pnv.Price_Peak[0];
    }
    pp returns the Close[0].
    The A1PNV indicator works fine on its own. It's only when I try to reference it in another indicator that I'm having an issue. Any help would be greatly appreciated.

    #2
    Hello CaptainAmericaXX,

    This is due to using "DataSeries" instead of using a Plot and also the public properties you created. You are mixing two separate concepts here, I will explain this in more detail.

    Code:
    private DataSeries pricePeak;
    You have created a private DataSeries object to store data. The problem is that you are using the private variable name: pricePeak but this is not the same as Price_Peak, your public property.

    The public properties you are using are referencing the Plots your indicator has, but you didn't add any so there is nothing to reference here:

    Code:
    return Values[0];
    return Values[1];
    When you try to get the value, you are referencing a empty plot which will default to the close price. From what you provided, it seems you have laid this out as the standard plotting format where you plot from indicator A, then call indicator "A" from another script to retrieve its value. If that is what you were trying to follow, you need to remove the private DataSeries you created and instead use two plots:
    https://ninjatrader.com/support/help...hlightsub=Plot

    Code:
    protected override void Initialize()
    {
        Add(new Plot(Color.Blue, "Price_Peak"));
        Add(new Plot(Color.Blue, "Price_Valley"));
    }
    Then in OnBarUpdate, make sure to use the public property you created:

    Code:
    Price_Peak.Set(Peak_Value(High,Open,Close));
    If you specifically wanted to use a DataSeries or other type as a public property, we have a sample showing how that can be used here: https://ninjatrader.com/support/help...alues_that.htm

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      THANK YOU Jesse! Thanks for catching my spelling errors in the private variable/properties and for teaching me the difference in the Plots and DataSeries values. That solved A LOT of recurring problems in my coding.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      57 views
      0 likes
      Last Post DynamicTest  
      Started by ScottWalsh, Today, 06:52 PM
      4 responses
      35 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
      19 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      9 views
      0 likes
      Last Post cre8able  
      Working...
      X