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

Last minute of 5 minute bar?

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

    Last minute of 5 minute bar?

    Hey,

    Is it possible to make an indicator, say that starts at 0 and plots the ticks down/ticks up of the LAST minute of each 5 minute bar? So basically just plots what the instrument is doing in its last 1 minute on a 5 minute chart, so if it went down 10 ticks(in the final minute of that 5m bar), my indicator(just a line in its own window starting at 0 would be -10), then next 5 minute bar commences and it doesn't do anything until the last minute of it then if it goes back up 20 ticks(in the final minute), my indicator goes to +10 etc.

    Can someone help me with making this?

    Thanks!

    #2
    Hello Jumper,

    Thanks for your post.

    There are a couple of ways to do this. Here is a code example using Bars.PercentComplete. The example tests to see if the percent complete (of the 5 minute bar) is 80% and if so then it will print the tick difference between the last price before percent complete = 80% and the current close[0] price (rounded to ticks).

    Code:
    if (Bars.PercentComplete < 0.8) doItOnce = true;  // set one time flag
    			
    if (!Historical && Bars.PercentComplete >= 0.8)  // don't bother with Historical data
    {
    	if (doItOnce)      //  On the first time through...
    	{
    		lastprice = Close[0];  // save the current close price
    		doItOnce = false;      // set flag false so only once until next 5 minute bar
    	}
    Print ("ticks = "+Math.Round((Close[0] - lastprice) / TickSize));  // send to output window
    }
    Alternatively, and much simpler, you can plot both the 5 minute bar and a one minute bar on the sample chart and same panel, see attached example.

    Here is a video on how to create the chart example: http://screencast.com/t/sXCqDqx6I4m
    Attached Files
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks, that chart is nice but it still shows all the minutes, I just want it minimized down to the FINAL minute only of the 5 minute chart and to only show the outcome of all those final minutes of the 5m bars, plotted as a line, like RSI style, how can I turn that code into a plotted line?

      Thanks for the reply!

      Comment


        #4
        Hello Jumper,

        Thanks for your reply.

        If you are familiar with coding I suggest looking at an indicator such as the RSI or MACD and using that as a template of code (that plots in a lower panel) in which to insert the code and modify for output.

        If you are not familiar with coding we can certainly provide a list of coders that would be able to meet your needs.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Code:
          protected override void OnBarUpdate()
                  {
                      // Use this method for calculating your indicator values. Assign a value to each
                      // plot below by replacing 'Close[0]' with your own formula.
          			
          			
          			if(Bars.PercentComplete < 0.8) doItOnce = true; // set one time flag
          			
          			if(Historical && Bars.PercentComplete >= 0.8) // don't bother with Historical data
          			{
          				if(doItOnce) 	// on the first time though...
          				{
          					lastprice = Close[0]; // save the current close price
          					doItOnce = false; // set flag false so only once until next 5 minute bar
          				}
          			}
          			
          			Print("ticks= " + Math.Round(Close[0] - lastprice) / TickSize);
          			ticks = (Math.Round(Close[0] - lastprice) / TickSize);
          			Plot0.Set(ticks);
                  }
          Can you tell me why this isn't working? It seems to be just plotting the price, as in it looks exactly like the chart I have up, not JUST the final minute of each bar's progress.
          As shown below....Just looks like price, or RSI. What am I missing?
          Attached Files

          Comment


            #6
            Hello Jumper,

            Thanks for your reply.

            Please review the code example I provide and make note that the "print" statement is within the "{}" of the if statement: "if (!Historical && Bars.PercentComplete >= 0.8)".

            In what you coded, the print statement and the plot0 statement are outside of the "if" statement. This means that the print and plot0 will be executed everytime rather than the last minute of the bar as you wish.

            Please make sure that you set the Parameter CalculateOnBarClose = false so that the code will execute on every tick of data. If set to true the code would only execute once per bar at the end of the 5 minute bar and thus would not provide the 1 minute data requested.

            Finally, because the code is setting the number of ticks during the last minute, you will want to set the plot0 = 0 during the prior 4 minutes.

            Here is what I tested with: (replacing the contents of the RSI indicator)

            Code:
            		protected override void OnBarUpdate()
            		{
            			
            			if (Bars.PercentComplete < 0.8)  
            				doItOnce = true;			// set flag true when not 5th minute
            			
            			if (!Historical && Bars.PercentComplete >= 0.8)
            			{
            				if (doItOnce)  
            				{
            					lastprice = Close[0];  // This is price at the beginning of last minute
            					doItOnce = false;      // Set false until next candle
            				}
            				double ticks = Math.Round((Close[0] - lastprice) / TickSize); // determines ticks
            				Value.Set(ticks);   // output ticks value for plot
            			}
            			else
            				Value.Set(0);  // Otherwise output zero
            		}
            I've attached a picture of what to expect. Note that the indicator works on realtime (or market replace) data only so when historical it will show as a flat line.
            Attached Files
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              That's almost what I need, but on each bar it resets back to zero? How can I keep it continuing where it left off, so say the last minute of the previous bar it went -10 ticks, once the next bar starts, that -10 is going back to 0, I want to to plot the next 1 minute continuing on from that -10 ticks. It's obviously that final "else Value.Set(0)" but when I comment that out it's not plotting a line at all?

              Thanks for the reply too!

              Comment


                #8
                Ahh it actually seems to just sit on 0 until the next last 1 minute starts then it actually does continue on from where it left off. My bad

                EDIT: Nope it's resetting to 0 each time haha.
                Last edited by Jumper; 09-22-2015, 12:53 AM.

                Comment


                  #9
                  Hello Jumper,

                  Thanks for your posts.

                  Correct, as the code stands now it will reset to 0 outside of the last minute. To change this behavior you need only comment out the else statement and the Value.Set(0); statement (or remove/delete them) and recompile and reload the indicator on the chart.

                  Please see the code section below, the lines to comment out (or remove/delete) are shown with // and are colored green.

                  Code:
                  protected override void OnBarUpdate()
                  		{
                  			
                  			if (Bars.PercentComplete < 0.8)  
                  				doItOnce = true;			// set flag true when not 5th minute
                  			
                  			if (!Historical && Bars.PercentComplete >= 0.8)
                  			{
                  				if (doItOnce)  
                  				{
                  					lastprice = Close[0];  // This is price at the beginning of last minute
                  					doItOnce = false;      // Set false until next candle
                  				}
                  				double ticks = Math.Round((Close[0] - lastprice) / TickSize); // determines ticks
                  				Value.Set(ticks);   // output ticks value for plot
                  			}
                  			[COLOR="Green"]// else[/COLOR]
                  			[COLOR="green"]//	Value.Set(0);  // Otherwise output zero[/COLOR]
                  		}
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Yeah I said I did that in my post, I commented out the else statement and the line didn't plot at all anymore?

                    Comment


                      #11
                      Hello Jumper,

                      Thanks for your reply.

                      Yes, I see that you did write that, sorry I missed it.

                      Note sure what to tell you, the code I showed compiles and works. I've recorded a video (I had to use market replay just to show you something) to show how it works.

                      Note you could change from line type to bar type and thought you might actually prefer that (thats in the video) Other ideas would be to color code the bars based on their direction.

                      Anyway I suggest reviewing your code again against what i posted and see if you are missing something. Here is the video: http://screencast.com/t/qkJaSTWCyZvv
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Wow thanks for doing the video.

                        Only difference I can think is that I used a blank new indicator template, instead of replacing the code from the RSI, as we can't edit/save system indicators? So unsure how you did that. Must be something in the RSI code that is making your plotting work vs mine. Mine definitely isn't doing what your video shows.

                        Thanks for your help!

                        Comment


                          #13
                          Hello Jumper,

                          Thanks for your reply.

                          I've attached my sample code so that you can see all that it is. You can import and run as is (I included the coloring for fun). I've attached two pictures one with a line type and the other as a bar type.

                          Really I just used the RSI code as it has a single plot and it is just easier for me to start with something like that. You can always change any indicator. Open the indicator, right mouse click on the code and select save as, give it a new name and then it is your own to do with. I called mine RsiTest because quite frankly i didn't know what to call it!

                          Hope this helps.
                          Attached Files
                          Paul H.NinjaTrader Customer Service

                          Comment


                            #14
                            Awesome thanks so much for that Paul

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by geddyisodin, Yesterday, 05:20 AM
                            7 responses
                            45 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by gbourque, Today, 06:39 AM
                            2 responses
                            5 views
                            0 likes
                            Last Post gbourque  
                            Started by cre8able, Yesterday, 07:24 PM
                            1 response
                            13 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by cocoescala, 10-12-2018, 11:02 PM
                            6 responses
                            939 views
                            0 likes
                            Last Post Jquiroz1975  
                            Started by cmtjoancolmenero, Yesterday, 03:58 PM
                            1 response
                            17 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Working...
                            X