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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      131 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      73 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X