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

Ron Black's Swing Ling indicator

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

    Ron Black's Swing Ling indicator

    Hi everyone,

    The default indicator called "Swing" in NT8, does not seem to be the logic of Rob Black's "Swing Ling" indicator. Is there an example of this out there?

    The easy language code is like this:

    Code:
    Vars: hH(H), lL(L), lH(H), hL(L), upsw(0), SwLine(0);
    
     {Find Swing Direction}
    if upsw=1 then begin
        if H>hH then hH=H; {update hH, hL}
        if L>hL then hL=L;
    
       if H<hL then begin {swing changes to down}
           upsw=0;
           lL=L;
           lH=H;
       end;
    end;
    
    if upsw=0 then begin
       if L<lL then lL=L; {update lL, lH}
       if H<lH then lH=H;
    
       if L>lH then begin {swing changes to up}
            upsw=1;
            hH=H;
            hL=L;
      end;
    end;
    
    if upsw=1 then plot1(hL, “SwLine”, cyan);
    if upsw=0 then plot1(lH, “SwLine”, magenta);
    So, is it already coded in Ninjascript ?
    Last edited by wolfcuring; 01-26-2019, 06:27 AM.

    #2
    So, I find the code for NT 7. And meanwhile, I want to add another zone to denote noise. The NT 7 code is as follows,

    Code:
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// S&C magazine September 2010. Getting Clear With Short-Term Swings
        /// </summary>
        [Description("S&C magazine September 2010. Getting Clear With Short-Term Swings")]
        public class ShortTermSwings : Indicator
        {
            #region Variables
    
            private Color      colorDown = Color.Red;
            private Color      colorUp   = Color.Green;
            private bool       paintBars = false;
            private DataSeries trend     = null;
    
            #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(new Plot(Color.Black, PlotStyle.Line, "Trend Line"));
                Overlay        = true;
                trend       = new DataSeries(this);
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                if(CurrentBar == 0)
                {
                    Trend.Set(Close[0] >= Open[0] ? 1 : -1);
                    Value.Set(Trend[0] == 1 ? Low[0] : High[0]);
                    return;
                }
    
                if(Trend[1] == 1)
                {
                    double higherLow = Math.Max(Value[1], Low[0]);
    
                    if (High[0] < higherLow - TickSize * 0.01)
                    {
                        Trend.Set(-1);
                        Value.Set(High[0]);
                    }
                    else
                    {
                        Trend.Set(1);
                        Value.Set(higherLow);
                    }
                }
                else
                {
                    double lowerHigh = Math.Min(Value[1], High[0]);
    
                    if (Low[0] > lowerHigh + TickSize * 0.01)
                    {
                        Trend.Set(1);
                        Value.Set(Low[0]);
                    }
                    else
                    {
                        Trend.Set(-1);
                        Value.Set(lowerHigh);
                    }
                }
    
                PlotColors[0][0] = Trend[0] == 1 ? ColorUp : ColorDown;
    
                if (PaintBars)
                    BarColor = PlotColors[0][0];
            }
    }
    So, I will keep updating until it is finished. Hope this can help who also needs it.

    Comment


      #3
      Now, I translated the NT 7 code into NT 8, but it does seem to work. How to fix this ?

      Besides, I want to add a third zone to denote noise. Rob Black's logic is that if the noise followed the up swing (with higher lows) it is still part of the up swing, and vice versa. And I want to define up swing in a stricter manner: every Low[0] is strictly higher than the previous one, and down swing: every High[0] < High[1], all else is considered noise. How can I do this?

      Thank you in advance !

      Code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class SwingLine : Indicator
          {
      
              private Brush ColorUp = new SolidColorBrush(Colors.Green);
              private Brush ColorDown = new SolidColorBrush(Colors.Red);
              private bool Paintbars ;
              private Series <double> trend;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Rob Black's Swing Line indicator. Using range to gauge short term trend. ";
                      Name                                        = "SwingLine";
                      Calculate                                    = Calculate.OnBarClose;
                      IsOverlay                                    = false;
                      DisplayInDataBox                            = true;
                      DrawOnPricePanel                            = true;
                      DrawHorizontalGridLines                        = true;
                      DrawVerticalGridLines                        = true;
                      PaintPriceMarkers                            = true;
                      ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                      //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                      //See Help Guide for additional information.
                      IsSuspendedWhileInactive                    = true;
      
                      Paintbars =true;
                      //add plots. 
                      AddPlot(new Stroke(Brushes.Black, 2), PlotStyle.Line, "Trend Line");
      
                  }
      
                  else if (State == State.DataLoaded)
                  {
                      trend = new Series<double>(this);                
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  if (CurrentBar==0)
                  {
                      if (Close[0]>=Open[0]) trend[0]=1; else trend[0]=-1;
                      if (trend[0]==1) Value[0]=Low[0]; else Value[0]=High[0];
                      return;
                  }
      
                  if(trend[1]==1)
                  {
                      double higherLow=Math.Max(Value[1],Low[0]);
      
                      if(High[0]<higherLow -TickSize*0.0001)
                      {
                          trend[0]=-1;
                          Value[0]=High[0];
                      }
                      else
                      {
                          trend[0]=1;
                          Value[0]=higherLow;
                      }
                  }
                  else 
                  {
                      double lowerHigh = Math.Min(Value[1],High[0]);
      
                      if (Low[0] > lowerHigh + TickSize*0.0001)
                      {
                          trend[0]=1;
                          Value[0]=Low[0];
                      }
                      else
                      {
                          trend[0]=-1;
                          Value[0]=lowerHigh;
                      }
                  }
      
                  if(Paintbars)
                  {
                      if(trend[0]==1) BarBrush=ColorUp;
                      if(trend[0]==-1) BarBrush=ColorDown;
                  }
      
              }
      
              #region Properties
      
              [NinjaScriptProperty]
              [Display(Name="Paintbars", Order=1, GroupName="Parameters")]
              public bool PaintBars
              { get; set; }
      
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Trend
              {
                  get { return trend; }
              }
              #endregion
      
          }
      }

      Comment


        #4
        Hello wolfcuring,

        When you mention "Now, I translated the NT 7 code into NT 8, but it does seem to work. How to fix this ? "

        What isn't working?

        Are you printing the value of the custom series and this value is not the correct calculated value?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello wolfcuring,

          When you mention "Now, I translated the NT 7 code into NT 8, but it does seem to work. How to fix this ? "

          What isn't working?

          Are you printing the value of the custom series and this value is not the correct calculated value?
          Hello, Chelsea,

          It is not plotting. I suspect that it is the

          Code:
          if(CurrentBar == 0) 
           {    Trend.Set(Close[0] >= Open[0] ? 1 : -1);    Value.Set(Trend[0] == 1 ? Low[0] : High[0]);    return; }
          part I did not get it right. My code in NT 8 is:

          Code:
           
           if (CurrentBar==0) {   if (Close[0]>=Open[0]) trend[0]=1; else trend[0]=-1;   if (trend[0]==1) Value[0]=Low[0]; else Value[0]=High[0];   return; }
          I know it is meant to set the first value of the indicator. But, not sure how to do this in NT 8.

          Another thing I am not quite sure with is:

          Code:
           
           if(High[0]<higherLow -TickSize*0.0001)
          Is it to exclude the case when High[0]=higherLow ? The original code in NT 7 is TickSize*0.01, as I want to use it on Forex, I used TickSize*0.0001.

          So, I am still working on it to debug. And I have attached the exported file below.

          Thank you for your help.
          Attached Files

          Comment


            #6
            So, the log file tells me that:

            Indicator 'SwingLine': Error on calling 'OnBarUpdate' method on bar 1: The calling thread cannot access this object because a different thread owns it.

            Comment


              #7
              Hello wolfcuring,

              Using prints to debug the script I was able to determine the issue is the ColorUp and ColorDown brushes. These are being initialized before the script instance that is run is created. Try moving the initialization of the brush objects and assigning these to the variables to State.DataLoaded in OnStateChange() instead.

              Also, it looks like it would be simpler to use Brushes.Green instead of new SolidColorBrush(Colors.Green).
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thank you Chelsea, I will have a try.

                Comment


                  #9
                  Hi Chelsea,

                  After trying to remove the error you pointed out, I get Ron Black's indicator working as expected (Attached in the file "SwingLine_Ron_Black"). The basic idea is that if the price bar is making higher lows then it is trending up, and when it is making lower highs it is trending down, others are noise.

                  While I also want to denote the noise. By noticing that when it is noise, the plot is not changing so Value[0]==Value[1], I added another data series (mytrend) to capture this (Attached as "SwingTrend"). And that is the only thing I added. But something wired happens after adding this new element. As shown in the picture, when there are lower highs, the plot remain the same as one bar ago.(picked out by blue cycle)

                  No quite understand why this is happening.

                  ​​Click image for larger version

Name:	EURUSD_2018_2.png
Views:	733
Size:	254.6 KB
ID:	1046450​​​​​​​

                  Comment


                    #10
                    Hello wolfcuring,

                    Setting Value[0 ]== Value[1] would cause the plot to have the same value as one bar ago.

                    Is this not what you are wanting?

                    Have you used prints to debug the logic to see if the condition that sets the plot is evaluating as true when you expect it?

                    Below is a link to a forum post that demonstrates using prints to understand behavior.


                    May we have the output from the print that is showing unexpected values in the condition?


                    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you Chelsea, will have more tries.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by judysamnt7, 03-13-2023, 09:11 AM
                      4 responses
                      59 views
                      0 likes
                      Last Post DynamicTest  
                      Started by ScottWalsh, Today, 06:52 PM
                      4 responses
                      36 views
                      0 likes
                      Last Post ScottWalsh  
                      Started by olisav57, Today, 07:39 PM
                      0 responses
                      7 views
                      0 likes
                      Last Post olisav57  
                      Started by trilliantrader, Today, 03:01 PM
                      2 responses
                      22 views
                      0 likes
                      Last Post helpwanted  
                      Started by cre8able, Today, 07:24 PM
                      0 responses
                      10 views
                      0 likes
                      Last Post cre8able  
                      Working...
                      X