Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ToDxBrush replacement

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

    ToDxBrush replacement

    at the end of this manual https://ninjatrader.com/support/help...arsperiods.htm under Best Practices

    it says not to use ToDxBrush too much

    but I have 5 plots on the chart, and I need that plot's color to be used in my OnRender drawings

    Currently I have

    Code:
                SharpDX.Direct2D1.Brush USDBrush = Plots[0].Pen.Brush.ToDxBrush(RenderTarget);
                SharpDX.Direct2D1.Brush EURBrush = Plots[1].Pen.Brush.ToDxBrush(RenderTarget);
                SharpDX.Direct2D1.Brush GBPBrush = Plots[2].Pen.Brush.ToDxBrush(RenderTarget);
                SharpDX.Direct2D1.Brush JPYBrush = Plots[3].Pen.Brush.ToDxBrush(RenderTarget);
                SharpDX.Direct2D1.Brush AUDBrush = Plots[4].Pen.Brush.ToDxBrush(RenderTarget);​
    
                // I draw multiple things using the same brush, but with different opacity so I need to modify this value during a for loop
                USDBrush.Opacity = i > scale ? 0 : (float)i/(float)scale;
                ...etc
    
                // at the end of OnRender
                USDBrush.Dispose();​
                ...etc
    this is called in the main body of OnRender

    is there a more efficient way to do this then?
    I don't want hard coded colors, if the user changes a color of a plot, I want the rendered drawings color to change as well

    #2
    Hello LuxSpuzy,

    If you need to create brushes on every render pass that would be fine for a few brushes. If you need to do something more complex that requires many brushes or frequently changing the brush you should create them using OnRenderTargetChanged. There is a sample of that concept here: https://ninjatrader.com/support/help...rTargetChanged

    Comment


      #3
      Thank you for your reply

      my Indicator keeps freezing and crashing all the time
      I've done everything I could think of and it still does it.
      for hours I thought it was because of OnRender as I didn't notice the indicator crashing before that, and even while using OnRender for a while it didn't crash (or I just wasn't aggressive on moving the chart around enough)

      but now it crashes even if showDash is disabled and OnRender and OnRenderTargetChanged aren't even doing anything

      I've spent at least 10 hours on this and I really need some help
      is it because I use 10 additional Data sources? is that overwhelming the platform? but why does it work if left alone, but crashes and freezes the whole platform if the chart is moved around.
      I am completely buffled
      I even tried to restart my PC and start fresh as I am just not sure what is causing this.



      EDIT: it crashes just because there are 11 AddDataSeries calls
      is there a way to fix this?

      note yesterday it took 40 seconds to load the indicator onto the chart and it didn't crash, today it always took around 2-5 seconds and it's crashing
      Last edited by LuxSpuzy; 03-07-2023, 11:27 AM.

      Comment


        #4
        Hello LuxSpuzy,

        I wouldn't be able to debug the script for you to find the problem. You would need to eliminate parts of the logic and keep testing the script to find which part controls the crash. Once that part of the logic is known we could go over that specific part.

        If you are seeing a crash or freeze you would need to keep the log window open to check if there is any messages being printed when that happens. If there are not then the only way to find the problem would be the process of elimination.

        I would suggest while testing to make sure to close all workspaces and don't use any imported items to confirm the problem relates specifically to your indicator.

        Comment


          #5
          Originally posted by LuxSpuzy View Post
          at the end of this manual https://ninjatrader.com/support/help...arsperiods.htm under Best Practices

          it says not to use ToDxBrush too much

          but I have 5 plots on the chart, and I need that plot's color to be used in my OnRender drawings
          Isn't all of this missing the point that there is a Plots[].BrushDX you can access directly?
          Bruce DeVault
          QuantKey Trading Vendor Services
          NinjaTrader Ecosystem Vendor - QuantKey

          Comment


            #6
            Bruce DeVault thank you didn't see that


            Jesse it seems to crash when the indicator is applied to a chart who's data it extracts through AddDataSeries

            Code:
            AddDataSeries("EURUSD", BarsPeriod);
            AddDataSeries("EURGBP", BarsPeriod);
            AddDataSeries("EURJPY", BarsPeriod);
            AddDataSeries("EURAUD", BarsPeriod);
            
            AddDataSeries("GBPUSD", BarsPeriod);
            AddDataSeries("GBPJPY", BarsPeriod);
            AddDataSeries("GBPAUD", BarsPeriod);
            
            AddDataSeries("USDJPY", BarsPeriod);
            
            AddDataSeries("AUDUSD", BarsPeriod);
            AddDataSeries("AUDJPY", BarsPeriod);​
            so if I use a chart with any of these symbols it will crash, on other symbols it doesn't
            if I remove "AddDataSeries("EURUSD", BarsPeriod);" and use EURUSD chart it doesn't crash.
            I am also using "AddDataSeries(freqType, freqValue);" where the 2 arguments are parameter inputs, defaulted to 1 Day, and that works just fine.
            but if I set it to 60 minutes, and my chart is 60 minutes, then it crashes again.

            so if the current charts instrument is called for with the same BarsPeriod, it crashes.

            I'm guessing this is a NinjaTrader bug and should be reported. or if this is known I'm interested in why it happens

            I made a workaround in my code
            Code:
            Ins = new int[11];
            symbCount = 0;
            
            string Namae = Instrument.MasterInstrument.Name;
            if(Namae != "EURUSD") { AddDataSeries("EURUSD", BarsPeriod); symbCount++; Ins[0] = symbCount; } else Ins[0] = 0;
            if(Namae != "EURGBP") { AddDataSeries("EURGBP", BarsPeriod); symbCount++; Ins[1] = symbCount; } else Ins[1] = 0;
            if(Namae != "EURJPY") { AddDataSeries("EURJPY", BarsPeriod); symbCount++; Ins[2] = symbCount; } else Ins[2] = 0;
            if(Namae != "EURAUD") { AddDataSeries("EURAUD", BarsPeriod); symbCount++; Ins[3] = symbCount; } else Ins[3] = 0;
            
            if(Namae != "GBPUSD") { AddDataSeries("GBPUSD", BarsPeriod); symbCount++; Ins[4] = symbCount; } else Ins[4] = 0;
            if(Namae != "GBPJPY") { AddDataSeries("GBPJPY", BarsPeriod); symbCount++; Ins[5] = symbCount; } else Ins[5] = 0;
            if(Namae != "GBPAUD") { AddDataSeries("GBPAUD", BarsPeriod); symbCount++; Ins[6] = symbCount; } else Ins[6] = 0;
            
            if(Namae != "USDJPY") { AddDataSeries("USDJPY", BarsPeriod); symbCount++; Ins[7] = symbCount; } else Ins[7] = 0;
            
            if(Namae != "AUDUSD") { AddDataSeries("AUDUSD", BarsPeriod); symbCount++; Ins[8] = symbCount; } else Ins[8] = 0;
            if(Namae != "AUDJPY") { AddDataSeries("AUDJPY", BarsPeriod); symbCount++; Ins[9] = symbCount; } else Ins[9] = 0;
            
            AddDataSeries(freqType, freqValue); symbCount++; Ins[10] = symbCount;​
            this way I can use the Ins array to call the Closes[][] and such correctly.
            Last edited by LuxSpuzy; 03-07-2023, 03:04 PM.

            Comment


              #7
              Hello LuxSpuzy,

              There are many scripts that use AddDataSeries that run normally so this likely has something to do with your script overall and the logic you used. One note is that AddDataSeries is not intended to be variable so before doing anything you would need to fix the AddDataSeries to not have the BarsPeriod being used, you would need to specify the series settings like shown in the help guide page.


              Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.
              Specifying the bars period directly looks like this:
              Code:
              AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)14, Value = 10, Value2 = 20 });
              If you ever run into a problem in NinjaScript and feel that it may be a bug you would need to extract the specific code causing a problem and then attach a sample that demonstrates problem. While doing that you can visit the relevant help guide sections to ensure the suggested syntax is being used. The indicator you provided is a full script so that wouldn't be something we could review or debug.

              Comment


                #8
                here you go, a script that freezes the chart and messes up the whole application.
                use it on a EURUSD 60 minute, 365 Days worth of data chart to get an instant crash. just move the chart left and right a few times and it crashes, at least on my machine
                I've also uploaded my chart template if that matters at all.

                the script has 5 plots that are useless and no math just cuz my old one had 5 plots so maybe there's a correlation, 10 AddDataSeries and no inputs

                I could reduce it a bit more to determine the actual cause but like I said, if I use a chart whose DataSeries I don't add, then it works, if I use any of the 10 charts I call data from, I crash, all was tested on 60 minutes, 365 days worth of data

                Good luck, let me know if I need to provide my hardware details or anything else that might help

                Click image for larger version  Name:	image.png Views:	0 Size:	34.8 KB ID:	1238822
                Last edited by LuxSpuzy; 03-08-2023, 09:46 AM.

                Comment


                  #9
                  Hello LuxSpuzy,

                  This sample is not valid for the reasons posted in post 7, you are still using a variable with AddDataSeries so that is likely to fail. The workaround you came up with also is prone to failure. We cannot test scripts that use variables with AddDataSeries because they are likely to fail in a lot of different use cases.

                  Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.
                  Code:
                  AddDataSeries("EURUSD", BarsPeriod); <-- not a valid use of BarsPeriod
                  If you need the BarsPeriod overload you need to manually type in the values like this:

                  Code:
                  AddDataSeries(new BarsPeriod() { BarsPeriodType = (BarsPeriodType)14, Value = 10, Value2 = 20 });

                  If you can use one of the other overloads to achieve your goal it would look similar to this:

                  Code:
                   AddDataSeries("ES 09-16", BarsPeriodType.Tick, 100);
                  Do you see the problem still if you fix the AddDataSeries statements?

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  633 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  364 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  105 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  567 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  568 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X