Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Using ChartControl.Properties.ChartText error: object reference not set...

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

    Using ChartControl.Properties.ChartText error: object reference not set...

    Hi I am trying to set the Brush of some labels to be obtained/controlled by the ChartControl properties.

    Two lines of code - first is working, the second not?! The only difference is the Brush from ChartControl properties:
    Code:
    Draw.Text(this, "ssLabel" + CurrentBar.ToString(), TrendValue().ToString(), 0, Highs[0][0] + (5 * TickSize), Brushes.Black);
    //Draw.Text(this, "ssLabel" + CurrentBar.ToString(), TrendValue().ToString(), 0, Highs[0][0] + (5 * TickSize), ChartControl.Properties.ChartText);
    Running with the second one gives me:
    Error on calling 'OnBarUpdate' method: on bar 147: object reference not set...

    Why at bar 147, all my object references are fine except the Brush? Any hints or help would be appreciated!

    Thank you,
    Momchil

    #2
    Hello momchi,

    Thanks for your post.

    I don't see anything wrong with your Draw.Text statement.

    What else is going on with the script?

    Is this a multi series script? If so are you drawing the text when BarsInProgress == 0?


    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul,
      thanks for your reply, yes it is a muti-time frame script so I'll condition the drawing only on primary (chart) series and update you.

      Comment


        #4
        Hi Paul,
        here is a bit more data to help you help me ... Conditioning the text drawing to the main series of bars did not help. The missing reference error is the first bar where the code calls the procedure to place a label. If I use a const brush like Brushes.Black - it always works, but my goal was to use the chart properties for the size of the text/labels. Is this the only way to get it?

        I am getting the error in the Analyzer (when running Backtest), during optimization I disable all drawing for performance/memory reasons.

        I tried "saving" the ChartText brush in a local method data (variable), initializing it during StateConfigure, but that failed right there - so the broader question is when/where it is safe to try to access ChartControl properties ChartText?

        best regards,
        M.

        Comment


          #5
          Hi Paul,
          we have made some progress on this - I stored my own local (method) object for the ChartControl.Properties in an overridden onRender() method like this:

          Code:
           protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
          {
          // Instantiate a ChartControlProperties object to hold a reference to chartControl.Properties
          ChartControlProperties ccProps = chartControl.Properties;
          
          // keep chart's text brush to use in Draw.Text
          [B][I]ChartBrush [/I][/B]= ccProps.ChartText;
          [B][I]LabelFont [/I][/B]= ccProps.LabelFont;
          [B]LabelFont.Size = 8;[/B]
          }
          see attached image please, and note the "-100" in Pink - label matches the Properties of the ChartText in Color, BUT, not in Size of the font...?
          And, if I even try to set it to 8 fixed - it still comes out with some other size??!
          Can you help me with that please?

          My label call line is:
          Code:
           Draw.Text(this, "ssLabel" + CurrentBar.ToString(), false, ConditionValue().ToString(), 0, Highs[0][0] + (5 * TickSize), 0, [I][B]ChartBrush[/B][/I], [I][B]LabelFont[/B][/I], TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
          Attached Files

          Comment


            #6
            Hello momchi,

            Thanks for your replies.

            You can access ChartControl in State.DataLoaded as well OnBarUpdate().

            Please note that if you use the Draw.Text overload of Draw.Text(NinjaScriptBase owner, string tag, string text, int barsAgo, double y) that it will automaticxally use the "default" template to pull the color, font and size. If you click on the text itself (when drawn on the chart using that overload) you can see what the draw settings are, in my case it is Arial 14 with slate gray as the color (based on the skin chosen). So when you don't specify the font or the color, that is what you will get which likely would not be the smaller chart properties parameter values.

            Note: You can change default settings for Draw.Text if you wish.

            If I can suggest a simpler way, here is an example you can test with:

            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0) return;

            if (ChartControl != null && CurrentBar %100 == 0)
            {
            Draw.Text (this, "test"+CurrentBar, "Default",0, High[0]+5 * TickSize);
            Draw.Text(this, "testa"+CurrentBar, "ChartCtrl", 0, High[0]+ 10 * TickSize, ChartControl.Properties.AxisPen.Brush);
            Draw.Text(this, "testb"+CurrentBar, true, "ChartCtrl2", 0, High[0]+ 15 * TickSize, 0, ChartControl.Properties.AxisPen.Brush, ChartControl.Properties.LabelFont,
            TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0) ;

            }
            }


            It will draw the same text every 100 bars. If you change the chart properties font and then reload ninjascript, the top text will match.

            Here is a screenshot of what to expect. (I set the chart properties font size to 8)

            Click image for larger version

Name:	Momchi-1.PNG
Views:	372
Size:	23.6 KB
ID:	1139944

            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              1) thanks for pointing out I needed to check for validity of ChartControl object, I was not aware that I had to do this.
              2) Is ChartControl available in StrategyAnalyzer runs? (the Display set to: "Chart")?

              ... because, after adding the check - I don't see any labels added, as if the code never runs to add them?

              thanks for your help so far!

              Comment


                #8
                Hello momchi,

                Thanks for your reply.

                Correct, ChartControl is not available in the Strategy analyzer.

                You could check the system bool IsInStrategyAanalyzer and then use alternative draw statements that do not use chart control.

                Reference: https://ninjatrader.com/support/help...egyanalyer.htm

                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by DJ888, 04-16-2024, 06:09 PM
                4 responses
                12 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by terofs, Today, 04:18 PM
                0 responses
                11 views
                0 likes
                Last Post terofs
                by terofs
                 
                Started by nandhumca, Today, 03:41 PM
                0 responses
                7 views
                0 likes
                Last Post nandhumca  
                Started by The_Sec, Today, 03:37 PM
                0 responses
                3 views
                0 likes
                Last Post The_Sec
                by The_Sec
                 
                Started by GwFutures1988, Today, 02:48 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Working...
                X