Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

PlotBrushes get stuck if Calculate.OnEachTick

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

    PlotBrushes get stuck if Calculate.OnEachTick

    NT 8.0.0.11: data connection CQG
    I assign a plot value in OnBarUpdate, for example: Value[0] = EMA(EmaPeriod)[0] ;

    OnMarketData assigns values to a couple of class variables for R and G of the RGB set; e.g., according to a transaction taking place at either bid or ask.

    Back in OnBarUpdate attempting to adjust the plot color via PlotBrushes[0][0] from SolidColorBrush(Color.FromArgb(a,R,G,b)) only works if Calculate.OnBarClose. If Calculate.OnEachTick, the plot color never changes.

    See the attached screenshots.

    Code:
            private    byte by_red = 255 ;
            private    byte by_green = 255 ;
            private    byte by_blue = 0 ;
    
            protected override void OnMarketData(MarketDataEventArgs e)
            {
                if (e.MarketDataType == MarketDataType.Last && CurrentBar >= 0)
                {
                    if (e.Price >= e.Ask)
                    {
                        by_red = 0 ;
                        by_green = 255 ;
                    }
                    else if (e.Price <= e.Bid) 
                    {
                        by_red = 255 ;
                        by_green = 0 ;
                    }
                }
            }
            
            protected override void OnBarUpdate()
            {
                Value[0] = EMA(EmaPeriod)[0] ;
                
                Brush brush = new SolidColorBrush(Color.FromArgb(255, by_red, by_green, by_blue));
                brush.Freeze();
                PlotBrushes[0][0] = brush ;                
            }
    Attached Files

    #2
    Hi tradesmart,

    I've taken a few moments to create a test script to test this, however, I am not able to reproduce your results. I am showing that the plot is able to update before the bar closes.

    http://screencast.com/t/uGpnPZdXdA4

    Attached is an export of the script I have tested.


    As a tip, to export a NinjaTrader 8 NinjaScript do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message

    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


    Below is a link to the help guide on Exporting NinjaScripts.
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi tradesmart,
      I've taken a few moments to create a test script to test this, however, I am not able to reproduce your results. I am showing that the plot is able to update before the bar closes.
      Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.

      Attached is an export of the script I have tested.
      Thank you for investigating. Yes, it works as you demonstrate, but not if Tick Replay is activated. Could you please try your script with the data series Tick Replay turned on? You should be able to see the problem as shown in the attached image.
      Attached Files

      Comment


        #4
        Originally posted by tradesmart View Post
        Thank you for investigating. Yes, it works as you demonstrate, but not if Tick Replay is activated. Could you please try your script with the data series Tick Replay turned on? You should be able to see the problem as shown in the attached image.
        One more observation: with Tick Replay on, the leftmost bars of the chart seem to change color as expected. However, thereafter, the plot gets stuck on a single color and never changes again.
        Attached Files

        Comment


          #5
          Hello tradesmart,

          Once TickReplay is enabled I am able to reproduce the behavior and I have reported this to development.

          Once I have more information or a tracking ID for the report, I will post this in this thread.

          Please let me know if you find any other behavior you feel is incorrect.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ChelseaB View Post
            Hello tradesmart,

            Once TickReplay is enabled I am able to reproduce the behavior and I have reported this to development.

            Once I have more information or a tracking ID for the report, I will post this in this thread.

            Please let me know if you find any other behavior you feel is incorrect.
            This problem also manifests itself in the trace file as I describe in this thread:

            Comment


              #7
              Hello tradesmart,

              The issue here is that there are too many custom brushes being created in memory.

              By enabling the TickReplay and creating the objects in OnMarketData or in OnBarUpdate with Calcuate.OnEachTick this causes the number of brushes created to exceed the maximum number of unique brushes (65535).

              Please adjust your code to use fewer brushes.

              Instead of creating them new each time, use a variable within the scope of the class that holds a brush that can be used as needed.
              Attached is an example.

              OR don't use custom brushes but use the pre-existing System.Windows.Media.Brushes instead.
              For example:
              brush = Brushes.Green;
              Attached Files
              Last edited by NinjaTrader_ChelseaB; 07-25-2016, 07:55 AM.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hello tradesmart,

                The issue here is that there are too many custom brushes being created in memory.

                By enabling the TickReplay and creating the objects in OnMarketData or in OnBarUpdate with Calcuate.OnEachTick this causes the number of brushes created to exceed the maximum number of unique brushes (65535).

                Please adjust your code to use fewer brushes.

                Instead of creating them new each time, use a variable within the scope of the class that holds a brush that can be used as needed.
                Attached is an example.

                OR don't use custom brushes but use the pre-existing System.Windows.Media.Brushes instead.
                For example:
                brush = Brushes.Green;
                Thanks, but my example was probably too simplistic in that it didn't alter the red and green byte values. In actual use, those values adjust dynamically as the ratio of bid to ask volume changes. So, for example, consider this brush_ratio = new SolidColorBrush(Color.FromArgb(255, r, g, 0)); The values for r and g will be changing constantly as the ratio changes, so I can't simply use a couple of brushes declared at the class level; I would need 256^2 of them. Right?

                I'll be grateful for any suggestions as to how I can accomplish this.

                Comment


                  #9
                  Hi tradesmart,

                  I would suggest creating a list of brushes. Check to see if the color has already been added to the list and use that brush from the list. If the color hasn't been added then add the brush. Likely this would not hit the 65535 limit.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_ChelseaB View Post
                    I would suggest creating a list of brushes. Check to see if the color has already been added to the list and use that brush from the list. If the color hasn't been added then add the brush. Likely this would not hit the 65535 limit.
                    Thanks for the great idea. I ended up using a dictionary instead of a list. Its key is the ratio rounded to 2 decimal places, so at most there will be 100 brushes stored in the dictionary values. Before I made this change, I noticed that there were something like half a million brushes being created. I guess that was a bit of a problem, eh?

                    No such issue with NT7, though. Most the the difficulties I'm having in porting NT7 code over to NT8 seem to be related to brushes having replaced colors. It's a learning experience.

                    Comment


                      #11
                      Hi tradesmart,

                      Agreed, this also a great learning experience for our NinjaTrader staff as well.

                      One note I might add, is that if you were creating millions of custom color objects in NT7, I would imagine this would cause very heavy CPU and memory resources.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_ChelseaB View Post
                        Hi tradesmart,

                        Agreed, this also a great learning experience for our NinjaTrader staff as well.

                        One note I might add, is that if you were creating millions of custom color objects in NT7, I would imagine this would cause very heavy CPU and memory resources.
                        You're probably right, but since I didn't encounter such errors in NT7, I didn't investigate further.

                        One thing I've run into after modifying several indicators to use my new brush dictionary is that if I load a previous workspace that had a chart with those indicators on it, that chart would not display live data updates and NT locked up. I had to kill the NT8 process, restart, and create a new chart with those same indicators and save it as a template. Then I edited the workspace XML to remove the chart's indicators, reloaded the workspace and then loaded the template into the empty chart. Now it works correctly.

                        Comment


                          #13
                          Hello tradesmart,

                          If you make a template and then change the script so that the template doesn't match the inputs any more, this can cause a hang. You would want to delete and recreate the template so that this matches the inputs of the indicator.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ChelseaB View Post
                            Hello tradesmart,

                            If you make a template and then change the script so that the template doesn't match the inputs any more, this can cause a hang. You would want to delete and recreate the template so that this matches the inputs of the indicator.
                            Yes, I know. But in this case no inputs were changed.

                            Comment


                              #15
                              Hello tradesmart,

                              Do you still have the old template and the script before changes?
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,404 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              95 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              159 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X