Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ES divided by SILVER

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

    ES divided by SILVER

    L.S.,

    I am looking for an indicator dividing the ES price by the SILVER price. I guess a multi frame indicator is needed by have no idea to start any help is welcome.

    cheers,
    Erik

    #2
    Originally posted by pingpong View Post
    L.S.,

    I am looking for an indicator dividing the ES price by the SILVER price. I guess a multi frame indicator is needed by have no idea to start any help is welcome.

    cheers,
    Erik
    Hi Erik,

    Well, an multi time frame indicator can be used, however, if you want to divide the price of the ES by the Silver price, one time frame will be sufficient. You will need to declare multiple dataseries which hold the prices of the ES and Silver (i.e. adding an instrument in Initialize()).

    In pseudocode you get something like this:

    Initialize()
    {
    Add(ES, PeriodType.Day);
    Add(Silver, PeriodType.Day);
    }
    OnBarUpdate()
    {
    Plot0.Set(Closes[0][0] / Closes[1][0]);
    }

    for when you want to divide the ES daily closing price by the daily closing price of Silver. (Does this give you an good idea about how to start?)

    Good luck!

    Regards,

    Comment


      #3
      pingpong, there is a pairs trading indicator suite available for download in the file sharing section, but J_o_s' suggestion is equally applicable.
      AustinNinjaTrader Customer Service

      Comment


        #4
        Thx Jos,

        My code is now

        protected override void Initialize()
        {
        Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
        Overlay = false;

        Add("ES 12-10", PeriodType.Day,1);
        Add("SLV", PeriodType.Day,1);

        }

        /// <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.
        //Plot0.Set(Close[0]);

        Print ("Close (ES): " + Closes[0][0]);
        Print ("Close (SLV): " + Closes[1][0]);

        Plot0.Set(Closes[0][0] / Closes[1][0]);


        }
        The result is 1. ES and SLV are having the same value. Which looks not oke to me.

        cheers,
        Erik

        Comment


          #5
          Erik, what prices are being printed out by your print statements? Do they represent the correct values for SLV and ES?
          AustinNinjaTrader Customer Service

          Comment


            #6
            No the prices printed out are the ones from ES.

            Comment


              #7
              Hi Erik,
              Originally posted by pingpong View Post
              No the prices printed out are the ones from ES.
              Correct, the way I mentioned doesn’t work (sorry for that). It was a little bit tricky to figure out (so please check the code below for yourself), but I think this solves your problem:
              Code:
                        protected override void Initialize()
                        {
                            Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                            Overlay                                                   = false;
                                                               
                                                               Add("AH",PeriodType.Day,1);
                                                               Add("RDS",PeriodType.Day,1);
                                                               
                                                               CalculateOnBarClose = true;
                        }
                 
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                 
                            //if (CurrentBar < 2)
                            //    return;
                 
                 
                 
                            Print("Closing price AH: " + Closes[1][0]  + " or, alternatively: " + BarsArray[1].GetClose(CurrentBar));
                            Print("Closing price RDS: " + Closes[2][0] + " or, alternatively: " + BarsArray[2].GetClose(CurrentBar));
                 
                            Print("BarsArray[0]: " + BarsArray[0].Instrument.FullName.ToString());
                            Print("BarsArray[1]: " + BarsArray[1].Instrument.FullName.ToString());
                            Print("BarsArray[2]: " + BarsArray[2].Instrument.FullName.ToString());
                 
                            //Plot0.Set(Closes[1][0] / Closes[2][0]);
                            // OR:
                            Plot0.Set(BarsArray[1].GetClose(CurrentBar) / BarsArray[2].GetClose(CurrentBar));
                                                               
                        }
              In this code I calculated the ratio between AH and RDS, two stocks. (I don’t have Silver closing prices here).

              Please note that there a 3 different dataseries: the two instruments which compose the ratio (the stocks AH and RDS), and the instrument which you have plotted (for example TOM2)). I hope the Output Window will make this somewhat clearer, for example:

              When I plot DSM stock, the output window reads:
              Code:
              Closing price AH: 10,36 or, alternatively: 10,36
                Closing price RDS: 23,45 or, alternatively: 23,45
                BarsArray[0]: DSM
                BarsArray[1]: AH
                BarsArray[2]: RDS
              This extra BarsArray is needed (as far as I can tell), because the method I mentioned earlier works only well when you plot one of the instruments of the ratio (i.e. RDS or AH). In this little workaround, the ratio is also plotted correctly when you watch an entirely different instrument: in that case you still get the ratio of AH divided by RDS.

              Also see the attachments.

              Btw: the...
              Code:
              //Plot0.Set(Closes[1][0] / Closes[2][0]);
                            // OR:
                            Plot0.Set(BarsArray[1].GetClose(CurrentBar) / BarsArray[2].GetClose(CurrentBar));
              section is a little bit redundant, and both statements give the same result (as far as I can tell).

              Regards,
              Attached Files
              Last edited by J_o_s; 11-08-2010, 02:58 AM.

              Comment


                #8
                Thx Jose,

                One step further. I am getting the following error which relates to the SLV data serie

                Closing (ES 12-10): 1072,5 or, alternatively: 1072,5
                Error on calling 'OnBarUpdate' method for indicator 'Pairs' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                using the following code:
                /// <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 = false;

                Add("ES 12-10", PeriodType.Day,1);
                Add("SLV", PeriodType.Day,1);

                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.
                //Plot0.Set(Close[0]);

                Print("Closing (ES 12-10): " + Closes[1][0] + " or, alternatively: " + BarsArray[1].GetClose(CurrentBar));
                Print("Closing (SLV): " + Closes[2][0] + " or, alternatively: " + BarsArray[2].GetClose(CurrentBar));

                Print("BarsArray[0]: " + BarsArray[0].Instrument.FullName.ToString()); // Primary data serie
                Print("BarsArray[1]: " + BarsArray[1].Instrument.FullName.ToString());
                Print("BarsArray[2]: " + BarsArray[2].Instrument.FullName.ToString());

                //Plot0.Set(Closes[1][0] / Closes[2][0]);
                // OR:
                Plot0.Set(BarsArray[1].GetClose(CurrentBar) / BarsArray[2].GetClose(CurrentBar));
                }
                Is it maybe related to the different trading hours of both instruments?

                Comment


                  #9
                  Originally posted by pingpong View Post
                  Thx Jose,

                  One step further. I am getting the following error which relates to the SLV data serie

                  Closing (ES 12-10): 1072,5 or, alternatively: 1072,5
                  Error on calling 'OnBarUpdate' method for indicator 'Pairs' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.



                  Is it maybe related to the different trading hours of both instruments?
                  Hi Erik,

                  Good question. I don't think it should matter much (perhaps Ninja Support can shed some light on this) if you use daily closing prices. What timeframe are you using? I guess it would be possible to recalculate the indicator based on the different time zones. I.e. if you are watching the 30 minute time frame, then perhaps instrument 1 needs to add 2 hour to its bar time. Now I come to think of it, aren’t the ES and Silver both trading in the same timezone?

                  By the way, I encountered the same error as you just now. In my case this was caused by insufficient historical data from one of the instruments. Do you have historical data for both of the instruments for the time period on which you set your chart? Do you still get the error if you set your chart to a much shorter timeframe of which you are sure you have enough data?

                  Regards,

                  Comment


                    #10
                    Thx again Jos,

                    For both instruments I have 365 days loaded on a daily timeframe. But I am not sure it will impact the Add(..) statement.

                    Lets see Ninja Support will pick it up.

                    cheers,
                    erik

                    Comment


                      #11
                      The Add() would pick up what you loaded for the primary series - have you tried adding a CurrentBars check for both series at the OnBarUpdate() start?

                      Comment


                        #12
                        Getting a bit more information on the bug.

                        Output window:
                        Closing (ES 12-10): 1197,75 or, alternatively: 1175
                        Closing (SLV): 24,24 or, alternatively: 24,24
                        BarsArray[0]: ES 12-10
                        BarsArray[1]: ES 12-10
                        BarsArray[2]: SLV
                        255 255 244
                        Closing (ES 12-10): 1218,5 or, alternatively: 1218,5
                        Closing (SLV): 24,24 or, alternatively: 23,42
                        BarsArray[0]: ES 12-10
                        BarsArray[1]: ES 12-10
                        BarsArray[2]: SLV
                        255 255 244
                        Closing (ES 12-10): 1218,5 or, alternatively: 1218,5
                        Closing (SLV): 24,24 or, alternatively: 23,42
                        BarsArray[0]: ES 12-10
                        BarsArray[1]: ES 12-10
                        BarsArray[2]: SLV
                        255 255 245
                        Closing (ES 12-10): 1218,5 or, alternatively: 1175,75
                        Closing (SLV): 25,62 or, alternatively: 25,62
                        BarsArray[0]: ES 12-10
                        BarsArray[1]: ES 12-10
                        BarsArray[2]: SLV
                        256 256 245
                        Closing (ES 12-10): 1222,25 or, alternatively: 1222,25
                        Error on calling 'OnBarUpdate' method for indicator 'Pairs' on bar 256: Object reference not set to an instance of an object.
                        I added the following lines on top of the OnBarUpdate()

                        Print (CurrentBars[0] + " " + CurrentBars[1] + " " + CurrentBars[2]);

                        if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired)
                        return;

                        Comment


                          #13
                          Please try without the BarsArray.GetClose reference, this would unfortunately not be supported NinjaScript.

                          Comment


                            #14
                            thx, code is now working.

                            protected override void Initialize()
                            {
                            Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                            Overlay = false;

                            Add("ES 12-10", PeriodType.Day,1);
                            Add("SLV", PeriodType.Day,1);

                            CalculateOnBarClose = true;
                            }

                            /// <summary>
                            /// Called on each bar update event (incoming tick)
                            /// </summary>
                            protected override void OnBarUpdate()
                            {

                            if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired)
                            return;

                            Print("Closing (ES 12-10): " + Closes[1][0]);
                            Print("Closing (SLV): "+ Closes[2][0]);

                            //Print("BarsArray[0]: " + BarsArray[0].Instrument.FullName.ToString()); // Primary data serie
                            Print("BarsArray[1]: " + BarsArray[1].Instrument.FullName.ToString());
                            Print("BarsArray[2]: " + BarsArray[2].Instrument.FullName.ToString());

                            Plot0.Set(Closes[1][0] / Closes[2][0]);
                            }

                            Comment


                              #15
                              Hello,

                              Excellent glad you got it working!

                              Let me know if I can be of further assistance.
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              571 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              331 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              549 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              549 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X