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

Making A Multi Instrument % Spread Indicator

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

    Making A Multi Instrument % Spread Indicator

    I have almost no experience and I am trying to make an indicator that measures % deviation from another instrument. I have read the multi instrument series chapter. I have in my Initialize method:
    Add("XXX",PeriodType.Minute, 1);
    Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Spread"));
    CalculateOnBarClose = true;
    Overlay = true;


    What math logic in my OnBarUpdate will take the percent change from the first bars price? I want the y axis to be percent with 0 as the baseline. For an example, go to google finance and compare 2 stocks...I want this type of graph.

    #2
    vt2009, we can help you out with specific errors and such but due to time and bandwidth limitations, we can't create specific indicators/strategies. If you get something going and you feel like it isn't working how it should, let us know and we'll take a look.

    However, I can give you hints/tips:

    You will need to create two variables that will store the first bar's price. Lets call these values inst0start and inst1start.

    In OnBarUpdate(), when CurrentBar == 0, set those values.

    You can use either Closes[1][0] to obtain the secondary instrument's most recent closing price or in OnBarUpdate() with an if (BarsInProgress == 1) { inst1start = Close[0];}

    For the primary instrument's most recent price, you can use Closes[0][0]. Or the same check as above but with BarsInProgress == 0.

    To get the percent change, you just have to manipulate the current prices verse the price from the start.

    Please let us know if you have any other questions.
    AustinNinjaTrader Customer Service

    Comment


      #3
      protectedoverridevoid OnBarUpdate()
      {

      // Use this method for calculating your indicator values. Assign a value to each
      // plot below by replacing 'Close[0]' with your own formula.
      If CurrentBar == 0;
      int inst0start=Close[0][0];
      int inst1start=Close[1][0];


      }

      Like this? Please be more clear. I only need to know 1 way to do it.

      Comment


        #4
        vt2009, I'm not supposed to do this and do not expect complete scripts from us in the future, but I had this script laying around so here it is:
        Code:
        #region Using declarations
        using System;
        using System.ComponentModel;
        using System.Diagnostics;
        using System.Drawing;
        using System.Drawing.Drawing2D;
        using System.Xml.Serialization;
        using NinjaTrader.Cbi;
        using NinjaTrader.Data;
        using NinjaTrader.Gui.Chart;
        #endregion
        
        // This namespace holds all indicators and is required. Do not change it.
        namespace NinjaTrader.Indicator
        {
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            [Description("Enter the description of your new custom indicator here")]
            public class PercentDifference : Indicator
            {
                #region Variables
                private string secondInstrument = @"TF 06-10"; // Default setting for SecondInstrument
                private double inst0start, inst1start = 0;
                private double inst0chg, inst1chg = 0;
                private double inst0pct, inst1pct = 0;
                private double difference = 0;
                #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(secondInstrument, PeriodType.Minute, 1);
                    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "PercentPlot"));
                    Add(new Line(Color.FromKnownColor(KnownColor.Black), 0, "ZeroLine"));
                    Overlay                = false;
                }
        
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {
                    if (CurrentBar == 0 && BarsInProgress == 1)
                    {
                        inst0start = Closes[0][0];
                        inst1start = Closes[1][0];
                    }
                    inst0chg = Closes[0][0] - inst0start;
                    inst1chg = Closes[1][0] - inst1start;
                    
                    inst0pct = inst0chg / inst0start * 100;
                    inst1pct = inst1chg / inst1start * 100;
                    
                    difference = inst1pct - inst0pct;
                    
                    PercentPlot.Set(difference);
                }
        
                #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 PercentPlot
                {
                    get { return Values[0]; }
                }
        
                [Description("")]
                [Category("Parameters")]
                public string SecondInstrument
                {
                    get { return secondInstrument; }
                    set { secondInstrument = value; }
                }
                #endregion
            }
        }
        Hopefully you'll be able to modify it to suit your needs.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Very Beneficial

          Extremely helpful. The spread I am making uses a logarithmic ratio- here is how I modified the code but for some reason it doesnt want to plot. Any suggestions?

          protectedoverridevoid OnBarUpdate()
          {
          if (CurrentBar == 0 && BarsInProgress == 1)
          {
          inst0start = Closes[
          0][0];
          inst1start = Closes[
          1][0];
          }
          inst0chg = Closes[
          0][0] - inst0start;
          inst1chg = Closes[
          1][0] - inst1start;

          inst0pct = inst0chg / inst0start *
          100;
          inst1pct = inst1chg / inst1start *
          100;

          double LogRatio= Math.Log(inst1chg/inst0chg);

          PercentPlot.Set(LogRatio);

          }

          Comment


            #6
            At the first bar of the OnBarUpdate() you reference one bar back, this will throw an error to the log tab -

            BertrandNinjaTrader Customer Service

            Comment


              #7
              vt2009, you can't take a log of a negative number, which is probably why it isn't plotting. If either inst0chg or inst1chg is negative, that will make the ratio negative, which would give you an imaginary answer.
              AustinNinjaTrader Customer Service

              Comment


                #8
                I am also looking into a multiinstrument indicator and I tried to compile the attached script posted by Austin. The part in the script I am having trouble with is this part:

                protected override void Initialize()
                {
                Add(secondInstrument, PeriodType.Minute, 1);
                ...
                }


                I am getting a compile time error that says "No overload for method 'Add' takes '3' arguments. So I am curious as to how you compiled this indicator? Add only takes a plot or a line as far as I can see. I understand that in strategies, there is an Add() function that takes the three parameters but not in indicators, and I thought this was an indicator that is being talked about?
                Last edited by dennho; 05-24-2010, 01:28 PM.

                Comment


                  #9
                  Hello dennho,

                  Multiseries support has been added to indicators only in version 7. For charting multiseries in version 6.5 you will have to plot from a strategy.

                  Your code should compile in an indicator in version 7. This version is in public beta and you can download at the link below:
                  http://www.ninjatrader.com/webnew/download_trading_software.htm
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Oh I see then. Thanks for the fast reply!

                    Comment


                      #11
                      Hi, New to the forum.

                      Can anyone paste the completed version of this code or PM it to me? Do not have much program knowledge.

                      Comment


                        #12
                        Used code above and it has compiled. How do I use it as an indicator now? It isn't showing up in the indicator list and Ive saved it.

                        Comment


                          #13
                          crmtrade, just to double check, you've hit the F5 key to compile it and received no errors? After that, when you're trying to apply it to a chart, it is not appearing in the indicator list? Could you try restarting NinjaTrader? The indicator would be called PercentDifference in the indicator list.
                          AustinNinjaTrader Customer Service

                          Comment


                            #14
                            Alright have it going. Now is it possible to have the chart display % change like google finance? That way I can see the percentage difference between the 2 % change numbers.

                            Comment


                              #15
                              crmtrade, you can just apply the indicator twice and pick two different instruments in the indicator settings.
                              AustinNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Stanfillirenfro, Today, 07:23 AM
                              9 responses
                              23 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by George21, Today, 10:07 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post George21  
                              Started by DayTradingDEMON, Today, 09:28 AM
                              2 responses
                              18 views
                              0 likes
                              Last Post DayTradingDEMON  
                              Started by navyguy06, Today, 09:28 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              8 responses
                              32 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X