Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculate sum of previous wicks

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

    #31
    NinjaTrader_BrandonH

    ​​
    Last edited by tkaboris; 03-04-2023, 06:18 PM.

    Comment


      #32
      Hello tkaboris,

      Thanks for that information.

      I see you have a space between 'Series' and '<double>' when declaring your custom Series. Please try removing the spaces on lines 34 and 35 between 'Series' and '<double>' so they read as 'Series<double>' and try running a compile.

      Does the script successfully compile?

      From my previous reply: "where is sumUpperWicks being declared in your script? Please send a screenshot showing how this variable is being declared as well."

      Please also send me a screenshot showing how CumulativeWicks is being declared in your script.

      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


        #33
        HI
        it was declared as
        double sumUpperWicks = (upperWicksSeries[1] + upperWicksSeries[2] + upperWicksSeries[3] + upperWicksSeries[4]);
        However, as i was struggling yesterday i went with eDanny approach using for loop. It seems working. But I have a question if you would be so kind to provide your feedback

        How can i allow user input variable to select how many bars to look back, like 5 for example? I have this loop and i currently hard code how many bars to look back and i would want to be somewhere between 2-7. Is it doable?

        Code:
        protected override void OnBarUpdate()
                {
        
                    if (Bars.IsFirstBarOfSession)
                    {
                        currentPnl = 0;
                    }
                    if (CurrentBar < BarsRequiredToTrade)
                    return;
        
                    #region wicks calculation
                    upperWicksSeries[1] = High[1] - Math.Max(Open[1], Close[1]);
                    upperWicksSeries[2] = High[2] - Math.Max(Open[2], Close[2]);
                    upperWicksSeries[3] = High[3] - Math.Max(Open[3], Close[3]);
                    upperWicksSeries[4] = High[4] - Math.Max(Open[4], Close[4]);
                    upperWicksSeries[5] = High[5] - Math.Max(Open[5], Close[5]);
                    upperWicksSeries[6] = High[6] - Math.Max(Open[6], Close[6]);
                    upperWicksSeries[7] = High[7] - Math.Max(Open[7], Close[7]);
        
                    lowerWicksSeries[1] = Low[1] - Math.Max(Open[1], Close[1]);
                    lowerWicksSeries[2] = Low[2] - Math.Max(Open[2], Close[2]);
                    lowerWicksSeries[3] = Low[3] - Math.Max(Open[3], Close[3]);
                    lowerWicksSeries[4] = Low[4] - Math.Max(Open[4], Close[4]);
                    lowerWicksSeries[5] = Low[5] - Math.Max(Open[5], Close[5]);
                    lowerWicksSeries[6] = Low[6] - Math.Max(Open[6], Close[6]);
                    lowerWicksSeries[7] = Low[7] - Math.Max(Open[7], Close[7]);
                    if(CurrentBar > numBars)
                    {
                    UpperSum = 0;
                    LowerSum = 0;
                    for(int i = NumberOfBars+1; i > 0; i--)
                    {
                    UpperSum += upperWicksSeries[i];
                    LowerSum += lowerWicksSeries[i];
                    }
                    }
                    Print(UpperSum);
                    Print(LowerSum);
                    #endregion​
        ​​
        Last edited by tkaboris; 03-04-2023, 06:18 PM.

        Comment


          #34
          Hello tkaboris,

          Thanks for your note.

          In regards to the compile error, it seems like this might be a stale error that has been resolved.

          Please run a compile in the NinjaScript Editor. Once you successfully run a compile, close NinjaTrader, and then restart NinjaTrader. After restarting NinjaTrader, open a New > NinjaScript Editor window and test to see if the compile error persists.

          If the compile error persists after following the above steps, please send me a copy of the script so that I may look into this further.

          In regard to your second question, you could consider creating a user-defined property in your script called something like 'NumberOfBars' and setting the RangeAttribute property to allow a value of 2 to 7. That user-defined input could then be used in your script instead of hard-coding the value.

          Please review the help guide documentation below for more information.
          NinjaScriptProperty: https://ninjatrader.com/support/help...yattribute.htm
          RangeAttribute: https://ninjatrader.com/support/help...eattribute.htm
          Creating user-defined input parameters: https://ninjatrader.com/support/help...d_input_pa.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


            #35
            Thank you i did that and enterd 5 ad number of bars to look back. However in screenshot below it enters on only 3 bars and 4th is a bullish bar. Ideally i would want to enter 5 in numbers of bars to look back and it will enter only when there are consecutive 5 bearish bars for long.Click image for larger version

Name:	image.png
Views:	119
Size:	1.34 MB
ID:	1237602

            Comment


              #36
              Hello tkaboris,

              Thanks for your note.

              How have you set up your user-defined NinjaScriptProperty in the script?

              How exactly is the NinjaScriptProperty you created being used in the script?

              Have you added debugging prints to the script yet to see how exactly your logic is evaluating?

              If not, please add debugging prints to the script to see exactly how your script is calculating logic when using the NinjaScriptProperty. Add prints to the script that prints out the index being used in your loop, the user-defined property value, and the CurrentBar to see how your logic is evaluating.

              If you need assistance interpreting the prints in the Output window, save the output by right-clicking the Output window and selecting Save As... -> give the output file a name and save -> then attach the output from both computers to your reply.

              Below is a link to a forum post that demonstrates how to use prints to understand behavior.
              https://ninjatrader.com/support/foru...121#post791121

              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


                #37
                Hi, current code doesnt count how many "same consecutive bullish/bearish bars" to look back. If I set bars to look back to 4 and there was only 3 consecutive bars it still takes a trade Please look at attachment. Also there is issue with displaying negative numbers for lowerwicks series. I need positive number wicks
                NumberOfBars = 4;

                [NinjaScriptProperty]
                [Range(2, 7)]
                [Display(Name = "Number Of Bars", Description = "Maximum Cumulative Wicks of last x bars ", Order = 20, GroupName = "02. Trade 34B")]
                public int NumberOfBars
                { get; set; }​

                Code:
                protected override void OnBarUpdate()
                        {
                
                            if (Bars.IsFirstBarOfSession)
                            {
                                currentPnl = 0;
                            }
                            if (CurrentBar < BarsRequiredToTrade)
                            return;
                
                            #region wicks calculation
                            upperWicksSeries[1] = High[1] - Math.Max(Open[1], Close[1]);
                            upperWicksSeries[2] = High[2] - Math.Max(Open[2], Close[2]);
                            upperWicksSeries[3] = High[3] - Math.Max(Open[3], Close[3]);
                            upperWicksSeries[4] = High[4] - Math.Max(Open[4], Close[4]);
                            upperWicksSeries[5] = High[5] - Math.Max(Open[5], Close[5]);
                            upperWicksSeries[6] = High[6] - Math.Max(Open[6], Close[6]);
                            upperWicksSeries[7] = High[7] - Math.Max(Open[7], Close[7]);
                
                            lowerWicksSeries[1] = Low[1] - Math.Max(Open[1], Close[1]);
                            lowerWicksSeries[2] = Low[2] - Math.Max(Open[2], Close[2]);
                            lowerWicksSeries[3] = Low[3] - Math.Max(Open[3], Close[3]);
                            lowerWicksSeries[4] = Low[4] - Math.Max(Open[4], Close[4]);
                            lowerWicksSeries[5] = Low[5] - Math.Max(Open[5], Close[5]);
                            lowerWicksSeries[6] = Low[6] - Math.Max(Open[6], Close[6]);
                            lowerWicksSeries[7] = Low[7] - Math.Max(Open[7], Close[7]);
                            if(CurrentBar > numBars)
                            {
                            UpperSum = 0;
                            LowerSum = 0;
                            for(int i = NumberOfBars+1; i > 0; i--)
                            {
                            UpperSum += upperWicksSeries[i];
                            LowerSum += lowerWicksSeries[i];
                            }
                            }
                            Print(UpperSum);
                            Print(LowerSum);
                            #endregion​
                and my simplified logic code is here
                Code:
                if (GoLong
                
                                        && (Close[1] < Open[1])     // previous bar bearish
                //                        && (Close[2] < Open[2])     // previous bar bearish
                //                        && (Close[3] < Open[3])     // previous bar bearish
                //                        && (Close[4] < Open[4])     // previous bar bearish                                        
                                        && (UpperSum <= CumulativeWicks)                      
                                        && (Close[0] > Open[0]))​
                Click image for larger version

Name:	image.png
Views:	170
Size:	5.4 KB
ID:	1237627
                Attached Files

                Comment


                  #38
                  Hello tkaboris,

                  Thanks for that information.

                  If you set the user-defined input NumberOfBars to 4, the index in your loop ('i') will be 5 since i = NumberOfBars+1. This means that the last 5 upperWickSeries values will be incremented to the UpperSum variable. It also means that the last 5 lowerWickSeries values will be incremented to the LowerSum variable.

                  To see exactly what values are being incremented to the UpperSum and LowerSum variables, you should add prints inside the loop one line above the 'UpperSum' calculation that prints out the index 'i', upperWickSeries[i], and lowerWickSeries[i].

                  for(int i = NumberOfBars+1; i > 0; i--)
                  {
                  Print("i: " + i + " | upperWickSeries[i]: " + upperWickSeries[i] + " | lowerWickSeries[i]: " + lowerWickSeries[i]);
                  UpperSum += upperWicksSeries[i];
                  LowerSum += lowerWicksSeries[i];
                  }

                  This will allow you to see exactly what index is processing in the loop and what values upperWickSeries and lowerWickSeries are evaluating to on each iteration of the loop.

                  For your entry condition, it seems that you are setting CumulativeWicks to a value of 3. One line above the condition used to place the entry order, you should print out each value in the condition to place the order. This means you should print out GoLong, UpperSum, CumulativeWicks, Close[0], and Open[0] to understand exactly how those values in your condition are calculated. Also, add a print that prints out CurrentBar so that you know what bar index is processing.

                  If you need assistance with understanding the prints in the Output window, save the output by right-clicking the Output window and selecting Save As... -> give the output file a name and save -> then attach the output from both computers to your reply.

                  ​Please let me know if I may assist further.
                  Last edited by NinjaTrader_BrandonH; 03-02-2023, 02:47 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


                    #39
                    For some reason loop calculates ​ it wrong. I set numberof Bars to 4.
                    I am attaching file just in case that stalemate error still there after restart and new editor open


                    .​
                    Last edited by tkaboris; 03-04-2023, 06:18 PM.

                    Comment


                      #40
                      It also seem slike calculation for shorts not working.
                      I selected to look for 6 bars back with 0 cumulative wicks . there were wicks in the series and it still triggered the trade. Could there be specific calculation for shorts?
                      Last edited by tkaboris; 03-04-2023, 06:18 PM.

                      Comment


                        #41
                        Hello tkaboris,

                        Thanks for your note.

                        If NumberOfBars is set to 4, the loop will loop over the past 5 bars on the chart. For example, if the CurrentBar is 5518, the loop will loop over bar 5513, 5514, 5515, 5516, and 5517.

                        I added a print in the loop that prints out the CurrentBar and prints that print out the UpperSum and LowerSum values. In the screenshot below, we can see the the CurrentBar when the loop was iterated over was bar 5518.

                        [I]for(int i = NumberOfBars+1; i > 0; i--)
                        {
                        Print("i: " + i + " | upperWickSeries[i]: " + upperWicksSeries[i] + " | lowerWickSeries[i]: " + lowerWicksSeries[i] + " | CurrentBar: " + CurrentBar);
                        UpperSum += upperWicksSeries[i];
                        LowerSum += lowerWicksSeries;
                        Print("UpperSum: " + UpperSum + " | LowerSum " + LowerSum);
                        }


                        See the attached screenshot.

                        In the screenshot, the Output window we see the CurrentBar when the loop was iterated over was on bar 5518. Since we are iterating over 5 bars (NumberOfBars+1), we must look back 5 bars to compare the values for each index.

                        The Output window says index 5 in the loop (bar 5513 on the chart) has an upperWickSeries value of 0.25 and the lowerWickSeries value is -0.25. We compare the High and Low price to the Open price in a Data Box window for bar 5513. We can see there is a 0.25 difference between the High price and the Open price which equals the upperWickSeries print in the Output window for index 5. We see a -0.25 difference between the Low price and the Open price which is equal to the lowerWickSeries print in the Output window.

                        If we were to compare the previous 5 bar values similar to what is explained above, we can see the prints are matching the values of the previous 5 candles.

                        If we were to set the NumberOfBars value to 2, the script would loop over the previous 3 bars since the index of the loop would be 3 (NumberOfBars+1 = 3).

                        Ultimately, you would need to further debug your strategy to understand exactly how every value in your logic is processing. ​You could use this concept explained above for testing your script to see exactly how the values in the loop in the script are calculating and compare that to your conditions for placing trades. Please note that it would be up to you to fully debug the script and modify your custom logic to get the script to behave and place trades exactly as you are expecting it to.

                        Please let me know if I may assist further
                        Attached Files
                        <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


                          #42
                          NinjaTrader_BrandonH Thank you i think longs are working ok. however according to post 40
                          I see that if i set cumulative wicks to 0, it doesnt open Longs, but opens shorts. It indicates that negative wicks are not processed the way longs are. Can you please verify? I would greatly appriciate it
                          Last edited by tkaboris; 03-03-2023, 09:59 AM.

                          Comment


                            #43
                            As you see when calculating lower wicks it gives out negative numbers and since its negative it doesnt calculate them at all
                            -2 technically should be 0. how can we make it happen?

                            Click image for larger version  Name:	image.png Views:	0 Size:	1.01 MB ID:	1237733
                            Last edited by tkaboris; 03-03-2023, 10:26 AM.

                            Comment


                              #44
                              To eliminate negative numbers you just need to reverse your math.
                              You have:
                              upperWicksSeries[1] = High[1] - Math.Max(Open[1], Close[1]);
                              which gives a positive upper wick of the previous bar, and:
                              lowerWicksSeries[1] = Low[1] - Math.Max(Open[1], Close[1]);
                              which gives a negative lower wick of the previous bar. Change the order of subtraction and use Math.Min to choose the lower of Open and Close and get a positive number:
                              lowerWicksSeries[1] = Math.Min(Open[1], Close[1]) - Low[1] ;
                              eDanny
                              NinjaTrader Ecosystem Vendor - Integrity Traders

                              Comment


                                #45
                                Thank you , it worked

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by NullPointStrategies, Today, 05:17 AM
                                0 responses
                                51 views
                                0 likes
                                Last Post NullPointStrategies  
                                Started by argusthome, 03-08-2026, 10:06 AM
                                0 responses
                                128 views
                                0 likes
                                Last Post argusthome  
                                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                                0 responses
                                69 views
                                0 likes
                                Last Post NabilKhattabi  
                                Started by Deep42, 03-06-2026, 12:28 AM
                                0 responses
                                42 views
                                0 likes
                                Last Post Deep42
                                by Deep42
                                 
                                Started by TheRealMorford, 03-05-2026, 06:15 PM
                                0 responses
                                46 views
                                0 likes
                                Last Post TheRealMorford  
                                Working...
                                X