Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate sum of previous wicks

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

    #16
    Thank you. currently I am hardconding number of bars which is four. Is it possible for a user to select how many bars to view upperwicks or lowerwicks? I was thinking is this done via enum in strategy? Do we have an example enum with strategy? in only found example in indicator

    Comment


      #17
      I was able to create ENUM based on moving average example. I am not sure if i am calling upperWicksSeries or lowerWIcksSeries right. I am stuck with upperWickSeries .. because In user settings i want to select only 1 value for both upperWickSeries and lowerWickSeries. how Do i accomplish this? And in entry logic condition how do i write it out? Thank you


      Enum declaration
      Code:
      private UpperBarsEnum.numberOfBars    barsNumber    = UpperBarsEnum.numberOfBars.Four;
      in onbarupdate
      Code:
      #region Enum Wicks
      
                  switch (barsNumber)
                  {
      
                      case UpperBarsEnum.numberOfBars.Three:
                      {                    
                          Value[0] = upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3];
                          break;
                      }                
                      case UpperBarsEnum.numberOfBars.Four:
                      {                    
                          Value[0] = upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3] + upperWicksSeries[4];
                          break;
                      }                
                      case UpperBarsEnum.numberOfBars.Five:
                      {                    
                          Value[0] = upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3] + upperWicksSeries[4] + upperWicksSeries[5];
                          break;
                      }            
                      case UpperBarsEnum.numberOfBars.Six:
                      {                    
                          Value[0] = upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3] + upperWicksSeries[4] + upperWicksSeries[5] + upperWicksSeries[6];
                          break;
                      }
                  }
      
                  #endregion​
      Code:
      [Display(GroupName = "Paremeters", Description="Choose How many Bars to look for cumulative wicks")]
              public UpperBarsEnum.numberOfBars BarsNumber
              {
                  get { return barsNumber; }
                  set { barsNumber = value; }
              }​
      My SeriesT for upper and lower wicks
      Code:
      if (High[1] >= Open[1])
                              {
                                  upperWicksSeries[1] = (High[1] - Open[1]) / TickSize;                          
                              }
                      if (High[2] >= Open[2])
                              {
                                  upperWicksSeries[2] = (High[2] - Open[2]) / TickSize;                          
                              }​
      if (Low[1] <= Open[1])
                              {
                                  lowerWicksSeries[1] = (Open[1] - Low[1]) / TickSize;                          
                              }
                      if (Low[2] <= Open[2])
                              {
                                  lowerWicksSeries[2] = (Open[2] - Low[2]) / TickSize;                          
                              }​
      outside of class
      Code:
      namespace UpperBarsEnum
              {
              public enum numberOfBars
              {
                  Three,
                  Four,
                  Five,
                  Six,
              }​
      And in my logic i calculate upperwicks and then have a condition if that number is less then cumulative ticks in wicks
      Code:
      double sumUpperWicks = (upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3] + upperWicksSeries[4]);
      Last edited by tkaboris; 02-28-2023, 08:39 PM.

      Comment


        #18
        Hello tkaboris,

        Thanks for your note.

        We do have a reference sample showing how to use enums in a custom NinjaScript. You may find the reference sample linked below.

        Creating a user-defined parameter type (enum): https://ninjatrader.com/support/help...ned_parame.htm

        That said, please tell me specifically what part of the code you shared to you need assistance with so I may accurately assist.

        Are you wanting to assign the upperWickSeries to a plot and lowerWickSeries to a plot and reference those plots in a condition to place an Entry order?

        I look forward to assisting you further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #19
          HI its with this ones.. I Currently i am only calling upperWicksSeries but i need that when user selects Four in ENUM it will call both upper and lower wicks series.

          case UpperBarsEnum.numberOfBars.Three:
          {
          Value[0] = upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3];
          break;
          }​

          and how do i reference in my entry logic
          If close > open

          && (sumUpperWicks <= CumulativeWicks) // this is my old logic without enum and i dont know how to reference it then
          Last edited by tkaboris; 03-01-2023, 11:11 AM.

          Comment


            #20
            See my example code from post Private Messages The correct upper and lower wicks are correctly counted by using Math.Min and Math.Max. You also don't need an enum to accomplish the sums of used defined bars by using loops.

            (Variables)

            private Series <double> upperWickSeries;
            private Series <double> lowerWickSeries;​
            private int numBars = 4; //default
            double UpperSum, LowerSum;

            if (State == State.DataLoaded)
            {
            upperWickSeries = new Series<double>(this);
            lowerWickSeries = new Series<double>(this);​
            }

            (OnBarUpdate())

            upperWickSeries[0] = High[0] - Math.Max(Open[0], Close[0]);
            lowerWickSeries[0] = Math.Min(Open[0], Close[0]) - Low[0];​

            if(CurrentBar > numBars)
            {
            UpperSum = 0;
            LowerSum = 0;
            for(int i = numBars; i >= 0; i--) //This sums the current bar with the previous 3 (with constant updates to the current bar wicks)
            {
            UpperSum += upperWicksSeries[i];
            LowerSum += lowerWicksSeries[i];
            ​}
            }
            //If you only want the previous 4 bars , not including the current bar, loop like this instead:
            for(int i = numBars+1; i > 0; i--) //This only needs to be calculated once at first tick of bar, so you and can execute the loop using IsFirstTickOfBar and put the loop in following brackets { }
            Last edited by eDanny; 03-01-2023, 12:39 PM.
            eDanny
            NinjaTrader Ecosystem Vendor - Integrity Traders

            Comment


              #21
              I get i is not found in current context. How do i fix it with loop i? never done it

              Comment


                #22
                I made a boo boo.
                Here is the corrected line:
                for(int i = numBars; i >= 0; i--)
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #23
                  Hello tkaboris,

                  Thanks for your note.

                  In the code you shared, you are currently assigning your upperWickSeries calculations to a plot.

                  If you would also like to assign your lowerWickSeries calculation to a plot, you could create another plot by calling AddPlot() again and then assign the lowerWickSeries calculations to the second plot.

                  For example:
                  case UpperBarsEnum.numberOfBars.Three:
                  {
                  Values[0][0]= upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3];
                  Values[1][0] = lowerWickSeries[1] + lowerWickSeries[2] + lowerWickSeries[3];
                  break;
                  }​

                  See the third sample code section in the AddPlot() help guide page which demonstrates adding values to different plots.

                  AddPlot(): https://ninjatrader.com/support/help...t8/addplot.htm
                  Values: https://ninjatrader.com/support/help...nt8/values.htm

                  Since the upperWickSeries is being added up and assigned to the first plot (Values[0][0] in the sample code above), you could reference that plot in your condition to place an order. For example:

                  if (Values[0][0] <= CumulativeWicks)
                  {
                  //do something here.
                  }

                  Please let me know if I may assist further.​
                  <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                  Comment


                    #24
                    thank you it fixed it. I now have error OnBarUpdate' method on bar 0: Index was outside the bounds of the arra

                    if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))
                    {
                    return;
                    }
                    //if (CurrentBars[0] < 10) return;

                    if(CurrentBar > numBars)
                    {
                    UpperSum = 0;
                    LowerSum = 0;
                    for(int i = numBars; i >= 0; i--) //This sums the current bar with the previous 3 (with constant updates to the current bar wicks)
                    {
                    UpperSum += upperWicksSeries[i];
                    LowerSum += lowerWicksSeries[i];
                    }
                    }
                    Print(UpperSum);

                    Comment


                      #25
                      NinjaTrader_BrandonH

                      HI i am getting error Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array

                      protected override void OnBarUpdate()
                      {
                      if (Bars.IsFirstBarOfSession)
                      {
                      currentPnl = 0;
                      }


                      region Enum Wicks

                      switch (barsNumber)
                      {

                      case UpperBarsEnum.numberOfBars.Three:
                      {
                      Values[0][0] = (upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3]);
                      Values[1][0] = lowerWicksSeries[1] + lowerWicksSeries[2] + lowerWicksSeries[3];
                      break;
                      }​

                      Comment


                        #26
                        Hello tkaboris,

                        Thanks for your note.

                        This error message indicates that you are using an invalid index somewhere in your script.

                        What is the exact index that you tried to access that was invalid?

                        A CurrentBar check could be added to the script to ensure that a certain number of bars have been processed before the script calculates information.

                        CurrentBar: http://ninjatrader.com/support/helpG...currentbar.htm

                        Please see the help guide page about making sure you have enough bars in the data series you are accessing: https://ninjatrader.com/support/help...nough_bars.htm

                        In the sample code you shared in post # 25, you are accessing an index of 3 (upperWickSeries[3]/lowerWickSeries[3]), this means that a minimum of 3 bars must be processed before accessing that index. If the CurrentBar is 1, then the index you are accessing ([3]) is invalid since there is only 1 bar that has processed.

                        Debugging prints should be added to the script to print out the CurrentBar and the indexes that you are accessing to make sure that the CurrentBar value is greater than or equal to the index you are accessing or that the Count of the object is greater than or equal to the index being used.

                        Please review this forum thread for more information about this error message: https://forum.ninjatrader.com/forum/...92#post1050092

                        Let me know if I may assist further.
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #27
                          I have
                          BarsRequiredToTrade = 10; and overkill below that still produces
                          Error on calling 'OnBarUpdate' method on bar 30: Index was outside the bounds of the array. i dont know how to debug it since strategy turns off


                          protected override void OnBarUpdate()
                          {
                          if (Bars.IsFirstBarOfSession)
                          {
                          currentPnl = 0;
                          }
                          if (CurrentBar < 10)
                          return;
                          if (Close[0] > Close[Math.Min(CurrentBar, 10)])
                          return;

                          if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))
                          {
                          return;
                          }

                          region Enum Wicks

                          switch (barsNumber)
                          {

                          case UpperBarsEnum.numberOfBars.Three:
                          {
                          Values[0][0] = (upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3]);
                          Values[1][0] = lowerWicksSeries[1] + lowerWicksSeries[2] + lowerWicksSeries[3];
                          break;
                          }​

                          Comment


                            #28
                            Hello tkaboris,

                            To debug the script you could start by reducing the code to determine what section of code is throwing the error message. Once you see the error stop appearing after commenting out a section of code, it is likely that section of code that is causing the index out of range error.

                            Once you locate the section of code throwing the error, you could narrow in on what is causing the error by adding prints to the script that prints out the CurrentBar value and the index you are accessing. You need to ensure that CurrentBar is greater than or equal to the indexes you are using in your code.

                            A simple example of this is calling Close[1] when CurrentBar is 0 (the very first bar) which will cause this error. The reason for this is that there will not be a bar ago; essentially there is not enough data at bar 0 to look back one bar.

                            The script will start from the far left of the chart at the very beginning where the CurrentBar will equal 0 and then start working to the right, calling OnBarUpdate() for each bar. In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call an index 1 bar ago.​

                            Note the BarsRequiredToTrade property only determines how many historical bars need to have processed on the chart before the strategy places trades. It does not have to do with making sure that you have enough bars in the data series you are accessing.

                            BarsRequiredToTrade: https://ninjatrader.com/support/help...redtotrade.htm

                            I see in your script that you have the condition below which checks if there are at least 10 bars that have been processed on the chart.

                            if (CurrentBar < 10)
                            return;


                            ​Then you have another condition seen below that could likely be removed from the script

                            if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))
                            {
                            return;
                            }


                            Then you could consider modifying your first CurrentBars condition to look something like this. The condition below will check if the number of bars on the chart are at least the same number as your BarsRequiredToTrade property which you noted is equal to 10.

                            if (CurrentBar < BarsRequiredToTrade)
                            return;


                            That said, if you are accessing an index larger than 10 in your script, this would not prevent the error from occurring. You would need to reduce the code in your script and use prints to find the exact line of code causing the index out-of-range error.

                            Let me know if you need assistance with creating a certain print or analyzing the outputt window.​​
                            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                            Comment


                              #29
                              The problem was on this line in entry order logic.
                              (sumUpperWicks <= CumulativeWicks)


                              for some reason i now have orange error class member declaration expected, it compiles though on line 34 which is
                              private Series <double> upperWicksSeries;
                              private Series <double> lowerWicksSeries;​

                              Comment


                                #30
                                Hello tkaboris,

                                Thanks for your notes.

                                Please send me a screenshot of the error message showing the full error message and the line number that the error is occurring on. Ensure that the code for the line number the error is referring to is visible in the screenshot.

                                Also, where is sumUpperWicks being declared in your script? Please send a screenshot showing how this variable is being declared as well.
                                • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
                                • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
                                ​I look forward to assisting further.
                                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Yesterday, 05:17 AM
                                0 responses
                                56 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                132 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                73 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                45 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                49 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X