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 hold any amount of swing highs/lows

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

    How to hold any amount of swing highs/lows

    Code at the bottom and context is that I have too many doubles (prevHigh's and prevLow's) and I would rather use something like a List but any attempt I have made to use Lists breaks the code somehow that doesn't raise an error but doesn't return anything on the chart. Any advice on how I should properly implement a List or some other container of multiple variables is much appreciated.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class ASR3 : Indicator
    {
    private bool direction;
    private double currHigh;
    private double currLow;
    private double prevHigh1;
    private double prevHigh2;
    private double prevHigh3;
    private double prevHigh4;
    private double prevHigh5;
    private double prevLow1;
    private double prevLow2;
    private double prevLow3;
    private double prevLow4;
    private double prevLow5;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "ASR 3";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    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;
    Period = 55;
    Deviations = 1;
    AddPlot(Brushes.Green, "Upper");
    AddPlot(Brushes.Red, "Lower");
    AddPlot(Brushes.Orange, "Middle");
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    double dev = StdDev(Input, Period)[0] * Deviations;
    if(CurrentBar == 0)
    {
    currHigh = Input[0];
    currLow = Input[0];
    prevHigh1 = Input[0];
    prevHigh2 = Input[0];
    prevHigh3 = Input[0];
    prevHigh4 = Input[0];
    prevHigh5 = Input[0];
    prevLow1 = Input[0];
    prevLow2 = Input[0];
    prevLow3 = Input[0];
    prevLow4 = Input[0];
    prevLow5 = Input[0];
    return;
    }
    else if(CurrentBar == 1)
    {
    if(Input[0] >= Input[1])
    {
    direction = true;
    }
    else if(Input[0] < Input[1])
    {
    direction = false;
    }
    }
    if(Input[0] > currHigh && direction == true)
    {
    currHigh = Input[0];
    }
    else if(Input[0] < currLow && direction == false)
    {
    currLow = Input[0];
    }
    if(direction == true && Input[0] < currHigh - dev)
    {
    direction = false;
    prevLow5 = prevLow4;
    prevLow4 = prevLow3;
    prevLow3 = prevLow2;
    prevLow2 = prevLow1;
    prevLow1 = currLow;
    currLow = Input[0];
    }
    if(direction == false && Input[0] > currLow + dev)
    {
    direction = true;
    prevHigh5 = prevHigh4;
    prevHigh4 = prevHigh3;
    prevHigh3 = prevHigh2;
    prevHigh2 = prevHigh1;
    prevHigh1 = currHigh;
    currHigh = Input[0];
    }

    Upper[0] = (prevHigh5 + prevHigh4 + prevHigh3 + prevHigh2 + prevHigh1) / 5;
    Lower[0] = (prevLow5 + prevLow4 + prevLow3 + prevLow2 + prevLow1) / 5;
    Middle[0] = (Upper[0] + Lower[0]) / 2;

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Period", Order=1, GroupName="Parameters")]
    public int Period
    { get; set; }

    [NinjaScriptProperty]
    [Range(0.01, double.MaxValue)]
    [Display(Name="Deviations", Order=2, GroupName="Parameters")]
    public double Deviations
    { get; set; }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Upper
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Lower
    {
    get { return Values[1]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Middle
    {
    get { return Values[2]; }
    }
    #endregion

    }
    }​

    #2
    Hello KDTrade,

    In what you provided I don't see any type of list being used, If you are having problem with a list we would need to see a more specific sample of the code and also what error you are having.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse, thanks for the timely reply. Here is an example of my attempt to use a List and as previously stated, it doesn't appear to return anything.

      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class ASR3Test : Indicator
      {
      private bool direction;
      private double currHigh;
      private double currLow;
      private List<double> Highs;
      private List<double> Lows;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"";
      Name = "ASR3Test";
      Calculate = Calculate.OnBarClose;
      IsOverlay = true;
      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;
      Period = 55;
      Deviations = 1;
      SwingsToStore = 5;
      AddPlot(Brushes.Green, "Upper");
      AddPlot(Brushes.Red, "Lower");
      AddPlot(Brushes.Orange, "Middle");
      }
      else if (State == State.Configure)
      {
      Highs = new List<double>();
      Lows = new List<double>();
      }
      }

      protected override void OnBarUpdate()
      {
      double dev = StdDev(Input, Period)[0] * Deviations;
      if(CurrentBar == 0)
      {
      currHigh = Input[0];
      currLow = Input[0];
      Highs.Append(Input[0]);
      Lows.Append(Input[0]);
      return;
      }
      else if(CurrentBar == 1)
      {
      if(Input[0] >= Input[1])
      {
      direction = true;
      }
      else if(Input[0] < Input[1])
      {
      direction = false;
      }
      }
      if(Input[0] > currHigh && direction == true)
      {
      currHigh = Input[0];
      }
      else if(Input[0] < currLow && direction == false)
      {
      currLow = Input[0];
      }
      if(direction == true && Input[0] < currHigh - dev)
      {
      direction = false;
      Lows.Append(currLow);
      currLow = Input[0];
      }
      if(direction == false && Input[0] > currLow + dev)
      {
      direction = true;
      Highs.Append(currHigh);
      currHigh = Input[0];
      }
      while(Highs.Count() > SwingsToStore)
      Highs.RemoveAt(0);

      while(Lows.Count() > SwingsToStore)
      Lows.RemoveAt(0);

      Upper[0] = Highs.Average();
      Lower[0] = Lows.Average();
      Middle[0] = (Upper[0] + Lower[0]) / 2;
      }

      region Properties
      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="Period", Order=1, GroupName="Parameters")]
      public int Period
      { get; set; }

      [NinjaScriptProperty]
      [Range(0.01, double.MaxValue)]
      [Display(Name="Deviations", Order=2, GroupName="Parameters")]
      public double Deviations
      { get; set; }

      [NinjaScriptProperty]
      [Range(1, int.MaxValue)]
      [Display(Name="SwingsToStore", Order=3, GroupName="Parameters")]
      public int SwingsToStore
      { get; set; }

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> Upper
      {
      get { return Values[0]; }
      }

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> Lower
      {
      get { return Values[1]; }
      }

      [Browsable(false)]
      [XmlIgnore]
      public Series<double> Middle
      {
      get { return Values[2]; }
      }
      #endregion

      }
      }​

      Comment


        #4
        Hello KDTrade,

        What specific part of the code are you having trouble with? Looking at the code I don't know what the problem would be based on the detail that its not returning anything. If you are seeing some problem with the conditions or logic you posted you would need to use Prints to better understand what part is not working.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse, I figured it out using Print. For some reason, Append does not actually add to the List, however, Add does, so thank you for your help in debugging this issue.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by GussJ, 03-04-2020, 03:11 PM
          15 responses
          3,271 views
          0 likes
          Last Post xiinteractive  
          Started by Tim-c, Today, 02:10 PM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by Taddypole, Today, 02:47 PM
          0 responses
          2 views
          0 likes
          Last Post Taddypole  
          Started by chbruno, 04-24-2024, 04:10 PM
          4 responses
          51 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          10 responses
          403 views
          1 like
          Last Post beobast
          by beobast
           
          Working...
          X