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

    #16
    Hello Jessica,
    thanks for your links to the videos.
    Yes this "Distance" indicator works inside the MAX indicator. Initially it did not update on the chart. However when I restart the platform (or when I reload all scripts) Max of ("Distance") shows up. Thanks.
    guidoisot

    Comment


      #17
      Hello Jessica,
      thanks again for your guidance for the “Distance” indicator.
      Please can you help on the following:
      - I have replaced the RSI with the TSI indicator. I think I did most of what was needed, but I cannot find out how to put in the settings of the indicator options for changing its color and thickness on the chart.
      - TSI uses two parameters, can I use the field of the smooth parameter which is already included in the script (idle ?) as the input field of the second parameter for TSI?
      - Do you know whether here on the forum it is available an indicator that paints the chart background depending on the slope of a line (either indicator or instrument), for example of the two DistanceTSI” indicator shown on the attached chart?
      - If not would it be something difficult to insert in the script such plotting option?
      Thanks.
      Attached Files

      Comment


        #18
        I am glad we are making progress toward completing this strategy. Let's see if we can address these one at a time.

        ...how to put in the settings of the indicator options for changing its color and thickness on the chart
        For this sort of thing generally, as we did with seconds for time series earlier, we start by looking at what your code needs. Let's copy a line from your text file.

        Code:
        [FONT=Courier New]      Add(new Plot(new Pen(Color.Navy, 2), PlotStyle.Line, "Diff"));[/FONT]
        So we can specify :
        • Color
        • Weight
        • PlotStyle
        • A label

        Next we look at what our options are for properties. If we go to the NT8 Control Center, select New -> NinjaScript Editor, right click Indicators and select New Indicator, go to the Input Parameters tab, and press add, in the type pulldown, we see these options


        • bool
        • double
        • string
        • int
        • time
        • Brush

        Brush, as we will see in some documentation I supply, will let you set colors. So we just need a Brush type parameter. We can copy and modify some of your existing code to make this happen. Let's copy from this property...


        Code:
        [FONT=Courier New]    /// <summary>
            /// </summary>
            [Description("Number of bars for smoothing")]
            [GridCategory("Parameters")]
            public int Smooth
            {
              get { return smooth; }
              set { smooth = Math.Max(1, value); }
            }[/FONT]
        We can see that that returns an int. So let's make this one return a Brush. Let's call this MyPlotColor.



        Code:
        [FONT=Courier New]    /// <summary>
            /// </summary>
            [Description("Color for my plot")]
            [GridCategory("Parameters")]
            public [B]Brush MyPlotColor[/B]
            {
              get { return [B]myPlotColor[/B]; }
              set { [B]myPlotColor [/B]= [B]value[/B]; }
            }[/FONT]
        So that takes care of color, let's take care of weight. That's easy since it's just an integer.

        Code:
        [FONT=Courier New]    /// <summary>
            /// </summary>
            [Description("[B]Line weight for my plot[/B]")]
            [GridCategory("Parameters")]
            public int [B]MyPlotWeight[/B]
            {
              get { return [B]myPlotWeight[/B]; }
              set { [B]myPlotWeight [/B]= Math.Max(1, value); }
            }[/FONT]
        Now since we've copied and modified "smooth" and "Smooth" we should look for other places it shows up in your code. It looks like smooth is defined earlier.

        Code:
        [FONT=Courier New]
            #region Variables
            // ...[/FONT][FONT=Courier New]
            private int          smooth  = 9;[/FONT]
        I added the // ... to skip over lines we aren't interested in. So just below that in our code let's define our new variables.

        Code:
        [FONT=Courier New]
            private int          smooth  = 9;[B]
            private Brush        myPlotColor = Brushes.Navy;
            private int          myPlotWeight = 2;[/B]
        [/FONT]
        I decided to stick with the default color and weight we already had. Brushes.Navy by the way I got from the publicly available documentation here,



        Now, finally, we can just modify that first line of code to use these.

        Code:
        [FONT=Courier New]      Add(new Plot(new Pen([B]myPlotColor.Color, myPlotWeight[/B]), PlotStyle.Line, "Diff"));[/FONT]
        I found out about Brush.Color from this publicly available MSDN documentation,



        I have gone ahead and added this to the script you gave me, and attached it.

        TSI uses two parameters, can I use the field of the smooth parameter which is already included in the script (idle ?) as the input field of the second parameter for TSI?
        You certainly can. It's your parameter to do with what you wish. I would recommend, though, using a differently named parameter you choose a name for and understand fully. If you are having trouble choosing a name, may I suggest using the one that's in the documentation for TSI.

        Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/true_strength_index_tsi.htm
        TSI(int fast, int slow)[int barsAgo]
        As we can see, the TSI has two periods, a fast and a slow period. Since you already have a parameter named slow, I would like to suggest slowTSI.

        Do you know whether here on the forum it is available an indicator that paints the chart background depending on the slope of a line
        We have something more powerful than indicators. We have methods and math I am including a quote from the Slope method's documentation.

        Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/slope.htm?zoom_highlightsub=slope
        Slope(ISeries<double> series, int startBarsAgo, int endBarsAgo)

        Tip: Thinking in degrees, for example a 1 to -1 return range would translate to 45 to -45. To convert you could look into working with this formula - Math.Atan(Slope) * 180 / Math.PI
        So we know that this method takes a line (every series is a line), and a time range, and gives us a slope. That's half of what we want, now we just need to set background color.

        We saw earlier that many methods in NinjaTrader contain Color objects. I would like then to include some more publicly available MSDN documentation for Color objects that is relevant to us.

        Originally posted by https://msdn.microsoft.com/en-us/library/system.drawing.color.aspx
        FromArgb(Int32, Int32, Int32)

        Creates a Color structure from the specified 8-bit color values (red, green, and blue). The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to 8 bits.
        One trick I know from working with RGB (red, green, blue) colors, is that if all three values are the same, you will get shades of grey from white to black. So all we need to do is turn your slope - a number from -1 to 1, and turn it into a number between 0 and 255. And we now have a color that is based on slope. That is

        Code:
        [FONT=Courier New]double mySlope = Slope(yourLine, 0 /* start bars ago */, 3 /* end bars ago */);
        int myShade = (int) ((mySlope + 1) * 127.5); // (-1 to 1) + 1 = (0 to 2), (0 to 2) * 127.5 = (0 to 255)
        Color myBackgroundColor = Color.FromArgb(myShade, myShade, myShade);[/FONT]
        Now we just need to apply this background color. Using this documentation,



        and the earlier documentation for SolidColorBrush, we get

        Code:
        BackBrush = new SolidColorBrush(myBackgroundColor);
        Please let us know if there are any other ways we can help.
        Attached Files
        Last edited by NinjaTrader_JessicaP; 09-16-2016, 09:37 AM.
        Jessica P.NinjaTrader Customer Service

        Comment


          #19
          Hello Jessica,
          thank you for your reply. I had a quick overview to your post, I am not sure I can understand all of its content, but I will try.
          I would like to ask your confirmation on the paragraph where you are referring to NT8
          Originally posted by NinjaTrader_JessicaP View Post
          If we go to the NT8 Control Center, select New -> NinjaScript Editor, right click Indicators and select New Indicator,
          is this correct? did you use NT8 editor in order to extract a script that would not be available in NT7? I just wanted to make sure before going ahead that this is the reason you are referring to NT8.
          Thanks.

          Comment


            #20
            I apologize, you are correct. Instead of exposing a brush, you will need to expose three integers, MyPlotColorRed, MyPlotColorGreen, and MyPlotColorBlue, and then use the provided instructions for Color.FromArgb that I provided.

            Please ask any questions that come up, we have hit upon a complicated topic, but I believe this is the last hurdle between us and having a working script.
            Jessica P.NinjaTrader Customer Service

            Comment


              #21
              Hello Jessica,
              no need for apologies. Maybe this was a "subconscious sign" from an expert in the subject as you are that we should shift to NT8.

              Unfortunately I have not yet been able to include your last tips on the script, partly because C# is still kind of guesswork for me and also because I have too many things to take care of that I am very short in time for everything.
              However, since you have referred to NT8 and it appears that this thread has increasing hits, I was wondering, (if you still have some more time available for this “Distance” indi), wouldn’t it be interesting and of sufficient general interest translating this indicator from NT7 to NT8? This would help me to get familiar with NT8 and it would also be a good example/comparison of how easier is to implement this kind of conditions with the new NT8 vs NT7.
              Thank you.
              guidoisot.
              Last edited by guidoisot; 09-19-2016, 02:27 AM.

              Comment


                #22
                If you are interested in converting a NinjaTrader 7 script to NinjaTrader 8, I highly encourage you to review this thread, as it contains a lot of valuable information and discussion on this topic.

                Jessica P.NinjaTrader Customer Service

                Comment


                  #23
                  Originally posted by guidoisot View Post
                  Unfortunately I have not yet been able to include your last tips on the script, partly because C# is still kind of guesswork for me and also because I have too many things to take care of that I am very short in time for everything.
                  However, since you have referred to NT8 and it appears that this thread has increasing hits, I was wondering, (if you still have some more time available for this “Distance” indi), wouldn’t it be interesting and of sufficient general interest translating this indicator from NT7 to NT8? This would help me to get familiar with NT8 and it would also be a good example/comparison of how easier is to implement this kind of conditions with the new NT8 vs NT7.
                  Thank you.
                  guidoisot.
                  Any community member may perhaps offer his/her contribution for implementing these last changes to this custom indicator? I have tried to do it, but it is out of reach for me.
                  @stalt I noticed your similar “Bollinger % upper and lower to close”
                  @ http://ninjatrader.com/support/forum...55&postcount=3
                  Maybe you can, kindly, do something and share?
                  Thanks.

                  Comment


                    #24
                    Hello Jessica,
                    I am trying to spot when (for now regardless on whether the curve is flattening or steepening) two different maturities in the yield curve (for ex ZB and ZN) are reciprocally positioned opposite to their respective moving averages. If ZB is above its SMA(n) and ZN is below its SMA(n), then enter short 1 contract ZB and immediately entry long 2 contracts ZN, (as 1 ZB tick is 2 ZN ticks). One way to spot these instances I think it could be measuring the distance between the two corresponding ZScore indicators derived from ZB and ZN. I have tried to manually execute such simulated trades based on this “rule”.
                    I am not sure whether this approach could work better on very short timeframes (say 75 ticks for ZB, and 150 tick for ZN, or 5 sec for both instruments, or .... ), or whether it would be better to apply it to much longer timeframes (say 60 min for both ZB and ZN). Another parameter to be evaluated is the ZScore period tu use, I would like to backtest in order to find out whether it would be better a short period (say 4 bars) or a much longer period (say 40 bars).
                    However I found some other issues to-be-solved-first:
                    1 how can I have both orders being executed simultaneously; when I tried to execute them manually on the DOMs it wasn’t easy being fast enough and I also I to use market orders with slippage;
                    2 how could I backtest this method? For example would it be possible to use the market replay feature when there are two different instruments running in parallel (in this example ZB and ZN)
                    3 How can I use the “Distance” indicator, we have already been working on, applied to two different instruments? One approach I was thinking to try was spotting when the difference between the 2 ZScore indicators (one applied to ZB, the other to ZN) was at either positive or negative extreme values, say calculated for the last 50 periods, or n periods… but I cannot modify the script of Distance for this goal.

                    Please can you (or any other forum member) provide any help on this?
                    Thanks
                    Attached Files
                    Last edited by guidoisot; 10-02-2016, 04:08 AM.

                    Comment


                      #25
                      Hello guidosot,

                      I would like to ask, were you able to receive information on obtaining the help of a certified NinjaScript consultant via e-mail? They should be able to help you directly as far as the correct operation of your script.

                      I would be happy to answer any further questions about NinjaTrader and NinjaScript that come up. With this in mind I will provide the best answers I may to your three questions.

                      1. If you are asking about hedging, this is something that may be restricted by your data feed provider. Even if it is not, this is only possible with NinjaScript through the unmanaged order entry approach, documented here,

                        http://ninjatrader.com/support/helpG...d_approach.htm
                      2. The best way to test an indicator I know of is the market replay connection. I would recommend finding several different indicators which are similar to your indicator, and plotting them on the chart directly using data series we create with ctrl + f . Your indicators should plot directly on top of any component indicators you add to a chart. I am including some documentation for the replay connection.

                        http://ninjatrader.com/support/helpG...t7/?replay.htm
                      3. I recommend, rather than trying to do this through code, simply adding two instances of the indicator to your chart.
                      Jessica P.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by gravdigaz6, Today, 11:40 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post gravdigaz6  
                      Started by MarianApalaghiei, Today, 10:49 PM
                      3 responses
                      9 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by XXtrader, Today, 11:30 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post XXtrader  
                      Started by love2code2trade, Yesterday, 01:45 PM
                      4 responses
                      28 views
                      0 likes
                      Last Post love2code2trade  
                      Started by funk10101, Today, 09:43 PM
                      0 responses
                      9 views
                      0 likes
                      Last Post funk10101  
                      Working...
                      X