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

Color first bar in 5-minute window

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

    Color first bar in 5-minute window

    Hi all, happy new year,

    I am looking at the 1-minute chart. I would like to change the color of my price bar depending on its time.

    E.g. each first bar in a 5 minute time frame should be colored and therefore easily visible: 9:01 bar, 9:06 bar, 9:11 bar, 9:16 bar, etc.

    Ideally I can change the settings easily to show other time frames (e.g. 3 bar in 15 minute time frame): 9:03 bar, 9:18 bar, 9:33 bar, etc.

    The code below marks me the 9:06 bar red, but how can I do this every 5minutes? It seems I cannot loop the TimeSpan to increase by 5 each time... Is there a better way? It seems complicated to me...

    private TimeSpan start = new TimeSpan(09,01,00);
    private TimeSpan timeperiod = new TimeSpan(00,05,00);

    if (Times[0][0].TimeOfDay == start.Add(timeperiod))
    {
    BarBrush = Brushes.Red;
    }

    Thanks for any guidance!

    pennywise




    #2
    Hello pennywise,

    You could update the start variable when the time is less than the current bar time to 5 minutes a head.

    if (start <= Time[0])
    {
    start = start.Add(timeperiod);
    }

    Or you could have a condition that checks if the time Minutes is divisible by 5)

    if (start.Minutes % 5 == 0)
    Last edited by NinjaTrader_ChelseaB; 01-23-2022, 11:44 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Works. Thank you!

      Comment


        #4
        Hi ChelseaB,

        I have a follow up question: I know by now how to handle time but struggle with the date.

        Say on Thursday January 20th 2022 I want my bars red-colored and on Friday January 21st 2022 I want my bars green colored, and I want to keep the bars colored like that for all eternity, I would need to hard-code the date: How would that look like?

        I found Print("Date: " + DateTime.Now) which gives me Date: 2022-01-21 8:39:28 PM in my output window. But if I use that logic and get an error message: if (DateTime.Now == new DateTime(2022,01,21)) it does not recognize it.

        Do you know what I am doing wrong?

        pennywise

        Comment


          #5
          Hello pennywise,

          Provide the full error message for assistance with error messages.

          If I understand correctly:

          if (Time[0].Year == 2022 && Time[0].Month == 1 && Time[0].Day == 20)
          {
          }
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Thank you. Just earlier today I found something that works too:

            if (Time[0].Date == new DateTime(2022,01,20))
            {
            }

            Have a great week and thanks again. p

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by steve257, 12-20-2023, 05:32 AM
            5 responses
            435 views
            0 likes
            Last Post Nothinox  
            Started by javaburnaus, Today, 01:51 AM
            0 responses
            1 view
            0 likes
            Last Post javaburnaus  
            Started by XanderT, Today, 01:27 AM
            0 responses
            3 views
            0 likes
            Last Post XanderT
            by XanderT
             
            Started by anton_tymoshchuk, Today, 01:14 AM
            0 responses
            8 views
            0 likes
            Last Post anton_tymoshchuk  
            Started by marco231, Yesterday, 11:32 PM
            1 response
            7 views
            0 likes
            Last Post marco231  
            Working...
            X