Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get the SMA for a specific range of time every day separately

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

    How to get the SMA for a specific range of time every day separately

    Good morning

    I need to calculate the SMA but for a specific range of time each day

    The most common scenario I could use an indicator like this would be to specify a range of time to calculate the SMA in the days loaded in the chart, but separately day by day, and for example graphically and numerically see the SMA calculated from 10am to 2pm every day, in order to see each SMA during that range of time in the last n days, and let’s say the indicator shows numbers like the next in the chart sub-panel:
    This past Friday SMA = 25, this past Monday SMA = 21, this past Tuesday SMA = 18, just as random numbers only to give the example that each day would have its separate result

    The idea would be to select the starting time and the ending time for example directly from the indicator settings or maybe an faster way

    I'm not able to find a solution for this or maybe a similar indicator that does something like what I described here. So, if there is an indicator already created to do this please provide me the reference to test it and see if it can do this what I need.

    Thank you!

    #2
    Hello futurenow,

    Thank you for your reply.

    To clarify, you want to basically get the average price between two times and print that to the chart for each day of the last X number of days, is that correct? The period would basically be the number of bars between the two times?

    I'm not aware of any indicator that exists that does this, however, you could set up your two times as inputs and for each bar you would use GetBar to get the bars ago value for the starting time, and calculate an SMA using that bars ago as your Period for the SMA, get the value, and assign that to a plot.



    I've had some time this morning so I've created a simple example of this. This indicator would only work on time based charts that would have a candle ending at the time specified, but could be modified to calculate off a 1 minute series so it would work regardless of time frame - I didn't get overly fancy, just wanted to give you an example starting point.

    Please let us know if we may be of further assistance to you.
    Attached Files

    Comment


      #3
      Thank you very much Kate for the explanations and for taking the time create the example as starting point.

      I will check the code inside and the information in the link you provided to see how to adapt it to what I need.


      Thank you and have a nice rest of the day!

      Comment


        #4
        Hello

        I've made some test and modifications and now I'm trying to add 2 format features in order to get something like in the picture below:

        1. to also print the date next to text for the day of the week

        2. To give some format to the text but the only I've gotten is to change the font color. but I also would like to change font, font size, brush area...



        About the date:

        I tried with a couple ways similar like the next example:

        Code:
        ...
        
        DateOfTheDay= Times[1][0];
        ...
        string DateOfTheDaystring = DateOfTheDay.ToString("r");
        ...
        Draw.Text(this, "MyText1" + CurrentBar, Time[0].DayOfWeek.ToString() + DateOfTheDaystring + " \n" + " SMA:" + Value[0].ToString(), 0, High[0] + (5 * TickSize), Brushes.Yellow);
        
        ...


        About the text format:

        The idea is to show the plotted text in a part where is not mixed with the chart, as shown in the attached picture, only with 2 decimals and everything shown in a cleaner way.

        for the format I tried some combinations following the guide and one example here in the forum:


        But I get the error "No overload for method "Text" takes X arguments "

        Here are some examples I tried to use in the example you attached above, inside 'OnBarUpdate()'
        Code:
        ...
        Draw.Text(this, "MyText1" + CurrentBar, Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString(), 0, High[0] + (5 * TickSize), TextPosition.Center, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true }, Brushes.Transparent, Brushes.Transparent, 25);
        ...
        Code:
        ...
        Draw.Text(this, "MyText2" + CurrentBar, Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString(), 0, High[0] + (5 * TickSize), 0, 20, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true }, Brushes.Transparent, Brushes.Transparent, 25);
        ...
        Code:
        ...
        Draw.Text(this, "MyText3" + CurrentBar, Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString(), 0, High[0] + (5 * TickSize), 0, 20, Brushes.Yellow, new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true }, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 25);
        ...
        Code:
        ...
        // I also tried this other way, but I also got error:
        
        NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true };
        
        Draw.Text(this, "MyText4" + CurrentBar, Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString(), 0, High[0] + (5 * TickSize), 0, Low[0], 5, Brushes.Yellow, myFont, TextAlignment.Center, Brushes.Black, null, 1);
        ...


        As visual guide, the result would be something like in the next picture, so the information we actually get in the example you posted just with some text format and shoing the date:


        Thank you

        Click image for larger version

Name:	Mock-up.png
Views:	433
Size:	60.7 KB
ID:	1168905

        Comment


          #5
          Hello futurenow,

          Thank you for your reply.

          Looks like you're not using the correct overloads for Draw.Text. The following works for me to display a box with that text and the SMA value trimmed to two decimal places, though you may want to tweak color and font settings:

          Code:
          NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true };
          Draw.Text(this, "MyText2" + CurrentBar, true,Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString("0.00"),0, High[0] + (5 * TickSize), 0, Brushes.Yellow, myFont, TextAlignment.Center, Brushes.Yellow, Brushes.Gray, 70);
          You can see all the options for overloads for Draw.Text in our help guide here:



          Please let us know if we may be of further assistance to you.

          Comment


            #6
            Originally posted by NinjaTrader_Kate View Post

            Looks like you're not using the correct overloads for Draw.Text. The following works for me to display a box with that text and the SMA value trimmed to two decimal places, though you may want to tweak color and font settings:

            Code:
            NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Consolas", 12) { Size = 14, Bold = true };
            Draw.Text(this, "MyText2" + CurrentBar, true,Time[0].DayOfWeek.ToString() + " SMA:\n\n" + Value[0].ToString("0.00"),0, High[0] + (5 * TickSize), 0, Brushes.Yellow, myFont, TextAlignment.Center, Brushes.Yellow, Brushes.Gray, 70);

            Thank you Kate, It worked well and now I can see where I had the mistake about the text format


            Just 2 extra details:

            1. Please check again the first part in my previous response about to also print the date of the day,
            e.g. "Tuesday 24-AUG-21 SMA: xxxx.xx'

            I've tried with some different similar ways as that example but I don't get the date plotted. I tried something similar in the past but to print a time of an event but no for a date of a specific day and in this indicator this would be useful to have a quick check in the chart without to see the time axis where if it is zoomed in then is not too friendly to see the date in a faster way.



            2. Now I'm seeing a kind of limitation about the indicator example at the moment to plot the visual text boxes and it is about when you select a range of time after 5:00pm ET then the Fridays days in the weeks get missed/skipped but in general the Fridays don't get plotted, but when you select a range of time on or before 5:00pm ET then the Fridays are plotted correctly, and the idea with the indicator is to work with some different range of time, with some of them after 5:00pm. I know markets close on Fridays 5:00pm ET until Sundays, but the rest of the days there are data after 5:00pm ET that is something needed and I don't know if is too complicated to specify that days like Fridays, Sundays or Holydays are special days where the market has special time, different than a standard day like today Aug 24 for example.


            Thank you


            Chart with Fridays:
            Click image for larger version

Name:	With Fridays.png
Views:	399
Size:	71.4 KB
ID:	1168952

            Chart with no Fridays:
            Click image for larger version

Name:	With no Fridays.png
Views:	343
Size:	72.6 KB
ID:	1168953
            Last edited by futurenow; 08-24-2021, 01:33 PM.

            Comment


              #7
              Hello futurenow,

              Thank you for your reply.

              Draw.Text can only handle one text color per drawing, you cannot change the color of just the date. You can format the date nicely like so:

              Time[0].ToString("MMMM dd, yyyy")

              You would have to use custom rendered text using SharpDX to mix the text colors. An example of rendering text using SharpDX can be found in the built in Sample Custom Render indicator.

              As far as dealing with Fridays when the end time is greater than the time the market closes on Fridays, the easiest would be to make another time input called FridayEndTime and use that to tell it when to print information on Fridays - set the end time to the Friday close time in your time zone. You'll also need to add some logic to stop it from printing the information twice on Fridays if the Friday end time is greater than the End Time you've already specified. I'm attaching an updated example script that illustrates this. You'd need to add additional logic for holidays, but this should suffice in most situations.

              Please let us know if we may be of further assistance to you.
              Attached Files

              Comment


                #8
                Good morning Kate!

                Originally posted by NinjaTrader_Kate View Post
                Draw.Text can only handle one text color per drawing, you cannot change the color of just the date. :
                Oh, the orange color in the guide above "Tuesday 24-AUG-21 SMA: xxxx.xx" was just as reference to highlight the relevant part, not exactly to give the date a different color, but of course thank you for the clarification



                You can format the date nicely like so

                Time[0].ToString("MMMM dd, yyyy")
                Yes, thank you. With this code portion I could change the date format, just modifying it for a custom format.



                As far as dealing with Fridays when the end time is greater than the time the market closes on Fridays, the easiest would be to make another time input called FridayEndTime and use that to tell it when to print information on Fridays - set the end time to the Friday close time in your time zone. You'll also need to add some logic to stop it from printing the information twice on Fridays if the Friday end time is greater than the End Time you've already specified. I'm attaching an updated example script that illustrates this. You'd need to add additional logic for holidays, but this should suffice in most situations.
                Thank you, now the example works with the "special" situation about Fridays that have different active hours in the day than the rest of the week days.



                Thank you very much for take your time for the examples and for give the explanations, everything well explained. Have a good day!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                578 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X