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

sum close in a series

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

    #16
    frankduc,

    To look at an index 200 less than the one we clicked on it would just require some simple math.

    Code:
    int clickedBarIndex  = ChartBars.GetBarIdxByX(chartControl, x);
    int modifiedBarIndex = clickedBarIndex - 200;
    You are going to need to figure out what to do when the user clicks on a point that does not have 200 bars before it.
    Josh G.NinjaTrader Customer Service

    Comment


      #17
      On the chart there is 260 bars between 15:16 and 16:00. Between 14:43 and 15:16 there is 130 bars.
      260/2+260 = 390 at 2.0 = 390

      If i do
      int modifiedBarIndex = clickedBarIndex - 200;
      It will only give me 260-200 = 60

      I want the sum of closes between 14:43 and cursorX, the way i see it i need the position at 14:43 or at least a way to count the 130 bars between 14:43 and cursorX.
      Is there an Nt method to do that? What kind of logic could create a Point Z at 14:43?

      This way i will be able to calculate an average close from 14:43 to 16:00 and not just from cursorX to 16:00
      Attached Files
      Last edited by frankduc; 05-21-2019, 08:31 AM.

      Comment


        #18
        frankduc,

        There is not a NT method that will do this for you and any sort of "Point Z" will need to be custom logic of your own creation. My sample should be a great start for you, but it will not be a final solution by any means.

        If you want to get a bar index by using a specific time you can use GetBarIdxByTime(): https://ninjatrader.com/support/help...ridxbytime.htm

        Perhaps another user on this forum has further advice for this situation as well.
        Josh G.NinjaTrader Customer Service

        Comment


          #19
          i have a rather simple question ... regarding the creation of a new "indicator" in 8 ... unlike 7 (where you have the option to create strategy OR indicator) ...
          i do not see the option to create a new indicator ONLY .... just the strategy builder what am i missing ??

          thanks

          Comment


            #20
            pfsmedical,

            You would need to use the NinjaScript Wizard inside the NInjaScript Editor to create other types of NInjaScripts

            Josh G.NinjaTrader Customer Service

            Comment


              #21
              Originally posted by NinjaTrader_JoshG View Post
              pfsmedical,

              You would need to use the NinjaScript Wizard inside the NInjaScript Editor to create other types of NInjaScripts

              https://ninjatrader.com/support/help...?ns_wizard.htm
              thanks josh ...

              Comment


                #22
                Is it possible to code a CMA on render that was intended for OnBarUpdate()?

                I have found a code of the CMA on the forum:


                {
                public class CMA : Indicator
                {
                private double sum = 0;

                protected override void OnStateChange()
                {
                if (State == State.SetDefaults)
                {
                Description = @"Cumulative Moving Average.";
                Name = "CMA";
                Calculate = Calculate.OnBarClose;
                IsOverlay = true;
                DisplayInDataBox = true;
                DrawOnPricePanel = true;
                DrawHorizontalGridLines = true;
                DrawVerticalGridLines = true;
                PaintPriceMarkers = true;
                ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                //See Help Guide for additional information.
                IsSuspendedWhileInactive = true;
                AddPlot(Brushes.Orange, "CumulativeMovingAverage");
                }
                }

                protected override void OnBarUpdate()
                {
                sum += Input[0];

                Value[0] = sum / (CurrentBar + 1);
                }

                #region Properties

                [Browsable(false)]
                [XmlIgnore]
                public Series<double> CumulativeMovingAverage
                {
                get { return Values[0]; }
                }
                #endregion


                I try to make it work on my indicator:

                I changed input for newsum (the region i want on the chart the CMA to apply)
                {
                sum += newsum+newsum1;

                Value[0] = sum / (CurrentBar + 1);
                }


                It compile but does not show any line on the chart. Dont know either what to do with region proprieties.

                Any suggestion?

                ty
                Last edited by frankduc; 05-22-2019, 12:34 PM.

                Comment


                  #23
                  frankduc,

                  It would be possible to recode something for OnRender() that was designed for OnBarUpdate() or vice-versa. But it would require completely different code.

                  I do not see anything that stands out as being incorrect in your snippet. It would really be up to you to debug your logic and see why it is not behaving the way you expect it to. I suggest adding Print() statements and monitoring the NS Output Window.

                  Josh G.NinjaTrader Customer Service

                  Comment


                    #24
                    I cant display the CMA line in the chart.

                    In CMA script to display the CMA in the chart you need AddPlot(Brushes.Orange, "CumulativeMovingAverage"); and that line is included in protected override void OnStateChange()


                    In my case the CMA is in protected override void OnRender(ChartControl chartControl, ChartScale chartScale) does that mean
                    AddPlot(Brushes.Orange, "CumulativeMovingAverage"); should end up in


                    public override void OnRenderTargetChanged()
                    {
                    textBrush = Brushes.Red.ToDxBrush(RenderTarget);
                    }

                    If its the case its not working. It seems to return the right average in the output window at least for the last point of the chart. But how can i get the rest of the cma result in the output window, because the output window only return the last point at the end of the chart and not the entire cma?
                    Anyway i cant create 2 Name in protected override void OnStateChange()? If i do that it will only change the name of the indicator.

                    Its like if i had 2 indicators in the same indicator. Do i have to rewrite all (State == State.SetDefaults) for the new indicator?

                    I know the last point of the average is the correct answer but cant say for the rest of the CMA. I dont need to see it in the chart (that would be practical) just need to see all cma points in the output to be sure all returns are ok.

                    One more info it seems the CMA in the output is updating like it was calculating on tick change and not bar change while it is Calculate = Calculate.OnBarClose; ?!?

                    ty
                    Last edited by frankduc; 05-23-2019, 12:02 PM.

                    Comment


                      #25
                      Hello frankduc,

                      I wanted to clarify the changes you made here.

                      You said that
                      In my case the CMA is in protected override void OnRender
                      Do you mean that you are calling the previously provided script in OnRender similar to CMA(..)[0], or have you added the CMA logic into OnRender?

                      AddPlot specifically will always remain in OnStateChange, this would not change and does not relate to using OnRender. OnRender is used to simply display data from "some" source. That source could be an existing plot or series or any data really. The Pivots indicator uses Plots and OnRender to display the plots in a custom way instead of a plot as an example.


                      But how can i get the rest of the cma result in the output window, because the output window only return the last point at the end of the chart and not the entire cma?

                      Can you provide the syntax for this part of the script?



                      I look forward to being of further assistance.

                      JesseNinjaTrader Customer Service

                      Comment


                        #26
                        Hello ,

                        Cant get all the closes list for clicedbarindex with that code. Its only giving me the close at cursorx everytime there's new transaction. What is the method to get all the closes and not just the close at the bar?

                        double closes= 0;


                        for (int i = 0; i <= clickedBarIndex; i++) //rightchart to cursorx
                        {
                        closes = Bars.GetClose(i);
                        }

                        Print(closes);

                        I have another issue if i do double newclose = closes1 - closes; //cursorx to endchart
                        Its doing a substraction between the closes of 2 different parts of the chart and i want only the closes list between cursorx and end of chart.


                        for (int i = 0; i <= clickedBarIndex; i++) //rightchart to cursorx
                        {
                        closes = Bars.GetClose(i);
                        }
                        for (int i = 0; i <= totalBarIndex; i++) //all chart
                        {
                        closes1 = Bars.GetClose(i);
                        }

                        double newclose = closes1 - closes; //cursorx to endchart




                        Is there a NT method to get CurrentBar to calculate ONLY in newclose or must i find myself a math logic to get there?

                        the CMA

                        protected override void OnBarUpdate()
                        {
                        sum += Input[0];

                        Value[0] = sum / (CurrentBar + 1);

                        Print(Value[0]);
                        }

                        In if (State == State.SetDefaults) i didn't know what to do with AddPlot(Brushes.Orange, "CumulativeMovingAverage"); so i changed for AddPlot(Brushes.Orange, ""SampleDisplayBarsAgo"");

                        I kept in proprieties:

                        [Browsable(false)]
                        [XmlIgnore]
                        public Series<double> CumulativeMovingAverage
                        {
                        get { return Values[0]; }
                        }
                        #endregion

                        Another thing puzzle me. Why it updates every tick instead of every bar in output window?

                        I think Input and Currentbar are necessary but they need to calculate only inside the zone i created (series of data of newclose)
                        Last edited by frankduc; 05-28-2019, 07:15 AM.

                        Comment


                          #27
                          Hello frankduc,

                          In what you provided you have a few problems.

                          Code:
                          for (int i = 0; i <= clickedBarIndex; i++) //rightchart to cursorx
                          {
                          closes = Bars.GetClose(i);
                          }
                          
                          Print(closes);
                          You are printing Closes after the loop finishes so you are only printing the last value that was set to the variable. If you wanted to print all values, you need to move the print inside the loop.

                          I have another issue if i do double newclose = closes1 - closes; //cursorx to endchart
                          Its doing a substraction between the closes of 2 different parts of the chart and i want only the closes list between cursorx and end of chart.
                          Then you would need to to know what cursorX is and the index of the last bar of the chart, the last bar of the chart is the ChartBars.ToIndex: https://ninjatrader.com/support/help...ghtsub=toindex

                          The GetClose method gets a Price, the variable "i" would be an index in your provided sample.

                          Is there a NT method to get CurrentBar to calculate ONLY in newclose or must i find myself a math logic to get there?
                          I am not completely certain I understand what you are asking here but the variable newclose is your variable, so that will be calculated how you want and will not be something the platform automatically calculates/creates for you. Much of what you are doing in this thread will require you to make a custom solution along with use the debugging tools available such as Prints to confirm what you are doing is correct.



                          Another thing puzzle me. Why it updates every tick instead of every bar in output window?
                          Lets go back to the previous comment that was made about converting this indicator to OnRender:
                          It would be possible to recode something for OnRender() that was designed for OnBarUpdate() or vice-versa. But it would require completely different code.
                          The code you have used is not valid for OnRender, this still needs to be done in OnBarUpdate exactly how it was being done before. The only part that would be different when using OnRender would be displaying the data. If you look at the Pivots indicator you can see how it uses Plots to store data from OnBarUpdate and OnRender to display the data in a custom way.

                          It is very likely that you should just retain the original indicator and call it from your code to get a value when needed:

                          Code:
                          CMA()[0]
                          or
                          CMA().GetValueAt(i) // used from OnRender
                          On render is called as a render loop meaning it is called very very frequently, and not specifically in line with the MarketData. This method is used for displaying visuals but not specifically calculating data. This is also out of context, so you cant use items like Close[0] Value[0] here because they have no meaning. You would need to instead use Bars.GetClose(indexOfBar) to get the value from a bar by its index, the same would go for a plot or indicators value using GetValueAt().

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

                          Comment


                            #28
                            Ok What i want is to create a CMA for the data series i am testing. In the chart it means from vertical blueline to the end (right side) of the chart. At barsago 1188 we have 2.62 = 1641 thats the vertical blue line.
                            2824.38 is the CMA input in the code. As you can see the cma does not appear on the chart only the final average in the price scale. But i want the CMA to start at the vertical blueline and draw on the chart. Is it possible in
                            OnBarUpdate
                            ?

                            First question why the CMA dont appear?
                            After that i want to send all close prices traded above the CMA to OnRender in my calculation to be used.

                            private SharpDX.Direct2D1.Brush textBrush;
                            private double sum = 0;

                            protected override void OnStateChange()
                            {
                            if (State == State.SetDefaults)
                            {
                            Description = @"Enter the description for your new custom Indicator here.";
                            Name = "SampleDisplayBarsAgo";
                            Calculate = Calculate.OnBarClose;
                            IsOverlay = true;
                            DisplayInDataBox = true;
                            DrawOnPricePanel = true;
                            DrawHorizontalGridLines = true;
                            DrawVerticalGridLines = true;
                            PaintPriceMarkers = true;
                            ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                            //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                            //See Help Guide for additional information.
                            IsSuspendedWhileInactive = true;
                            AddPlot(Brushes.Orange, "CumulativeMovingAverage");

                            }
                            else if (State == State.Historical)
                            {
                            //add event handler
                            if (ChartControl != null) ChartControl.MouseUp += ChartControl_MouseUp;
                            }
                            else if (State == State.Terminated)
                            {
                            //remove event handler
                            if (ChartControl != null) ChartControl.MouseUp -= ChartControl_MouseUp;
                            }
                            }

                            private void ChartControl_MouseUp(object sender, MouseButtonEventArgs e)
                            {
                            //cause a refresh to poll after up click
                            ForceRefresh();
                            }

                            protected override void OnBarUpdate()
                            {
                            sum += Input[0];

                            Value[0] = sum / (CurrentBar + 1);

                            Print(Value[0]);
                            }
                            Attached Files
                            Last edited by frankduc; 05-28-2019, 08:09 AM.

                            Comment


                              #29
                              Hello frankduc,


                              As you can see the cma does not appear on the chart only the final average in the price scale. But i want the CMA to start at the vertical blueline and draw on the chart.
                              Are you asking to not include the data prior to the blue line, as in calculate only from that point forward? Or do you just want to hide the plot before the line and retain the original calculated value?

                              In the first case of excluding the prior data, that would be more difficult because it appears that you are doing this selection by mouse. The mouse is not related to bar events or processing so you would have to then manually calculate the indicators value using the values between those points. It looks like all the indicator is doing is: sum / (CurrentBar + 1); So you would need to manually (using math) sum the values between those two points and then do the division with the Index you are rendering (assuming this is done in OnRender).

                              If you mean to just hide the plot before the line, that is much easier and would be nearly identical to how the Pivots indicator works in terms of rendering. In the Pivots indicator it calculates using OnBarUpdate and Plots the data. The Plots are hidden and OnRender uses Plot data to display the pivot lines. If this is what you are asking, you would just need to hide the existing plot and then replot the value for only the bars after the lines X point.

                              Is it possible in OnBarUpdate
                              Its possible to calculate the CMA normally from OnBarUpdate, you wouldn't be able to click a point in the chart and then go back and recalculate that indicator only for the span of data between the selected points. That would require manual calculations as that type of logic does not fall within how indicators calculate.


                              First question why the CMA dont appear?
                              If you have OnRender in the script and have not called base.OnRender(...) the plots are not going to be rendered. This is similar to how the Pivots indicator works, it hides its plots by not calling base.OnRender() so it can render the data differently.

                              After that i want to send all close prices traded above the CMA to OnRender in my calculation to be used.
                              This thread has had many questions so before we continue further, I wanted to make sure I am on the same page here. Is the overall goal to just display the indicator past the line, and the line is being added by clicking on the chart? Will the indicator also be calculated for the full series, or just past the line? Can you tell me if this is this a correct estimate of your goal?

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

                              Comment


                                #30
                                English is not my native language, sorry in advance if my explanation lack clarity, also i'm a newbie in coding.

                                As you can see in the chart this is the RMMA. It produce any ma you want for the time period. In this case 922 periods. I dont need to see the CMA's displayed in the chart. I just need the data.

                                I was hoping to set a time period in my indicator (proprieties) like 922 and automatically the indicator will calculate the 922 CMA's for that trend period. I dont want to have to cut the data myself in the chart. Well i could if there is no other choice, i suppose i could simply set 922 in data series proprieties. (not practical if i cant see the past and will have to constantly ajust the time period)

                                From what you said prior i thought it was impossible to code the CMA in Onrender that's why you gave me
                                CMA().GetValueAt(i) // used from OnRender



                                Suppose to calculate the zone i want like on the second chart attach i set only the period i need. The problem is if i do that everytime there is a new bar i lose 1 bar at the beginning at the chart (left side).
                                Can we use islock to freeze the chart at the number of periods i desired ?
                                If i set the number of period to 1367 bar like on the chart we add a +1 somewhere to keep the chart staying at the beginning at 10:56 always?
                                I think islock could do that?
                                The problem is can we use RMMA to set multiple CMA's from 10:56 to 8:59.


                                I try to understand how it works to find the most simple way to code the indicator.
                                "This thread has had many questions so before we continue further, I wanted to make sure I am on the same page here. Is the overall goal to just display the indicator past the line, and the line is being added by clicking on the chart? Will the indicator also be calculated for the full series, or just past the line? Can you tell me if this is this a correct estimate of your goal? "




                                Where's barsago 1188 on the chart is the point where i divide the volume between the uptrend (11:08 to 9:47) and the downtrend from (9:48 blueline to 11:08).

                                in the past. Eventually i will need every % from 1.00 to 4.24. So we create a CMA for every % but its more simple to calculate a CMA for all the trend and than calculate formulas results. Soon or later 2.62 will reach the top of the trend as the number of bars increase.



                                I suppose its difficult to figure out where i am going without the overall project. Lets say with the CMA coded i would be at 30% of the project. Without the CMA i cant get the volume traded above the CMA and cant code the formulas.
                                Attached Files
                                Last edited by frankduc; 06-01-2019, 04:26 PM.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by benmarkal, Yesterday, 12:52 PM
                                3 responses
                                23 views
                                0 likes
                                Last Post NinjaTrader_Gaby  
                                Started by helpwanted, Today, 03:06 AM
                                1 response
                                19 views
                                0 likes
                                Last Post sarafuenonly123  
                                Started by Brevo, Today, 01:45 AM
                                0 responses
                                11 views
                                0 likes
                                Last Post Brevo
                                by Brevo
                                 
                                Started by pvincent, 06-23-2022, 12:53 PM
                                14 responses
                                244 views
                                0 likes
                                Last Post Nyman
                                by Nyman
                                 
                                Started by TraderG23, 12-08-2023, 07:56 AM
                                9 responses
                                388 views
                                1 like
                                Last Post Gavini
                                by Gavini
                                 
                                Working...
                                X