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

Slope function output

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

    Slope function output

    Hi,

    Does anyone know what the output of the Slope() function actually represents?
    It seems to change on ticksize. How can I just convert it to degrees?
    (where 90 is straight up and -90 is straight down)


    Thanks in Advance

    #2
    I can give you the math we use:

    y2 - y1 / x2 - x1 = price2 - price1 / endBarNumber - startBarNumber
    RayNinjaTrader Customer Service

    Comment


      #3
      OK, I worked it out. You can't use the Slope() function for the angle as it gives a price related calculation as its ouput. But you can work the true angle if you override the Plot class to get the value. Of course, a side effect of working out the true angle is that as the chart is resized or stretched the angle outputted changes too. Here's the code for anyone wanting it:



      public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
      {
      // Default plotting in base class.
      base.Plot(graphics, bounds, min, max);

      int index = -1;
      int bars = CalculateOnBarClose ? (ChartControl.BarsPainted - 2) : (ChartControl.BarsPainted - 1);
      DataSeries emaSeries = this.EMASeries; // populated in onBarUpDate


      if (base.Bars != null)
      {
      while (bars >= 0)
      {
      double emaSlope;

      index = ((ChartControl.LastBarPainted - ChartControl.BarsPainted) + 1) + bars;
      if (ChartControl.ShowBarsRequired || ((index - base.Displacement) >= base.BarsRequired))
      {
      double val = emaSeries.Get(index);
      if (!double.IsNaN(val) && (val != 0))
      {
      int y2 = (bounds.Y + bounds.Height) - ((int) (((val - min) / ChartControl.MaxMinusMin(max, min)) * bounds.Height));
      val = emaSeries.Get(index+1);
      if (!double.IsNaN(val) && (val != 0))
      {
      int y1 = (bounds.Y + bounds.Height) - ((int) (((val - min) / ChartControl.MaxMinusMin(max, min)) * bounds.Height));
      emaSlope = ((180/Math.PI) * Math.Atan2(y1-y2,ChartControl.BarSpace))*-1;
      // do something
      }
      }
      }
      bars--;
      }
      }
      }

      Last edited by Gumphrie; 07-16-2007, 06:12 PM.

      Comment


        #4
        Hello Gumphrie,
        This is great. I would really like to learn how to use slope in a strategy or just with an indicator. Could you please show us an example of how this could be applied/attached to the working EMA indicator code or how you to use it in a working strategy using the slope code.


        Thanks so much.

        Comment


          #5
          Couldn't you also just use some math to determine the angle? Slope of 1 is 45 degrees and slope of -1 is -45. Slope of 0 is 0 degrees. You don't have to worry about 90 or -90 because those won't exist on a chart.

          Basically just use m=tan θ where m is the slope and θ is the degrees.

          Example
          Code:
          (System.Math.Atan(Slope(SMA(5),CurrentBar-1,CurrentBar))*180/Math.PI).ToString()
          This will give you the angle in degrees of an SMA.
          Last edited by NinjaTrader_JoshP; 07-16-2007, 05:28 PM.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            uacvax,

            Do you mean the output from Slope()? I thought that was what is was too, 1 to -1 = the angle, but it isn't. Try it on instrument with a different tick size! The output of Slope() is in effect the same as the percentage rise or fall of the given index. If thats not what you mean then I'm not sure as my trigonometry is very rusty.

            dwalls

            Please find attached. I've used Ray's MA switching code as a base and used the slope angle code to create a 'slope box' at the bottom right corner of the chart.

            -- attachment removed - see latest post(s) --
            Last edited by Gumphrie; 07-17-2007, 08:45 AM.

            Comment


              #7
              I'm not quite sure what you mean by an indicator with a different tick size. Can you give me an example?
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by uacvax View Post
                I'm not quite sure what you mean by an indicator with a different tick size. Can you give me an example?
                Lets say NQ and SE, one is 0.25, the other is 0.0001. So when Slope() takes one price away from the other the results will be quite different across these instruments.

                Comment


                  #9
                  Gumphrie,
                  Thanks, I got it work. Much appreciated.
                  I have two things:

                  1) Would it be possible for you to add the LSMA to the list of indicators in the MASlopeBox or make a seperate MASlopeBox for the LSMA? Thanks

                  2) I found this in another code and was hoping it might help you in some way with you previous post about the different instruments:

                  //**************************************
                  // This secion is performed on First bar only
                  // Contains Set-ups that remain constant for chart, so don't need to be repeated every bar/tick
                  //
                  if (CurrentBar == 1)
                  {
                  // Set the stringformat parameter for decimal places to suit the instrument.
                  // This ensures that trailing zeros are always displayed - maybe an easier way to do this?
                  double temp1 = TickSize - (int)TickSize;// get fractional part of TickSize
                  decimals = temp1.ToString(); // make it into a string
                  int tickLength = decimals.Length; // get the number of characters in the string
                  switch (tickLength) // set format depending on length of string
                  {
                  case1: decimals = "f0"; // eg YM 1t = 1 point
                  break;
                  case3: decimals = "f1"; // eg ER2 1t = 0.1 point
                  break;
                  case4: decimals = "f2"; // eg NQ 1t = 0.25 point
                  break;
                  case5: decimals = "f3"; // eg 1t = 0.001 point
                  break;
                  case6: decimals = "f4"; // eg 6E 1t = 0.0001 point
                  break;
                  case7: decimals = "f5"; // eg 1t = 0.00001 point
                  break;
                  default: decimals = ""; // default is no fixed decimal format
                  break;
                  // more options can be added if neccessary......
                  }

                  // StringFormat set up
                  formatCenter.Alignment = StringAlignment.Center; // text centered
                  formatRight.Alignment = StringAlignment.Far; // text right aligned

                  Comment


                    #10
                    I also found this on the slope:

                    // slope compares current EMA with the average of the 2 prior bars (or the EMA 1.5 bars back)
                    ema34slope = (int)(RadToDegrees*(Math.Atan((EMA(34)[0]-(EMA(34)[1]+EMA(34)[2])/2)/1.5/TickSize)));

                    Comment


                      #11
                      Hmm I'm not quite sure as to why you would take one away from the other in that fashion. Slope is a function of a single line or curve. You are taking two different curves and in effect using priceSE-priceNQ/1. I'm not sure exactly what that calculates, but I'm pretty sure that isn't a slope. If there is some relationship between the two instruments that you wanted a slope of you need to first plot that relationship as its own curve and then take the slope of that new composite curve.

                      When you go (priceSE[2]-priceSE[1])/(2-1) and compare it to (priceNQ[2]-priceNQ[1])/(2-1) the two slopes are "normalized" on the (-1,1) scale so the 0.00 vs 0.0000 shouldn't matter. A numerical move of the same magnitude on both price scales will result in the same slope (e.g. a 0.50 cent move on either instrument yields a slope of 1/2).

                      Since the 0.0000 scale is smaller, a 5% change in price in that instrument will not yield a slope equal to a 5% change in price of an instrument on the 0.00 scale. This is to be expected because your "run" factor for calculating the slope is the same, 1min/bar/etc on both bars. This is proper slope calculation so I'm not sure what kind of number you were hoping to get out of the Slope() function. The slopes you get on the 0.0000 scale will just generally be smaller than on the 0.00 scale because of the normalized "run".

                      Also, slope is not a function to determine percent change. If you wanted percent change you would do (NewPrice-OldPrice)/OldPrice.

                      Edit: I believe the code dwalls posted from the WoodiesCCIPanel indicator will work to your particular likings. It normalizes the ticksizes so the slopes should be directly comparable.
                      Last edited by NinjaTrader_JoshP; 07-16-2007, 11:38 PM.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        uacvax

                        I'm not directly taking one price away from the other, I'm just saying that Slope() is in effect doing that in the calculation ( (priceSE[2]-priceSE[1])/(1) and (priceNQ[2]-priceNQ[1])/(1) ). I'm not saying it is the same as the percentage either, just that also does something similar in that its taking one price away from another.

                        There doesn't seem to be a proper way, just different ones. I'm throwing out my study results in effect. I wouldn't use the code I posted in a strategy as its chart specific, being dependent on the scale of the chart, but it may help someone whose trading style is chart orientated or if you need quick visual clarification. I might use the output of Slope() but its not really the slope at all, but a price change comparison for the given instrument. That may be what you want or it might not be. So something like the Woodies calculation is probably the best compromise, taking into account the ticksize its probably the best you can get for strategy work

                        Its interesting that most people who use Woodies and other similar systems seem to use tick bars rather than time based ones. Seeing as the slope is calculated on the basis that the 'x' axis is the number of new prices rather than time periods or 'chart space' that seems to make sense.
                        Last edited by Gumphrie; 07-17-2007, 02:22 AM.

                        Comment


                          #13
                          Well isn't a price change comparison exactly what a slope is on a chart? Rise/Run with Rise being the y-axis (price) and Run being the x-axis (time). But I see your point. I was just slightly confused .
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            dwalls,

                            I don't have the code for LSMA, could you send or redirect. Thanks!

                            Funny you digged out the WoodiesCCIPanel indicator code, thats exactly the point I got to. I hadn't noticed the emaSlope calculation though, Thanks Again!

                            Comment


                              #15
                              Originally posted by uacvax View Post
                              Well isn't a price change comparison exactly what a slope is on a chart? Rise/Run with Rise being the y-axis (price) and Run being the x-axis (time). But I see your point. I was just slightly confused .
                              Me too. Maths is not my strong point. It just seems any number based on time is not 'quantifiable' when comparing it to the charts.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Christopher_R, Today, 12:29 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Christopher_R  
                              Started by sidlercom80, 10-28-2023, 08:49 AM
                              166 responses
                              2,235 views
                              0 likes
                              Last Post sidlercom80  
                              Started by thread, Yesterday, 11:58 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post thread
                              by thread
                               
                              Started by jclose, Yesterday, 09:37 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jclose
                              by jclose
                               
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              10 responses
                              1,415 views
                              0 likes
                              Last Post Traderontheroad  
                              Working...
                              X