Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to change object property from OnBarUpdate() method

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

    Unable to change object property from OnBarUpdate() method

    I created a grid with some buttons and I need to change the color of these buttons using the OnBarUpdate() method.
    The object is created using the code below:

    Dispatcher.InvokeAsync((() => {
    TrafficLight = new System.Windows.Controls.Grid {
    Name = "MyCustomGrid2", HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Top
    };

    System.Windows.Controls.ColumnDefinition col0 = new System.Windows.Controls.ColumnDefinition();
    System.Windows.Controls.ColumnDefinition col1 = new System.Windows.Controls.ColumnDefinition();


    TrafficLight.ColumnDefinitions.Add(col0);
    TrafficLight.ColumnDefinitions.Add(col1);

    VWAP = new System.Windows.Controls.Button {
    Name = "VWAPButton", Content = "VWAP", Foreground = Brushes.White, Background = Brushes.Black
    };

    EMA = new System.Windows.Controls.Button {
    Name = "EMAButton", Content = "EMA", Foreground = Brushes.White, Background = Brushes.Black
    };

    System.Windows.Controls.Grid.SetColumn(VWAP, 0);
    System.Windows.Controls.Grid.SetColumn(EMA, 1);

    TrafficLight.Children.Add(VWAP);
    TrafficLight.Children.Add(EMA);

    UserControlCollection.Add(TrafficLight);
    }));
    }

    I have a few more codes to display and destroy the object, but these codes are not relevant to my problem, as far as I know.

    This code add two buttons at the top of the screen. Everything was working, until I decided to change the background color based on some criteria on the OnBarUpdate() method.
    To accomplish this task, I had to use a dispatcher..

    ChartControl.Dispatcher.InvokeAsync((() => {
    if ( VWAP1.PlotVWAP[0] < Close[0]) { // When inspecting these values, they never change
    VWAP.Background=Brushes.Green;
    else
    VWAP.Background=Brushes.Red;
    }​
    Print(String.Format("VWAP: {0}", Close[0])); ///This code prints
    if(EMAL[1] < EMAH[1] ) {
    EMA.Background=Brushes.Red;
    else
    VWAP.Background=Brushes.Green;
    }​​​
    Print(String.Format("EMA: {0}", Close[0])); ///// This code never prints
    }));​

    Again, this code works, but when inspecting the variable values, they are off of the real ones.
    Inside the invoke, Close[0] doesn't change at all, while outside, the value is correct and changing as the price changes on the chart.

    Also, when I use the same invoke, or a new invoice, to change the other object EMA, it never executes.

    I can't figure out what is wrong with the code.

    Please help!


    #2
    Hello chbruno,

    Thanks for your post.

    You would call the ChartControl.Dispatcher within the condition in OnBarUpdate() and set the Background color of the button in the Dispatcher.

    Please see the reference sample on the forum thread linked below that demonstrates changing the color of a button to green when the Close price is greater than the Open price and red when the Close price is less than the Open price.

    Is this even possible? If so, would you give me a general direction? I've tried if (condition == true) { one set of chart button specs) else { a different set of chart button specs} but that doesn't seem to do the trick. Or should I be applying the conditions to ForceRefresh() only? Just asking before I potentially waste
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thank you for your guidance!
      That worked! I changed the dispatcher to handle only the chart events, leaving all logical tests out of it.

      Appreciate your help!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      43 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      20 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      30 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      47 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      38 views
      0 likes
      Last Post CarlTrading  
      Working...
      X