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

how to plot the difference of two lines on the chart

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

    how to plot the difference of two lines on the chart

    Hello,
    could you please provide me with some hints on how I could build (or where I could find, if it already exists somewhere) an indicator calculating and possibly plotting on a histogram, the difference between two lines.
    For example the difference between :
    the values of two different indicators (for ex RSI(period=n) and RSI(period=m) )
    derived from the same data series (for ex ES(min=5)) or even from two different data series (for ex ES(min=5) for RSI(n) and ES(min=30) for RSI(m)).
    I thought that I saw some time ago a post regarding a modified MACD accepting such different inputs data series (lines), but I haven't been able to find it again.
    Thank you
    Guido Tosi.

    #2
    Hello guidoisot, and thank you for your question.

    In order to create the indicator you are describing, you can follow these steps :

    • NT7 Control Center -> Tools -> Edit NinjaScript -> Indicator -> MACD -> OK
    • Right-click in the editor -> Save as -> Give it a new name -> OK
    • OK the dialog that comes up
    • Add any custom data series, such as the 5 and 30 minute E-mini S&P series you mentioned, using this documentation

      http://ninjatrader.com/support/helpGuides/nt7/add3.htm
      • Add code at the beginning of OnBarUpdate to make sure that you have enough bars of every type that you added before you attempt to compare
    • Modify the Diff series setting at the bottom to reflect what you would actually like to plot, for example, the difference between two RSI values, one with a period m reflecting the 5 minute ES, and one with a period n reflecting the 30 minute ES.

    I have attached an indicator script to this post where I have done all of the above for reference. Please let us know if there are any other ways we can help.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hello
      thank you for your help.
      Before opening the attached .cs example, I have been trying to follow the suggestions on the text of your email, but I could not go ahead from the point shown on my .cs , attached here.
      Now that I see your example I will try to follow and understand it (... tomorrow ... ).
      In the meantime, if you happen to have a couple of spare minutes, please let me know about the main mistakes I have been doing and have blocked me in my attempt.
      Thanks. Best
      guidoisot.
      Attached Files

      Comment


        #4
        Hello guidoisot,

        One thing I did notice is that you have added a data series correctly in Initialize, but you are missing this code at the beginning of your OnBarUpdate.

        Code:
        [FONT=Courier New]            foreach (int CurrentBarForSeries in CurrentBars)
                    {
                        if (CurrentBarForSeries < 1)
                        {
                            return;
                        }
                    }
                    if (BarsInProgress != 0)
                    {
                        return;
                    }[/FONT]
        In plain language, what this code does is two things :
        • It makes sure every data series has at least one bar available, and
        • It makes sure OnBarUpdate was triggered by your primary series

        I also noticed toward the end, that you did not attempt to take a difference between e.g. two RSI values; the code at the bottom is essentially the same as the MACD indicator.


        We are happy to help with any additional questions that come up
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Hello Jessica,
          thank you for your reply.
          I have been trying to implement your hints and also by looking to the sample I have come up with the attached code. I would be grateful if you could help on how to correct its mistakes (attached is also an image of the compiler feedback) and how you would go in order to generalize its inputs not only for RSIs but for two generic plots, say:, "line01" and "line02".

          I have also embedded in the code some comments/questions near some items I would like to have a better understanding, perhaps trying to catch what they mean in “plain language”.
          Thank you.
          Best regards,
          guidoisot
          Attached Files

          Comment


            #6
            Hello guidoisot,

            I am including an annotated copy of your script, with my initials and the date and time I began commenting preceding every comment. Briefly, a lot of code in this script is left over "cruft" from your MACD code, and can be deleted. However, I have left this code in the indicator along with explanations as to its function for educational purposes. I have also left some code which shows a generalization approach you can take in this indicator. I would like to leave removing the MACD "cruft" from this indicator to you, as well as modifying this so that its two data series can take any instrument and period you would prefer. When you are are ready to generalize further, please let me know, and I can show you a method for selecting multiple bar types for your data series.
            Attached Files
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Hello Jessica,
              Thank you for all the help you have given me on this scripting exercise.
              I have modified, compiled and plotted on a chart this new indicator "Distance". It really looked great and it gave me that good perception/hope it will be providing useful trading info.
              I first applied this indi "Distance" to a NQ chart, but I realized only later on that it was, (most likely, as far as I can understand) doing its calculation on the ES and not on the NQ, the primary instrument .
              I then tried to modify it in order to have it using by default the primary instrument also for the secondary series (which however at this stage should not be used, since the "distance" is calculated just between two oscillators (two RSIs), for the same instrument (the primary one), but with two different periods, (do you agree? I am not so sure about all this). However after this modification, aiming to having the oscillators (the two RSIs) calculated on the NQ, the indi does not appear anymore on the chart. I do not understand where the mistake is.
              While exploring this indicator I was wondering whether it could potentially be useful also for calculating the distance between two instruments, for example between any couple taken from ZT, ZF, ZN, ZB and UB. If so I think it could perhaps measure the reciprocal relative strengths of the various spreads between the various maturities of the yield curve ... (but I guess this is .... way up the road, from where I am right now).
              Thanks again.
              Best regards.
              guidoisot

              ps my last text comments I have inserted into the code (attached) are signalled by long empty underscores
              Attached Files
              Last edited by guidoisot; 08-24-2016, 01:02 AM.

              Comment


                #8
                Hello guidoisot,

                I am glad we were able to get close to a working indicator. It looks like I could have done a better job explaining a couple concepts.

                First, in one of my comments, I misunderstood your meaning, and told you you could get rid of 2 lines of code which create data series, but I did not mention which ones explicitly. I have gone ahead and removed the 2 indicators I was referring to, and restored the one you had commented out.

                We can tell that we need the series you commented out, because its name is Diff, and we can see at the bottom that it is Diff we are setting to the output of your 2 RSI indicators.

                Second, the reason I went ahead and removed those two data series is so I could demonstrate how this affects your Values[] series. Toward the bottom, in your Properties series, where we configure the Diff series, I left an educational note. Please review this note.

                To make the items I'd like to address this time clear, I have removed all the old MACD code and my previous comments. All my comments in the code this time pertain to this post.

                Please let us know if there are any other questions we can answer.
                Attached Files
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Jessica,
                  thank you for your reply.
                  Working on this “Distance” sample script has really been a great learning exercise for me. I hope some other forum users, also trying to learn some basics on NT scripting, may benefit from reading this thread as well. I noticed that there are some input-parameters-selection issues that limit the flexibility of this “Distance” script:
                  it does not appear possible to apply an indicator to this indicator (in the attached image I have tried with MAX(50) but I did not find the suitable input fields on the parameters window of “Distance”);
                  I found a similar issue when I tried to change the period of the secondary data series: it was 30 min., I wished to change it down to 15 min., the only way I could do that was by opening the script text and replacing 30 min. with 15 min.. However, once I did that, I realized that the histogram was not painting anymore the actual distances between the two RSI;
                  one last feature missing, I think, is an input form of “Distance” from where one can select the oscillator/indicator/instrument on which “Distance” should work on; so far in our example we have been referring to RSI, but how could one tell “Distance” to do the same calculation/plots for another oscillator/indicator/instrument? For ex using RSX, or d9ParticleOscillator, …. without having to open each time its script, modify it manually, compiling it , checking it is doing ok, etc …. etc…..
                  I attach a couple of screenshots, and the text of the script (I tentatively modified down to 15 min instead of 30 min) with some new comments referred to what I have been trying to describe here on this post.
                  Thanks again.
                  Guidoisot.
                  Attached Files
                  Last edited by guidoisot; 08-26-2016, 04:50 AM.

                  Comment


                    #10
                    Hello Jessica,
                    please find attached a screenshot showing an example of the “Distance” indicator not applied for comparing two RSIs, but rather two d9ParticleOscillators:
                    on panel 2 is the first d9PO for the 5 min timeframe with (period=19; phase=0);
                    on panel 3 is the 2nd d9po for the 6 min timeframe with (period=20; phase=0);
                    The plot of this “Distance” is on panel 1 overlapped to the price.
                    The vertical green and red lines are when “Distance” crosses the zero horizontal line.
                    It looks really great. Through these first tests I notice that in addition to the issues described on previous post, it is not possible to use two different periods (19 and 20 in this example) on the same 5 min timeframe, i.e. only on the primary data series. In your opinion is there a way to modify the script so that this indicator "distance" does not necessarily require two distinct data series, but can work on two oscillators just with different parameters, but on the same timeframe, i.e. just on the primary data series.
                    I hope I am not asking too many questions and help, however it looks like we are almost near the final stage with completing this interesting script.
                    Thank you.
                    Best.
                    Attached Files

                    Comment


                      #11
                      Hello guidoisot,

                      You are definitely not asking too many questions. We are always happy to help, this is one of my favorite parts of this job.

                      However I will need to get back to you on Monday, after having some time to review the information you have sent me. I appreciate your patience as we work together to put together this strategy.
                      Jessica P.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Jessica
                        Great to hear that. It really looks like step by step this indi is getting better and better. Let me attach the .cs I have used for the Distance screenshot_3_.jpg I have posted earlier.
                        Thank you.
                        Attached Files

                        Comment


                          #13
                          Hello guidoisot,

                          First I would like to apologize for the delay getting back to you.

                          The best way to set up an instrument with multiple time frames on the same instrument, is to have 2 period properties for input periods, and then to use these in the 2 argument version of the Add method. These will have to be the same interval type, so let's make these as basic as possible. We'll make both intervals seconds.

                          Since we will be using seconds, this handy chart will make it easier to extend this to other intervals such as minutes, days, and years.

                          • 1 minute = 60 seconds
                          • 1 hour = 3600 seconds
                          • 1 day = 86400 seconds
                          • 1 week = 604800 seconds
                          • 1 month (30 days) = 2592000 seconds
                          • 1 year = 31557600 seconds


                          The code to set up 2 properties looks like this, bolded lines are new :

                          Code:
                          [FONT=Courier New]
                            public class Distance : Indicator
                            {
                              #region Variables[SIZE=3][B]
                              private int secondsForFirstSeries;
                              private int secondsForSecondSeries;[/B][/SIZE]
                              private int          m  = 19; //previous fast
                              private int          n  = 20; //previous slow[/FONT]
                          Then near the bottom,

                          Code:
                          [FONT=Courier New]
                              [Browsable(false)]
                              [XmlIgnore()]
                              public DataSeries Diff
                              {
                                get { return Values[/* JDP 11:49:58 MDT 2016 this is no longer data series 2, this is 0, please ask if it is not obvious why*/ 0 ]; }
                              }[/FONT][SIZE=3][B][FONT=Courier New]
                          
                              /// <summary>
                              /// [/FONT][FONT=Courier New][FONT=Courier New]Number of seconds for first interval
                          [/FONT]    /// </summary>
                              [Description("Number of seconds for first interval")]
                              [GridCategory("Parameters")]
                              public int SecondsForFirstSeries
                              {
                                get { return secondsForFirstSeries; }
                                set { secondsForFirstSeries  = Math.Max(1, value); }
                              }[/FONT][FONT=Courier New]
                          
                              /// <summary>
                              /// [/FONT][/B][/SIZE][FONT=Courier New][SIZE=3][B][FONT=Courier New]Number of seconds for second interval
                          [/FONT]    /// </summary>
                              [Description("Number of seconds for second interval")]
                              [GridCategory("Parameters")]
                              public int SecondsForSecondSeries
                              {
                                get { return secondsForSecondSeries; }
                                set { secondsForSecondSeries  = Math.Max(1, value); }
                              }[/B][/SIZE]
                          
                              /// <summary>
                              /// </summary>
                              [Description("Number of bars for fast oscillator, - or line01")]
                              [GridCategory("Parameters")]
                              public int M
                              {
                                get { return m; }
                                set { m  = Math.Max(1, value); }
                              }[/FONT]
                          Try adding that first. Then try loading up your indicator in a chart. If you see the two new parameters in the properties window, we have done this step successfully. If you can't see these two properties, please let me know.

                          Next let's actually create our data series. That is done by changing this ...

                          Code:
                          [FONT=Courier New]
                                Add(PeriodType.Minute, 5); //is it possible to select per each additional data series: the type of bar and its period
                                                           //from a parametrs input form of the indicator? ++++++++++++++++++++++++++++++++++++
                                Add(PeriodType.Minute, 6);[/FONT]
                          ... to this

                          Code:
                          [FONT=Courier New]
                                Add(PeriodType.[B][SIZE=3]Second[/SIZE][/B], [SIZE=3][B]secondsForFirstSeries[/B][/SIZE]); //is it possible to select per each additional data series: the type of bar and its period
                                                           //from a parametrs input form of the indicator? ++++++++++++++++++++++++++++++++++++
                                Add(PeriodType.[SIZE=3][B]Second[/B][/SIZE], [B][SIZE=3]secondsForSecondSeries[/SIZE][/B]);[/FONT]
                          We can set "secondsForFirstSeries" and "secondsForSecondSeries" to 300 and 360, respectively, to get our original series values.

                          Please try this out, and please let us know if any other questions come up.
                          Jessica P.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Jessica,
                            thank you for your reply.
                            I have updated the indicator Distance and now it looks much better. It has some issues such as it does not follow changes in the timeframe of the primary data series of the chart and it appears that it cannot be used by another indicator. For example I tried to plot its Donchian or its Min and Max levels for a given nr of periods, say 50 bars, but I could not insert Distance as the argument of neither Donchian nor Min nor Max.
                            I have done some testing of how it works in a simple strategy (text attached here below) but it appears it needs to be inserted into other indicators in order to be better exploited.
                            I was wondering whether this Distance indicator could be easier to obtain in NT8? Perhaps there are some new feature in NT8 that allow that. In any case from now on, I am going to try to explore and to familiarize more with NT8, especially with its strategy builder which I understand should be more powerful and flexible.
                            Please could you let me know whether you are aware of any educational videos or examples showing how to use the strategy builder on NT8 ?
                            Thank you best regards.
                            Guidoisot.
                            Attached Files

                            Comment


                              #15
                              Hello again guidoisot,

                              While it's true that your distance indicator relies on L2 data and not price data as most indicators use, it should still be working inside indicators like MAX. Would it be possible for you to provide a code sample where you are attempting to do this?

                              We have several excellent training videos on our youtube channel here,



                              Of interest to you will be the Strategy Wizard 301 video here,

                              Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.


                              I am looking forward to assisting further
                              Jessica P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              27 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 04-23-2024, 09:53 PM
                              2 responses
                              49 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              193 views
                              0 likes
                              Last Post Hasadafa  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,235 views
                              0 likes
                              Last Post xiinteractive  
                              Working...
                              X