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

max number of bars

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

    max number of bars

    Hello,


    I try to create an increment to get the volume backward from cursorX.

    for (int i = 0; i < 100; i++)

    for(int barIndex = ChartBars.GetBarIdxByX(chartControl, cursorPointX) - i; barIndex <= ChartBars.GetBarIdxByX(chartControl, cursorPointX); barIndex++)


    {
    sumOfVolumes4 += Bars.GetVolume(barIndex);
    }

    Print(sumOfVolumes4);


    i should be equal to 1,2,3 till 100 to the test the volume for every period. But its not returning what expected and i would like to know if there is a nt method to get the max number of bars till the beginning of the chart to prevent crashing. If there is 400 bars i would like to test the 400 bars and than put an if statement to text if the sum of volume = myvariable in volume.

    ty

    #2
    Hello frankduc,

    Thanks for your post.

    With your nested loops, you are creating potentially bad references since ChartBars.GetBarIdxByX(chartControl, cursorPointX) could be BarIndex 0, and i can be from 0 to 99. Bar Indexes start at 0 and cannot be negative.

    If you are trying to loop from the first bar on the chart to the BarIndex that you click on, I suggest using 0 as the starting index for your loop, and then to use ChartBars.GetBarIdxByX(chartControl, cursorPointX) as the end index for your loop.

    For example:

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
        double sumOfVolumes4 = 0;
    
        Point clickPoint = chartControl.MouseDownPoint;
        clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(clickPoint.X, chartControl.PresentationSource);
    
        for(int barIndex = 0; barIndex <= ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X); barIndex++)
        {
            sumOfVolumes4 += Bars.GetVolume(barIndex);
        }
    
        ClearOutputWindow();
        Print(sumOfVolumes4);
    }
    Enabling BarIndexes in your Data Box can help to visualize what the bar indexes on your chart are so it can be easier to understand how you need to write your loop to go over the proper bar indexes.

    Please let me know if you have any questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      I think i didn't explain myself correctly.

      In the chart the part between the red line and the right side the beginning of the chart = 7284000 contracts

      Divided by my ratio 1.27 = 5716000 contracts

      The blue line is the bar where there is 5 716 000 contracts from the red line or cursorpointx. The value of that bar is 1623

      So i am looking for bar 1623 that way i can set a vertical line or a blue dot at that bar. My ratio is moving it can be 1.38 or 2.1 etc. So the number of contracts will change so is the position of the bar.

      Do you understand where i am going.

      Ty
      Attached Files

      Comment


        #4
        Hello frankduc,

        Your goal sounds clearer, but please clarify if I am not following.

        As I understand, your intention is to find a certain index looping from a cursor point to the first bar of a chart when you have calculated enough volume. The loop I provided in Post #2 could be modified to start from your cursor point's index, and loop back to bar index 0. You could then put a condition in your loop to accumulate the volume, and then you could assign barIndex to another variable and then call break; to break out of the loop when you have found enough volume. I.E.

        Code:
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            double sumOfVolumes4 = 0;
            int foundIndex;
        
            Point clickPoint = chartControl.MouseDownPoint;
            clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(clickPoint.X, chartControl.PresentationSource);
        
            for(int barIndex = 0; barIndex <= ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X); barIndex++)
            {
                sumOfVolumes4 += Bars.GetVolume(barIndex);
                if (sumOfVolumes4 >= MyVariable)
                {
                    foundIndex = barIndex;
                    break;
                }
            }
        
            ClearOutputWindow();
            Print(sumOfVolumes4);
        }
        This will then leave you with the bar index for the amount of volume you are tracking from your cursor point going back. A similar approach can be taken to loop from your cursor point to the CurrentBar index to check in the other direction.

        I look forward to being of further assistance.
        JimNinjaTrader Customer Service

        Comment


          #5
          It seems to work but how do i get the value number of the bar at the blue line? There is no bargetbar to get her number like bar 1623.

          I mean without i cannot have my x,y position to create a visual object in the chart to tell me where myvariable and sumofVolumes4 are equal.

          Comment


            #6
            Hello frankduc,

            The code provided saves the bar index of the bar that satisfies the amount of volume that should be accumulated as "foundIndex." Since you are stepping back bar by bar in your loop, you would track the bar indexes you step through from your loop. There is not a built in NinjaScript method that does this.

            Please let me know if there is anything else I can do to help.
            JimNinjaTrader Customer Service

            Comment


              #7

              the way i see it i just need to Print(foundIndex); before the break and it return the exact bar! bar 895

              The problem if i try textstring the result it says the name foundIndex dont exist.
              Its like prisoner in the bracket.
              how to get foundIndex out of the bracket?
              Last edited by frankduc; 07-09-2019, 08:08 AM.

              Comment


                #8
                Hello frankduc,

                foundIndex is declared in the scope of OnRender, it will be available and can be accessed within that method, but would not exist outside of it.

                I have included a link to a publicly available C# resource on Scope below.

                Scope - http://www.blackwasp.co.uk/CSharpVariableScopes.aspx

                We look forward to being of further assistance.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Something i am unsure. Is
                  foundIndex = the number of bars backward from clickpointx meaning i have to sum the result of foundIndex + barsago

                  or

                  foundIndex = the number in barindex

                  if result is 784

                  In BarIndex does it mean its bar 784 of 2507?



                  thank you
                  Last edited by frankduc; 07-09-2019, 01:39 PM.

                  Comment


                    #10
                    Hello frankduc,

                    Code:
                     for(int barIndex = 0; barIndex <= ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X); barIndex++)
                    The loop counts from the first bar on the chart (int barIndex = 0) and adds 1 to the bar index (barIndex++) until it reaches the index of the click point barIndex <= ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X).

                    A foundIndex of 784 would be the the 784th bar on the chart. If you enable Bar Indexes in your Data Box, you will see this index in the data box when you put your mouse over that bar.

                    We look forward to being of further assistance.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Is it possible to make it calculate from clickPointX backward where in fact int barIndex =
                      ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X) and its going in the direction of 0?

                      I think i got it

                      for(int barIndex = ChartBars.GetBarIdxByX(chartControl, (int)clickPoint.X); barIndex <= ChartBars.ToIndex; barIndex--)
                      Last edited by frankduc; 07-11-2019, 08:17 AM.

                      Comment


                        #12
                        Hello frankduc,

                        Yes this is possible by modifying the loop to iterate backwards. This is not a NinjaScript concept but a general C# concept. I suggest reviewing educational C# materials so you can better understand how you can utilize loops to accomplish your goals.

                        I have included a publicly available resource below.

                        Iterate over numbers with for. Increment or decrement an index int from a start to an end value.


                        If you have any other NinjaScript specific inquiries, please do not hesitate to open a new ticket.
                        JimNinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by kempotrader, Today, 08:56 AM
                        0 responses
                        6 views
                        0 likes
                        Last Post kempotrader  
                        Started by kempotrader, Today, 08:54 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post kempotrader  
                        Started by mmenigma, Today, 08:54 AM
                        0 responses
                        2 views
                        0 likes
                        Last Post mmenigma  
                        Started by halgo_boulder, Today, 08:44 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post halgo_boulder  
                        Started by drewski1980, Today, 08:24 AM
                        0 responses
                        4 views
                        0 likes
                        Last Post drewski1980  
                        Working...
                        X