Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate sum of previous wicks

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

    Calculate sum of previous wicks

    Hi, How can i calcualate the sum of previous 4 bars with if there are any wicks. Calculate sum in sizes.
    Is there a function for it?
    I want to calcuate sum of sizes of wicks if there are any and make a condition in my entry later.
    double sumOfHighWicks = ((High[1] >= Open[1]) + (High[2] >= Open[2]) + (High[3] >= Open[3]) + (High[4] >= Open[4]));

    Last edited by tkaboris; 02-28-2023, 07:53 AM.

    #2
    Hello tkaboris,

    Thanks for your post.

    There are no documented methods or functions to add together the wicks of bars on a chart.

    This would require you to come up with custom calculations to detect if a bar has a wick and what the size of that wick might be followed by adding together the calculations.

    To detect if there is an upper wick on a down bar you could create a condition that checks if the High price is greater than or equal to the Open price. To detect if there is an upper wick on an up bar you could create a condition that checks if the High price is greater than the Close price.

    An example of getting the number of ticks for the upper wick of a down bar might look something like this.
    Code:
    if (Close[0] < Open[0])
    {
        if (High[0] >= Open[0])
        {
            double wickSize = (High[0] - Open[0]) / TickSize;
            Print("wickSize " + wickSize + " CurrentBar " + CurrentBar);
        }
    }
    You may consider saving the wick size to a custom Series<int> variable so that you could access previous wick size values to add them together.

    Review the help guide documentation below for more information.

    PriceSeries:https://ninjatrader.com/support/help...riceseries.htm
    ​Series<T>: https://ninjatrader.com/support/help...t8/seriest.htm
    TickSize: https://ninjatrader.com/support/help...8/ticksize.htm

    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


      #3
      Thank you,
      If i do it the simple way to calculate upper wicks in a down bars, I get whole ticks number including the body sizes of 4 bars. How can I only calcuate wicks? excluding body?

      double sumOfHighWicks = ((Math.Abs(High[1] - Close[1]) + Math.Abs(High[2] - Close[2]) +
      Math.Abs(High[3] - Close[3]) + Math.Abs(High[4] - Close[4]) ) / TickSize);​

      Comment


        #4
        Hello tkaboris,

        Thanks for your note.

        "How can I only calcuate wicks? excluding body?"

        The sample code I shared in post # 2 demonstrates how to calculate the upper wick size of a down bar. This does not include the body of the candle in the calculations, only the upper wick size of a down bar in ticks.

        You would need to implement logic similar to the sample code I shared to get the upper wick size of a down bar in ticks.

        The calculations could be saved to a Custom Series variable (Series<double>) if you would like to access prior wick sizes of down bars for calculations or comparisons. See the Series<T> help guide page linked in post # 2 for more information about Custom Series variables and sample code.

        Please let me know if I may assist you further.
        Last edited by NinjaTrader_BrandonH; 02-28-2023, 01:39 PM.
        <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


          #5
          Thank you. I would need a bit more help with series T. I am not sure how to make it right. Do i need to create additional longWickSeries for bar 3,2 1 ?

          I have
          created a variable
          private Series<double> longWickSeries;
          and
          longWickSeries = new Series<double>(BarsArray[4]);

          Code:
          protected override void OnBarUpdate()
          {
          if (CurrentBar < Math.Max(BarsRequiredToTrade, BarsBefore))
          return;
          
          if (ToTime(Time[0]) >= StartTradeTime * 100 && ToTime(Time[0]) < StopTradeTime * 100 && Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade)
          {
          
          if (High[1] >= Open[1])
          {
          double longWickSize1 = (High[1] - Open[1]) / TickSize;
          Print("wickSize1 " + longWickSize1 + " CurrentBar " + CurrentBar);
          }
          if (High[2] >= Open[2])
          {
          double longWickSize2 = (High[2] - Open[2]) / TickSize;
          Print("wickSize2 " + longWickSize2 + " CurrentBar " + CurrentBar);
          }
          if (High[3] >= Open[3])
          {
          double longWickSize3 = (High[3] - Open[3]) / TickSize;
          Print("wickSize3 " + longWickSize3 + " CurrentBar " + CurrentBar);
          }
          if (High[4] >= Open[4])
          {
          double longWickSize4 = (High[4] - Open[4]) / TickSize;
          Print("wickSize4 " + longWickSize4 + " CurrentBar " + CurrentBar);
          }
          
          double sumOf4HighWicks = longWickSize1 + longWickSize2 + longWickSize3 + longWickSize4;
          }}

          Comment


            #6
            If i create additional series
            longWickSeries1 = new Series<double>(BarsArray[1]);
            longWickSeries2 = new Series<double>(BarsArray[2]);
            longWickSeries3 = new Series<double>(BarsArray[3]);
            longWickSeries4 = new Series<double>(BarsArray[4]);​

            and then in entry i have
            && (longWickSeries1 + longWickSeries2 + longWickSeries3 + longWickSeries4)
            its complaining that + cannot be assigned to series...

            Comment


              #7
              Hello tkaboris,

              Thanks for your note.

              I do not see where you are using a custom series variable in the code your shared.

              I have attached a simple example script demonstrating how to create a custom series variable, how to save the down bar upper wick size in ticks, and access the current value assigned to the series as well as the previous 2 values assigned to the custom series.

              Notice that to access the current value being held in the custom series, we use a bars ago value of 0, such as wickSizeSeries[0]. To access the previous value assigned to the series we use bars ago 1, such as wickSizeSeries[1]. We use a bars ago value of 2 to get the value assigned to the custom series 2 values ago, wickSizeSeries[2], and so on.

              To add together the last 4 values held in the custom series, you could call SUM(wickSizeSeries, 4)[0];.

              SUM(): https://ninjatrader.com/support/help...mation_sum.htm

              Please note that BarsArray[1] means that you have additional data series added to your script using AddDataSeries() and you want to synchronize the custom series to that added series. If you are not calling AddDataSeries(), you should not use BarsArray when instantiating your custom series.

              Based on the code you shared, it seems that you just want to get the upper wick size of the last 4 candles on the chart and add them together.

              If this is the case, you could create 5 regular double variables named something like wickSize1, wickSize2, wickSize3, wickSize4, and last4BarsWickSizeTotal. Then, calculate the upper wick size of the last 4 bars. Finally, add them together and assign the value to last4BarsWickSizeTotal. See the code below.

              Code:
              // class level variables
              private double wickSize1, wickSize2, wickSize3, wickSize4, last4BarsWickSizeTotal;
              
              //OnBarUpdate() section
              if (High[1] >= Open[1])
              {
                   wickSize1 = (High[1] - Open[1]) / TickSize;
              }
              
              if (High[2] >= Open[2])
              {
                   wickSize2 = (High[2] - Open[2]) / TickSize;
              }​
              
              if (High[3] >= Open[3])
              {
                   wickSize3 = (High[3] - Open[3]) / TickSize;
              }​
              
              if (High[4] >= Open[4])
              {
                   wickSize4 = (High[4] - Open[4]) / TickSize;
              }
              
              last4BarsWickSize = wickSize1 + wickSize2 + wickSize3 + wickSize4;​


              You could study and test both options to see which one suits your overall goal.

              Let me know if I may assist further.
              Attached Files
              Last edited by NinjaTrader_BrandonH; 02-28-2023, 01:40 PM.
              <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


                #8
                Have you added multiple bar types (dataseries) to your chart? I don't think you understand what BarsArray[1] or [2] etc. means. Ex. If you added volume bars to a minute bar chart you would use BarsArray[1] to access the volume bars. You need to instead do something like this:
                upperWickSeries = new Series<double>(this);
                lowerWickSeries = new Series<double>(this);
                Now you have a data series of upper wicks and lower wicks. One slot each for every bar. Now you need to populate them only with wicks:
                upperWickSeries[0] = High[0] - Math.Max(Open[0], Close[0]);
                lowerWickSeries[0] = Math.Min(Open[0], Close[0]) - Low[0];
                Now you can access the upper wicks of four previous bars by reading the stored info in upperWicksSeries[4], upperWicksSeries[3], upperWicksSeries[2] and upperWicksSeries[1].
                Same for the lower wicks.
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #9
                  Thank you, why did i get SUM couldnt be converted to double when i tried to calculate it?

                  Comment


                    #10
                    Its compiled when i changed it to this line
                    double sumWicks = (wickLongSizeSeries[1] + wickLongSizeSeries[2] + wickLongSizeSeries[3] + wickLongSizeSeries[4]);

                    Comment


                      #11
                      Hello tkaboris,

                      Thanks for your note.

                      You would need to provide an index ([0]) when calling SUM() to resolve this error. For example:

                      double sumWicks = SUM(wickLongSizeSeries, 4)[0];

                      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


                        #12
                        Thank you
                        when calculating lower wicks i get negative number and i need positive one. What can i do?

                        if (Low[1] <= Open[1])
                        {
                        lowerWicksSeries[1] = (Low[1] - Open[1]) / TickSize;
                        }
                        if (Low[2] <= Open[2])
                        {
                        lowerWicksSeries[2] = (Low[2] - Open[2]) / TickSize;
                        }
                        if (Low[3] <= Open[3])
                        {
                        lowerWicksSeries[3] = (Low[3] - Open[3]) / TickSize;
                        }
                        if (Low[4] <= Open[4])
                        {
                        lowerWicksSeries[4] = (Low[4] - Open[4]) / TickSize;
                        }​

                        upperWicksSeries[1] is 1
                        upperWicksSeries[2] is 0
                        upperWicksSeries[3] is 0
                        upperWicksSeries[4] is 2
                        --------------
                        lowerWicksSeries[1] is -6
                        lowerWicksSeries[2] is -6
                        lowerWicksSeries[3] is -6
                        lowerWicksSeries[4] is -6​
                        Last edited by tkaboris; 02-28-2023, 02:09 PM.

                        Comment


                          #13
                          Never mind i had to change to Open[1] - Low[1]/TickSize
                          Thank you

                          Comment


                            #14
                            When i run it on ES its working as suppose to. When i change to NQ i dont get a right results. I think sometimes 10 is actually no wick and should be 0. Maybe there some adjustments need to be done?

                            upperWicksSeries[1] is 0
                            upperWicksSeries[2] is 8
                            upperWicksSeries[3] is 10
                            upperWicksSeries[4] is 10
                            --------------
                            lowerWicksSeries[1] is 10
                            lowerWicksSeries[2] is 10
                            lowerWicksSeries[3] is 0
                            lowerWicksSeries[4] is 0
                            --------------
                            upperWicksSeries[1] is 0
                            upperWicksSeries[2] is 0
                            upperWicksSeries[3] is 8
                            upperWicksSeries[4] is 10
                            --------------
                            lowerWicksSeries[1] is 10
                            lowerWicksSeries[2] is 10
                            lowerWicksSeries[3] is 10
                            lowerWicksSeries[4] is 0​

                            UPDATED .. its working alright
                            Last edited by tkaboris; 02-28-2023, 03:04 PM.

                            Comment


                              #15
                              Hello tkaboris,

                              Thanks for your note.

                              You may add a CurrentBar print to your indicator prints and compare your indicator to the candles on the chart and the Data Box window to confirm your script is calculating correctly. The print might look something like this.

                              Print("wickSizeSeries[0] is " + wickSizeSeries[0] + " CurrentBar is " + CurrentBar);

                              You could see the CurrentBar value in the Output window, open a Data Box window by clicking the Data Box button on the chart, compare the CurrentBar print output to the candle on the chart with the same Bar Index number, see what the difference between prices are in the Data Box window, and calculate the number of ticks there is between the prices.

                              An example of this would be if you compare the High price of a bar to the Open price of a bar and there is a 0.5 price difference, this would be equal to 2 ticks. You would then compare that value to the print output for that bar to see if the print for that bar shows a wick size print of 2.

                              Ultimately, it will be up to your to debug your script and ensure that it calculates values correctly. If you see values are not calculating correctly, you would need to further debug your script and modify it accordingly.

                              See the help guide documentation below for more information.

                              Working with the Data Box: https://ninjatrader.com/support/help...dingTheDataBox
                              Debugging your NinjaScript code: https://ninjatrader.com/support/help...script_cod.htm

                              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

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by sukhob, 05-18-2023, 08:26 PM
                              2 responses
                              127 views
                              0 likes
                              Last Post TraderJA  
                              Started by timwey, Yesterday, 10:55 AM
                              4 responses
                              23 views
                              0 likes
                              Last Post timwey
                              by timwey
                               
                              Started by DJ888, 06-28-2024, 10:18 PM
                              3 responses
                              39 views
                              0 likes
                              Last Post AdamDJ8
                              by AdamDJ8
                               
                              Started by ntwong, Yesterday, 08:22 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post ntwong
                              by ntwong
                               
                              Started by Sigma Cypher, 01-08-2022, 06:56 AM
                              9 responses
                              1,170 views
                              0 likes
                              Last Post Tbryant
                              by Tbryant
                               
                              Working...
                              X