Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wrong Brush Used for Background in Fast Moving Market

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

    Wrong Brush Used for Background in Fast Moving Market

    I have an indicator the paints the background color based on various conditions. Normally, everything works fine and the correct background color is painted. However, when the market is moving fast (I am using volume bars), the prior background color is used even though conditions have changed and a new color should be used. The strange thing is that if I press F5 to reload the script, then the correct color is used. And as I said, if the market is moving at a 'normal' pace, the correct color is also used.

    Here's my code:

    Code:
    if(Close[0] > emaMid[0] && Close[0] > emaSlow[0]) {
    
    
    
    Brush myBrush = new SolidColorBrush(Color.FromArgb(faOpacity, faRed, faGreen, faBlue));
    
    myBrush.Freeze();
    
    BackBrush = myBrush;
    
    }
    
    else if(Close[0] < emaMid[0] && Close[0] < emaSlow[0]){
    
    Brush myBrush = new SolidColorBrush(Color.FromArgb(fbOpacity, fbRed, fbGreen, fbBlue));
    
    myBrush.Freeze();
    
    BackBrush = myBrush;
    
    }
    
    else{
    
    BackBrush = null;
    
    AboveBelow[0] = 0;
    
    }

    Any idea why this is happening?

    Thanks.

    #2
    Hello GoldBands,

    If you declare a variable in the scope of an if's action block, it can only be used in that action block.

    If you want to be able to set the variable and use this throughout the script, declare the variable of the scope of the class, and assign the value in State.DataLoaded.

    In the class:
    private Brush myBrush;

    In OnStateChange when State is State.DataLoaded:
    myBrush = new SolidColorBrush(Color.FromArgb(fbOpacity, fbRed, fbGreen, fbBlue));
    myBrush.Freeze();

    In OnBarUpdate():
    BackBrush = myBrush;​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Great. That fixed the issue. Thanks!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X