Runtime error: SetBackgroundColor exception when minimized and streaming
Author: vvardog
Creation Date: 7/6/2020 4:24 PM
profile picture

vvardog

#1
I am using c# to do custom charting, and am calling SetBackgroundColor to highlight bars that meet my criteria in my code.
It works as expected except when data streaming is enabled and the chart window is minimized. When a data update comes in while minimized, I get an "Object reference not set to an instance of an object" exception and my code halts. If I use a try/catch construct around the call to SetBackgroundColor, the exception is avoided but my custom chart painting does not occur until the next data stream update (or I restart).
The call to SetBackgroundColor only takes the bar number and color, so I'm assuming the actual bar is not yet instantiated (due to being minimized) and causes the exception. My thinking here is that if I can detect that the window is minimized, I simply defer calling SetBackgroundColor.
I guess it would be helpful to know when the window becomes "unminimized", so that I can force a repaint at that time.
Any pointers or helpful info is appreciated, thanks in advance.
profile picture

Eugene

#2
Could you show sample code sufficient to demonstrate the exception - as well as indicate affected time frame and symbols?
profile picture

vvardog

#3
Code sample is a great idea.

This exception occurs reliably using any stock symbol, and using 5-minute or 1-minute timeframes (I have not tested other time frames).

I first noticed this when I was opening up several windows, and minimized windows I wasn't currently interested in. When switching back to the minimized windows, I noticed the exception message and starting troubleshooting. I determined that the exception would only occur during streaming while minimized.

As a simple workaround I used a try/catch around the call to SetBackgroundColor, and this did prevent the exception. However when the window was restored, none of my custom chart painting and indicators were shown (until the next streaming update or I pressed 'Run').

This test code reproduced the exception on my first try. Here are my steps:
I compiled the code, set the date range to include today, selected a symbol from my list of 5-minute data sets, minimized the window, and then waited more than 5 minutes, so that the next update would occur while the window was minimized.

CODE:
Please log in to see this code.


I've assumed that if the code can detect (windowstate == minimized) then do not call SetBackgroundColor, and then hopefully I can get a notification that (windowstate changed and != minimized) so that I can force a repaint in my code.
But perhaps this is not the best way to avoid the exception, I'm open to suggestions since I'm fairly new to this platform.

Thanks for the rapid response and help
profile picture

Eugene

#4
Thank you for the detailed test case. Indeed it's easy to reproduce this cosmetic issue using a minimized 1-minute chart of e.g. Random data provider.

I think the workaround you've already found (try/catch block) is completely sufficient. You don't need the bar to be painted while the chart is minimized (so having a handled exception is just fine) because you're not looking at it. And after it's restored and a new bar added to the chart, it's being repainted correctly with SetBackgroundColor executed.

Detecting FormWindowState.Minimized may be possible (I haven't tried it) but isn't straightforward in WealthScript. Frankly, I don't think it's worth the effort to dedicate any more time to it given the cosmetic nature of the issue which is alleviated by a try/catch block.
profile picture

vvardog

#5
Would it be possible to detect coming out of the minimized state, so that I can force a repaint?

The reason is, I would prefer to not have to wait (up to) another 5 minutes to see my custom chart painting, or to press 'Run' to force a repaint. I'm attempting to automate some of my workflow so this would have true value for me.

Also, when using try/catch, the exception is avoided but for some unknown reason my other custom chart painting is not displayed. Even if my other painting would be displayed, I wouldn't have confidence in the background color and would be forced to always manually re-run after restoring.

( Side note - Is there a reason why SetBackgroundColor only fails when the window is minimized in streaming mode? This appears to be a bug from my perspective, if my code would become overly complicated when attempting to work around this exception. I understand its likely too late to attempt to address this. )

Thanks again for the help
profile picture

Eugene

#6
Here's how to catch the error in minimized window state without a try/catch block using an undocumented method:

CODE:
Please log in to see this code.


Nonetheless you cannot force a repaint from Strategy code. In WealthScript a complete bar updates and then your strategy executes over all the bars in the chart. Either way you will have to wait up to 5 minutes or whatever bar scale is selected.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).