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

Data box data script

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

    #16
    Hello frankduc ,

    Thank you for your reply.

    I use a volume chart 400 period. The calculation is automatic and formulas return the answer as a Bar.
    On minute chart Bar won't return in volume, it return in numbers of bars. I want it to return in volume on a minute chart. IB dont backfill overnight data.
    Yes, this syntax was intended for your original question and to demonstrate what syntax is used to find bar indexes and then convert to a BarsAgo. The word "bar" here is simply a variable name, this is not returning a bar specifically but a number of BarsAgo per your original question. This line of syntax also includes the subtraction from the ToIndex to give a BarsAgo from the ToIndex. If you wanted to return a volume you would need to use an index to pick a bars volume from the Volume series. You can use the index you get back from GetBarIdxByX in many ways to retrieve all types of data from various series the script has access to.

    So i get tick data only when i start NT in the morning. I need a way so in a minute chart the answer return is in volume not in number of bars. In my chart exemple 2.62 = 226 bars but in a minute chart 226 dont equal the same in volume.
    What do i need to change to make it return in volume
    In my previous statement I address why you are not getting volume and instead getting BarsAgo, however, I am not certain what you are asking here in relation to the tick and minute series. How does this factor into your question?

    The syntax we are speaking about is simply for converting an X value to an Index of a bar, however, this would not have any relation to multiple series and only the primary panel which the indicator is in. The ChartBars object used references specifically the primary series.

    The problem is i dont understand how to introduce
    GetValueAt .
    This method can be used with any series and takes an index, you can use it for volume like the following:

    Code:
    double volume = Volume.GetValueAt(someIndex);

    That is what i am asking. What is the method to use to link the return of my formulas and the creation of the line.
    In
    RenderTarget.DrawLine(startPoint, endPoint, areaBrushDx, 4);
    could it be
    RenderTarget.VerticalLine(............?); What code do we use? you just cant plug in (
    bar.ToString() + " \n 2.62
    ) and a vertical line appear at bar 226

    If I understand you must teach with a method how to find bar 226 in the chart. Its specific position x, y and than it can draw a vertical line.
    Is there only a positioning for the bar? A code that tells you where is bar 226 and than you use rendertarget to draw the line?
    You are correct you would need to work out the math to calculate that bar. Based on the line of questions surrounding the clicked bar, the prior samples have related to that and how to calculate BarsAgo from that point. I had also previously shown how to convert the BarsAgo back to an X value for rendering so the code required for this task is all here. You would then use the X and Y values you calculated with RenderTarget.DrawLine to have the line appear on the chart. I would suggest just experimenting with OnRender and the samples to gain a stronger understanding of the rendering process as this will help. I can also suggest using Print statements to output the values as you are calculating them.


    I look forward to being of further asistance.
    JesseNinjaTrader Customer Service

    Comment


      #17
      I tried
      int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
      double volume = Volume.GetValueAt(bar);

      But calculation still return in number of bars and not in volume (numbers of contracts) on a minute chart.
      double volume = Volume.GetValueAt(someIndex);

      What is some index? I know what is an index. What does it want more?
      I tried


      int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
      {
      for (bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex))
      double Volume = Volume.GetVolume(bar);
      }

      Bar is an objet? Its been define has a series of bars (the index)

      I tried int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
      double volume = Volume.GetValueAt(Math.Abs(ChartBars.GetBarIdxByX( chartControl, cursorPointX) - ChartBars.ToIndex));

      and than i changed bar for volume

      string textString = "BarsAgo:" + volume.ToString()

      At bar it return 457 when there is 1311 bars.
      457 is not an answer in volume.
      Somethings wrong.

      I dont get it.
      Sorry Ty
      Last edited by frankduc; 03-04-2019, 06:48 AM.

      Comment


        #18
        Hello frankduc ,

        Thank you for your reply.

        I tried
        int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
        double volume = Volume.GetValueAt(bar);

        But calculation still return in number of bars and not in volume (numbers of contracts) on a minute chart.
        Correct, the calculation provided is for calculating a number of BarsAgo based on your first question, You now have other goals which will require different syntax than what we have been using. A BarsAgo is only relevant to the CurrentBar being processed, or in this case the last bar on the chart the ChartBars.ToIndex. An index runs the opposite way and starts with the first bar on the chart being index 0 and moves forward on from there ending with ChartBars.ToIndex. A BarsAgo cannot be used with GetValueAt because it is not an index but is a number of bars from the current bar.


        What is some index? I know what is an index. What does it want more?
        someIndex is just a placeholder for an index you might use, this is just for example to let you know to enter a value there. This is a good point to note that reviewing the help guide for each of these items will provide a sample of the syntax which you can compare to understand the syntax being used better. The help guide shows using an integer in the method to retrieve the value for an index: https://ninjatrader.com/support/help...sub=GetValueAt


        int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
        {
        for (bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex))
        double Volume = Volume.GetVolume(bar);
        }

        Bar is an objet? Its been define has a series of bars (the index)

        bar in this use case is the name of a variable. This is an integer as it is a number of bars ago and is defined as an int: int bar. This cannot be used as you have shown because this is not an index, this is what you had originally asked for in this post which was the calculated BarsAgo.




        I look forward to being of further assistance.





        JesseNinjaTrader Customer Service

        Comment


          #19
          Hello,

          I would like to know if its possible to link two indicators.
          In my indicator the numbers of barsago = 164 . Is it possible to create a SMA 164? Everytime i would click on the chart and get a new numbers of barsago the sma will automatically update to the number in the indicator.

          thank you

          Comment


            #20
            Hello frankduc,

            It would not be possible to update an existing indicator on the chart, however, the found BarsAgo could be used from inside the same script to calculate values. Because the action is random (mouse click) the script would not be able to calculate historical data over again, it would just be able to use a new value going forward from the time when you clicked. More advanced logic may be able to be added to recalculate historical bars for the indicator, however, this use case is not something we would have any sample for.

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

            Comment


              #21
              Hello,

              I am trying to create a calculation similar to the previous one using volume instead.

              int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
              double volume = Volume.GetValueAt(bar);

              int bar1 = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
              double price = Price.GetValueAt(bar1);

              I get an error message saying price dont exist in the context. What am i doing wrong?
              So i can use bar1 for exemple in this calculation : ((bar / 3)+bar+bar1).ToString()

              Comment


                #22
                Hello frankduc,

                If you have not created a series named "Price" then the error would be correct, Price is not a NInjaScript property name. You could use "Close" instead if the Close price is what you are trying to get.




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

                Comment


                  #23
                  I did the change for Close:

                  int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                  double volume = Volume.GetValueAt(bar);

                  int bar1 = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                  double close = Close.GetValueAt(bar1);

                  here's some of my calculation:

                  " \n 3.20: " + ((bar1 / 3.20)+bar1).ToString()

                  But it still return the answer in volume instead of close price. Like if it was " \n 3.20: " + ((bar / 3.20)+bar).ToString()
                  it should sum all close 2909 + 2909.25 + etc and not 400 + 400 + etc

                  Any idea why?

                  Thank you

                  Comment


                    #24
                    Hello frankduc,

                    From what you provided, I would not be sure what the problem may be. Are you able to provide a more complete sample of the code used that I can try as well?

                    Based on the description and the result not changing, it sounds like potentially you have not compiled but just saved the script or the last working version is being used.

                    Can you confirm you had compiled and reapplied the script?



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

                    Comment


                      #25

                      //get the X and Y point of the cursor and convert them to pixels
                      int cursorPointX = ChartingExtensions.ConvertToHorizontalPixels(chart Control.MouseDownPoint.X, chartControl);
                      int cursorPointY = ChartingExtensions.ConvertToVerticalPixels(chartCo ntrol.MouseDownPoint.Y, chartControl);

                      //get the right most bar ToIndex, and subtract the current cursor point to get a BarsAgo
                      int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                      double volume = Volume.GetValueAt(bar);

                      int bar1 = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                      double close = Close.GetValueAt(bar1);

                      //define a font
                      NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);

                      // the advantage of using a SimpleFont is they are not only very easy to describe
                      // but there is also a convenience method which can be used to convert the SimpleFont to a SharpDX.DirectWrite.TextFormat used to render to the chart
                      // Warning: TextFormat objects must be disposed of after they have been used
                      SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();

                      // Once you have the format of the font, you need to describe how the font needs to be laid out
                      // Here we will create a new Vector2() which draws the font according to the to top left corner of the chart (offset by a few pixels)
                      SharpDX.Vector2 upperTextPoint = new SharpDX.Vector2(cursorPointX, cursorPointY);
                      // Warning: TextLayout objects must be disposed of after they have been used


                      .I thought bar1 equal the sum of last bars in close price like its the case in volume. I did compile and than i removed in the list of indicators and add it again in the indicators list. did it several time.
                      I want to be able to divide multiply values in volume, close price and in bars so i can create a average.

                      ty
                      Last edited by frankduc; 05-08-2019, 07:52 AM.

                      Comment


                        #26
                        Hello frankduc,

                        I see you are using Bar1 in your calculation, however, that would be a barsago, not a price or volume.

                        Code:
                        int bar1 = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                        You would need to use the variables you created which contain the volume or price. you are currently not using the volume either.

                        Code:
                        double [B]close [/B]= Close.GetValueAt(bar1);
                        bar and bar1 in your example would represent a barsago. The variable below where you have bar and bar1 would be what should be used here or volume and close. It does look like you used the BarsAgo though instead of using the Index provided by GetBarIdxByX.

                        Code:
                        double volume = Volume.GetValueAt(bar);
                        double close = Close.GetValueAt(bar1);

                        I look forward to being of further assistance.
                        Last edited by NinjaTrader_Jesse; 05-07-2019, 12:20 PM. Reason: corrected wording
                        JesseNinjaTrader Customer Service

                        Comment


                          #27
                          I dont get it. If i remove
                          int bar1 = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                          and keep only
                          double volume = Volume.GetValueAt(bar);
                          double close = Close.GetValueAt(bar1);

                          It returns an error message saying bar1 dont exist.
                          I just want bar for the volume (thats ok) and bar1 for the price and bar2 for the number of bars. So i can input them in the calculation.
                          What do i have to do?

                          If i dont identify double close has bar1 how will i be able to differentiate volume from close price in my formulas? If i just replace volume by close i will end up with calculation base on close.

                          Comment


                            #28
                            Hello frankduc,

                            I am not certain I am following what you are trying to do here, a few posts ago you asked:

                            I am trying to create a calculation similar to the previous one using volume instead.
                            In addition to this question, you noted:

                            I get an error message saying price dont exist in the context. What am i doing wrong?
                            I provided an explanation of the error, however, you provided the necessary syntax to get the volume index or its BarsAgo so that part is already here. Going back to this question, what volume are you trying to get? You already have the syntax to get the volumes index or its BarsAgo, can you further explain the problem with getting the volume?


                            The reason for the new error is that the bar and bar1 variables are used later by the GetValueAt syntax so that is still needed however I do see that would need to be modifed if you are trying to retrieve the actual values. Before we continue further, are you trying to retrieve the amount of volume or the bars ago? What you provided gets the BarsAgo which is what was asked early on in this post. The other syntax for getting the index is also included in that syntax as well so if you wanted to get volume at a specific index, the ChartBars.GetBarIdxByX can be used for that purpose.



                            I look forward to being of further assistance.







                            JesseNinjaTrader Customer Service

                            Comment


                              #29





                              bar = cumulative volume of last bars.
                              I want to introduce the close price in the calculation and numbers of bars (candle)

                              Now what i did is replace bar1 in
                              " \n 1.00: " + ((bar1 / 1.00)+bar1).ToString(); by close " \n 1.00: " + ((close / 1.00)+close).ToString();

                              It suppose to give me the closing price for the last number of bars (in the exemple last 5 bars ago) so 2910.25 * 5 = 14551 but as you can see it returns 2927. It make no sense to me it should return 2910.25 * 5. It doesn't sum the last 5 closing price. It just give me the close and its not even the right close price.

                              Than i will use the number of bars to divide by 5 and get the average price of the last 5 close price for exemple.

                              Understood?
                              TY
                              Attached Files
                              Last edited by frankduc; 05-08-2019, 07:52 AM.

                              Comment


                                #30
                                Hello frankduc,

                                I believe this is where the confusion is if you are trying to gather the price you would need to use the close variable as I had previously noted, however you need to supply the Index instead of the BarsAgo to get the price from a specific bar. I previously I copied/pasted the whole BarsAgo syntax from one of your prior posts to show you what syntax was needed, however, I provided a more specific description in post 12 of this syntax:

                                Im not sure how this is helping me
                                int bar = Math.Abs(ChartBars.GetBarIdxByX(chartControl, cursorPointX) - ChartBars.ToIndex);
                                This is what is required to calculate the BarsAgo. This takes the cursors X value and gets the bars Index then subtracts that from the bars count. This gives a difference in BarsAgo.
                                This is calculating a BarsAgo which is what you originally wanted. This uses part of the syntax you would need to get the index of the bar which would be used with GetValueAt():
                                Code:
                                ChartBars.GetBarIdxByX(chartControl, cursorPointX)
                                The value GetBarIdxByX produces is the index of the bar, this can be used with GetValueAt to get that bars values by its index. In your previous post, it looks like you are trying to supply BarsAgo for the index which is likely not going to be the value you had expected.


                                Please let me know if I may be of further assistance.
                                JesseNinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by judysamnt7, 03-13-2023, 09:11 AM
                                4 responses
                                59 views
                                0 likes
                                Last Post DynamicTest  
                                Started by ScottWalsh, Today, 06:52 PM
                                4 responses
                                36 views
                                0 likes
                                Last Post ScottWalsh  
                                Started by olisav57, Today, 07:39 PM
                                0 responses
                                7 views
                                0 likes
                                Last Post olisav57  
                                Started by trilliantrader, Today, 03:01 PM
                                2 responses
                                21 views
                                0 likes
                                Last Post helpwanted  
                                Started by cre8able, Today, 07:24 PM
                                0 responses
                                10 views
                                0 likes
                                Last Post cre8able  
                                Working...
                                X