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

Help to call 2 Indicators into a third indicator

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

    Help to call 2 Indicators into a third indicator

    Hello,

    I have created 2 indicators (A and B) which each have a DrawText line that send computations to my chart, and both work fine. I have attempted to bring them into a third indicator C, with the intention of deducting the calculations as follows (IndicatorA + IndicatorB). Indicator B is already a negative. I have compiled an indicator C, but am not sure how to go about achieving this computation and having it appear on the chart.

    1. Do I need to do a new DrawText line in Indicator C to calculate and display A+B? Currently I don’t even see the original texts from A or B when I load C (is this because the indicator values are not a plot and so I have to do DrawText again in C).
    2. The parameters in Indicators A and B do not show up in the properties for Indicator C when it is loaded. Is there a way for me to access them other than loading A and B?
    I have copied the relevant sections of Indicators A to C below.

    Thanks for the help.

    IndicatorA – important sections:

    #region Variables
    // Wizard generated variables
    // User defined variables (add any user defined variables below)
    private DataSeries myIndicatorA;

    #endregion

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = true;
    CalculateOnBarClose = true;
    //to set the input type when added to a chart. False means setting cannot be changed
    PriceTypeSupported = false;

    //Now create the objects that hold the data
    myIndicatorA = new DataSeries(this);
    }

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    if (CurrentBar == 0)
    {
    return;
    }

    {
    DataSeries
    // class is used to set the value of the current location, or bar.
    myIndicatorA.Set(diffVol);
    }
    if ((Open[0] == Close[0]) && (Math.Abs(High[0] - Open[0]) > Math.Abs(Low[0] - Open[0]))
    {
    DrawText (TagdiffVol1 + CurrentBar, diffVol.ToString(), 0, Low[0] - 2 * TickSize, Color.Red);
    }

    IndicatorB:

    public class IndicatorB : Indicator
    {
    #region Variables
    // Wizard generated variables

    // User defined variables (add any user defined variables below)
    // These will hold DataSeries variables that can be accessed by
    // other indicators and strategies
    private DataSeries myIndicatorB;
    //A parameter/variable that could have been generated by the wizard

    private string TagwTSum1 = "wTSum"; //The tag for the DrawText
    #endregion

    protected override void Initialize()
    {
    //This indicator only used to build other indicators and strategies
    // so nothing placed on the price chart, so
    Overlay = false;
    // Indicator to update when the bar closes, so
    CalculateOnBarClose = true;

    //Now create the objects that hold the data, i.e.
    // the DataSeries created under variables above

    myIndicatorB = new DataSeries (this);

    }

    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each - need at least 2 bars for calculation
    if (CurrentBar == 0)
    {
    return;
    }
    if (wTSum < 15)
    {
    DrawText (TagwTSum1 + CurrentBar, wTSum.ToString(), 0, High[0] + 1 * TickSize, Color.Black);
    }
    //Set the calculated values to the plots

    myIndicatorB.Set (wTSum);


    }
    #region Properties

    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries MyIndicatorB
    {
    get {return myIndicatorB;}
    }
    #endregion

    IndicatorC:

    public class IndicatorC : Indicator
    {
    #region Variables
    // Wizard generated variables
    // User defined variables (add any user defined variables below)
    private DataSeries diffIndA;
    private DataSeries changeIndB;
    private DataSeries IndicatorA;
    private DataSeries IndicatorB;
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = true;

    //Now create the objects that hold the data
    diffIndA = new DataSeries(this);
    changeIndB = new DataSeries (this);

    IndicatorA = new DataSeries (this);
    IndicatorB = new DataSeries (this);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.


    DiffIndA.Set (IndicatorA[0]);
    ChangeIndB.Set(IndicatorB[0]);
    }


    #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 Plot0
    {
    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 DiffIndA
    {
    get {return diffIndA;}
    }
    [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 ChangeIndB
    {
    get {return changeIndB;}
    }

    #endregion
    }
    }

    #2
    Hello GeorgeW,

    Thanks for your post.

    If I understand correctly you have two values in two indicators that you want to add together in a 3rd indicator.

    To accomplish that you would want to expose those variables (from indicator A & B) so you can read into "C" and output from there.

    Please see http://ninjatrader.com/support/forum...ead.php?t=4991 as a reference on how to expose indicator values that are not plots.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      I have followed the example given in the link and formatted the Initalize method as follows in IndicatorC:

      Add (IndicatorA());
      Add (IndicatorB());

      I am still getting the following errors on both lines:
      CS1501 “No Overload for method …”,
      CS1502 “The best overload match for …”
      CS1503 “Argument 1 cannot convert from indicator to chart line …”

      When I hover the mouse over the arguments, the intellisense shows what should be there and (+1 overload) in both cases. I believe this may be the totals in both the original indicators (diffVol for IndicatorA), neither of which have been made public in properties, but the values they are set to have been made public in properties.

      Indicator A for example has the following lines of code. Am I wrong in thinking that if “myIndicatorA” is made public in properties then “diffVol” which is set to it should also be public? Is there something else I need to do to get it to compile and work?

      Cheers!

      Initalize ()
      myIndicatorA = new DataSeries(this);

      OnBarUpdate ()
      double diffVol;
      myIndicatorA.Set(diffVol);

      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 MyIndicatorA
      {
      get {return myindicatorA;}
      }

      Comment


        #4
        Hello GeorgeW,

        Thanks for your post.

        I'm having trouble trying to follow what you are doing. Can you post the 3 separate scripts you are using and identify what variable you are trying to pass from indicator A to C as well as B to C?

        Alternatively if you would rather not post in public, please do send them into Platformsupport[at]Ninjatrader[dot]com with a subject line of atten Paul and a link to this thread.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Thanks for trying to help, Paul. I have kind of sorted the issue out by copying Script B to Script A, and with a few adjustments that does what I want it to do. I would have preferred to do it by bringing indicators A & B into C, especially as I will need to know how to do it for the future, but following the example from the link does not work for me, and unfortunately I cannot share the scripts.

          Thanks again.

          Comment


            #6
            What is the difference between Adding an indicator to another indicator using the Add() method and just using the hosted indicator as a condition in an if statement in the new host indicator? For example:

            Add (Stochastics (3, 14, 7);
            OR
            if (Stochastics (3, 14, 7).KO[0]<=30)
            {“Take some action here”}

            Additionally, am I right in thinking that where there are parameters for the arguments in brackets (3, 14, 7) in the hosted indicator code, I cannot use those parameters for the arguments in the new host indicator code, and must use actual values, because the parameters have not been defined and made public in the host indicator code?

            Thank you!

            Comment


              #7
              Hello,
              The Add(Indicator) method will add the indicator as a plot. This is limited to use in strategies only though. You could emulate the adding an indicator with the Add(Plot) method by creating a plot and then setting the plot to the indicator value. For example:
              Code:
              protected override void Initialize()
              {
                   Add( new Plot(Color.Blue, PlotStyle.Line, "MyStochasiticsD"));
                   Add( new Plot(Color.Green, PlotStyle.Line, "MyStochasiticsK"));     
              }
              protected override void OnBarUpdate()
              {
                  MyStochasticsD.Set(Stochastics(3, 4, 7).D[0]);
                  MyStochasticsK.Set(Stochastics(3, 4, 7).K[0]);
              }
              
              #region Properties
              
              /// <summary>
              /// </summary>
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries MyStochasticsD
              {
              	get { return Values[0]; }
              }
              
              /// <summary>
              /// </summary>
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries MyStochasticsK
              {
              	get { return Values[1]; }
              }
              #endregion
              Please see the following link on Plots: http://ninjatrader.com/support/helpGuides/nt7/plots.htm

              Calling an indicator as a method in code simply process the indicator and returns a value.
              Calling Stochastics(3, 4, 7).K[0] will not plot the Stochastics K value, but instead will return the K value of 0 bars ago.

              You are correct in that the user defined values would not be made public and could not be called from the new indicator. You could either create the same inputs in the new indicator or expose the user defined inputs in the indicator. For convenience I am providing the link to the reference sample on exposing indicator values: http://ninjatrader.com/support/forum...ead.php?t=4991

              Please let us know if you have any further questions.
              Cody B.NinjaTrader Customer Service

              Comment


                #8
                Thank you CodyB. I think that has made things a little clearer in my mind. I'll know for sure once I've experimented with some indicators.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Mupulen, 04-16-2024, 11:26 AM
                1 response
                54 views
                0 likes
                Last Post techbech  
                Started by Quanto, Today, 02:17 PM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
                24 responses
                126 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by Skifree, Today, 11:21 AM
                3 responses
                11 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by manueldecastro, Today, 01:16 PM
                3 responses
                14 views
                0 likes
                Last Post NinjaTrader_BrandonH  
                Working...
                X