Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing Background during Night Session

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

    Drawing Background during Night Session

    While it is the best way/ method to draw a background from a certain time to another time with NT8? A method that is efficient and doesn't have strange re-drawings.
    Thank you.
    G

    #2
    Hello giogio1,

    The most simple way to color the background is to use BackBrush. That will color the background on the bar where the condition is true. To color the background for a time you can make a time condition to call BackBrush. There are samples of these concepts in the following links.



    Comment


      #3
      Thank you Jesse, perfect!

      A question , with this code:
      BackBrush = nightColor;
      everything works and nightColor is a System.Windows.Media.SolidColorBrush.

      ​but in State.Configure I set
      nightColor = BrushFromArgb(opacityBackground, NightColor);

      then in OnBarUpdate() I get.the error "the calling thread cannot access this object because a different thread owns it"

      Can you explain me why this is happening?
      The object nightColor has been fully defined during the State.Configure and no one else should access it.
      Can you explain and how to correct it?
      Thank You!
      Gio

      Comment


        #4
        Hello Gio,

        You will need to freeze the brush.

        See the section 'Understanding custom brushes' in the help guide page Working with Brushes.


        As well as the section 'Using WPF brushes' in the help guide page NinjaScript Best Practices.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you. I've read again and it's pretty much all clear now, and I can use them now.

          I also remember I made this question https://forum.ninjatrader.com/forum/...-sharpdx-color

          where you answered me "A SharpDX brush can only be used in the context of OnRender() and cannot be used in other methods like OnStateChange()."

          I also well understand that custom brushes need to be frozen when used because they are not thread-safe, very clear.
          I understand freeze() as synonym of locking an object in multithreading programming.

          At this point the question is (particularly the third point):
          can we say that:
          - in a OnRender() method we should use SharpDX brushes
          - in a OnRender() method we should NOT use System.Windows.Media.Brushes
          (at this point I don't see the need to use these System.Windows.Media.Brushes at all, also because of the conversion overhead).
          - we end up creating a SharpDX brush each time we enter a OnRender() method (and destroy it when we exit the method)
          ?

          Thank you.
          G

          Last edited by giogio1; 10-17-2024, 05:09 AM.

          Comment


            #6
            Hello giogio1,

            Your understanding is correct, OnRender requires DX objects like DXBrush. For simple use cases you can just create the brush right in OnRender. If you have a lot of brushes its suggested to use OnRenderTargetChanged, there is a sample of managing brushes here: https://ninjatrader.com/support/help...getchanged.htm

            Comment


              #7
              Great!, it's all getting more and more clear


              - If I have well understood the OnRenderTargetChanged()​ callback let us minimize the number of times we recreate the brush resources (and of of course the other listed in the documentation too)
              as compared to recreating them at each call to OnRender(). Correct?



              - Maybe this is the last question, I remember many months ago I've read that the NinjaTrader framework freezes() the resources if I forget doing it... do I remember well? then when I read I was left with the question... if NT does it for me why you ask the user to do it?





              Comment


                #8
                Hello giogio1,

                That is correct, that is only called when the render target changes which would require re creating brushes.

                I am not aware of any auto freezing situations, we generally suggest to make sure you are managing all resources correctly in your code to avoid any problems.

                Comment


                  #9
                  Is there more information or examples about using custom brushes as background colors? Is it possible?

                  I added this line to my public class section

                  private Brush SoftGreen;

                  if(State == Configure) section

                  // initiate new solid color brush with customcolor
                  Brush SoftGreen = new SolidColorBrush(Color.FromRgb(193, 245, 207));
                  SoftGreen.Freeze();


                  Later, when I try to call the SoftGreen color from the BackBrush commands it says Brushes does not contain a definition for this color.
                  (That makes sense, because it is not a default Brushes color).

                  I also tried:
                  BackBrush = Brush.SoftGreen;
                  and BackBrush = SoftGreen;

                  I'm hoping you all might have some syntax or reference page to point me in the right direction....?
                  Thanks,

                  Berry





                  Comment


                    #10
                    Hello Berry Trader,

                    The Brushes collection is the C# default brushes, your custom brushes don't get listed with those. To use your custom brush you would use SoftGreen where you want to use it.

                    Your sample is defining a local variable so you are not acctually assigning anything to the private variable, you need to change the following line:

                    Brush SoftGreen = new SolidColorBrush(Color.FromRgb(193, 245, 207));

                    to
                    SoftGreen = new SolidColorBrush(Color.FromRgb(193, 245, 207));

                    BackBrush = SoftGreen;

                    Comment


                      #11
                      Jesse,
                      That worked just as you suggested. This is a good time for me to say that you and all your tech support coworkers do an awesome job. You have always answered my questions with kindness, efficiency and it is obvious to me that you read my questions carefully. May you *never*, *never*, *never* be replaced by bots!

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      649 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      370 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      109 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      574 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      576 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X