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

Draw.Text Object reference not set to an instance of an object.

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

    Draw.Text Object reference not set to an instance of an object.

    Hi,

    I used NT7 for develop many indicator and/or strategies and i don't remember this type problem so far. So i'm thinking problem related NT8.

    First step, I develop simple indicator which is draw up/down arrows for show instrument trend. And then develop a strategy which is using this indicator for buy/sell decision. Both indicator and strayegy are working correctly.

    Second step, On indicator, i add a counter for count bars after trend reverse and draw text on a chart for each bar to trend counter value. I put on a chart this indicator and check indicator working correctly and also check output window too for is there any error/warning message for it. Again every thing working correctly, output window is clear. After that again i restart back-test the strategy and receive an error message for my indicator that "Object reference not set to an instance of an object.".

    For test purpose, i uncomment only Draw.Text(...) lines from my indicator and strategy works again. Is this is a bug? Because it's not make sense for me, "can not use draw.text in an indicator if you'll use it in a strategy."

    Best Regards,
    Aytaç Aşan
    Last edited by aytacasan; 02-15-2017, 11:52 AM.

    #2
    Hello aytacasan,

    Thank you for writing in.

    Could you please send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader8>Log and My Documents>NinjaTrader8/Trace folders.

    I look forward to your email.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan,

      Thanks for your quick response. When i try to prepare your requested email, suddenly i found a problem and test it. In your NT8 help guide saying that when draw text on your chart it'll be useful to use ChartControl.Properties, so if NT theme change your drawings color will be adjust vs selected theme and can not be disappear on background which is maybe same color your text's brush color. Because of that i wrote below code for draw my text on a chart in my indicator;

      Draw.Text(this, string.Format("BullishCounter{0}", CurrentBar), BullishCounter[0].ToString(), 0, High[0] + TickSize, ChartControl.Properties.ChartText);

      It's working correctly if i add indicator to a chart but in my strategy i'm adding indicator via AddChartIndicator method and i think ChartControl come with null value and i'm receiving object reference not set... error. I don't know NT's internal infrastructure but from strategy angel, this situation (null ChartControl) seems normal but from an indicator angel it's not so good.

      If i want to write theme independent indicator and also want to use this indicator in my strategies how can i solve the problem?

      PS: Here is my solutions, i used #2 for solve my problem temporary.

      1. Define TextColor input parameter for user selection and use it.
      2. Change indicator code with this;
      var textBrush = ChartControl != null ? ChartControl.Properties.ChartText : Brushes.Black;
      Draw.Text(this, string.Format("BullishCounter{0}", CurrentBar), BullishCounter[0].ToString(), 0, High[0] + TickSize, textBrush);

      Best Regards,
      Aytac Asan
      Last edited by aytacasan; 02-15-2017, 06:17 PM.

      Comment


        #4
        Hello Aytac,

        In what State are you calling AddChartIndicator?

        I look forward to your reply.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Hello Alan,

          I'm calling the AddChartIndicator method when State == State.Configure. Because help guide samples doing like that.

          Shortly you can reproduce problem with below steps;

          1. Write an indicator with 2 plots(Up & Down), and in OnBarUpdate write if (Close[0] > Open[0] && Close [1] < Open [1]) then Up[0] = Low[0] else if (Close[0] < Open[0] && Close [1] > Open [1]) then Down[0] = High[0].

          2. Write a strategy and in OnBarUpdate write if indicator.Up[0] != 0 then EnterLong(...) else if Down[0] != 0 EnterShort(...). So far there is no problem with indicator on chart and strategy tested in the strategy analyzer.

          3. On indicator code after set Up plot value write that Draw.Text(..."Up"..., ChartControl.Properties.ChartText) and after set Down plot value write that Draw.Text(..."Down"..., ChartControl.Properties.ChartText). After this change, indicator will continue to work without any problem on chart.

          4. Retest your strategy without change anything you will see you are receiving an error message on output window "object reference not set".

          5. Return your indicator and delete "ChartControl.Properties.ChartText" or change it with Brushes.Black. And retest the strategy. It'll start to work without any problem. I hope this will help for clerify the problem.

          Best Regards,
          Aytac Asan
          Last edited by aytacasan; 02-16-2017, 10:49 AM.

          Comment


            #6
            Hello aytacasan,

            AddChartIndicator should be called in State.DataLoaded, after changing the state do you still experience the issue?

            Code:
             if (State == State.DataLoaded)
                {
                    // Charts a 20 period simple moving average to the chart
                    AddChartIndicator(SMA(20));
                }
            See AddChartIndicator section of our helpguide,


            I look forward to your reply.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Hi Alan,

              I look at your link and understand what you mean and changed the strategy code. Move the AddChartIndicator method call from Configure state to DataLoaded state but again receiving same error message. Please follow my steps for reproduce the error.

              PS : In both cases (Call method inside Configure vs DataLoaded) strategy test result exactly same. Of course, for escape from error, if i use predefined brush (ex. Brushes.Black) when i draw text to a chart.

              Regards,
              Aytac
              Last edited by aytacasan; 02-16-2017, 05:57 PM.

              Comment


                #8
                Hello aytacasan,

                Do you have a current bar check inside your strategy, same as your indicator? For example,
                Code:
                	if(CurrentBar<10) return;
                I have attached a strategy which references an indicator and plots text on the chart using,
                Code:
                	Draw.Text(this, "tag1", "Text to draw", 10, High[0], ChartControl.Properties.ChartText);
                Please see attached and use as a reference. Please let us know if you still need assistance.
                Attached Files
                Alan P.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Alan,

                  In these days try to understand logic of NT8. Yes many concepts same as NT7 but also there is many difference. Because of that still i'm encounter some difficulties. Because of that i'm a little bit sad and also a little bit angry. I wrote to you some steps and said please do same things. But you are so persist.

                  First when i try to import your sample codes. I'm receiving error. Don't understand why and don't want to spend time about it. Please look at attachment.

                  Second i look at your sample source code from inside zip file and i see you aren't using indicator in a strategy. Only showing indicator. This sample only similar not same thing in my opinion. But of course you know better than me that internal logic of NT...

                  So i decide to write sample for you that produce an error, as soon as possible. Maybe i'm doing so basic/stupid fault.If so you can see easily in this way.

                  PS: I'm always check CurrentBar inside my indicator & strategies. In my indicator and strategies using like this;

                  Indicator:
                  if (CurrentBar < BarsRequiredToPlot)
                  return;

                  Strategy:
                  if (CurrentBar < BarsRequiredToTrade)
                  return;

                  Regards,
                  Aytac
                  Attached Files
                  Last edited by aytacasan; 02-17-2017, 02:42 PM.

                  Comment


                    #10
                    Hello aytacasan,

                    The sample I provided does indeed use an indicator inside of a strategy.

                    I would suggest deleting or renaming any indicator you have named, MyCustomIndicator18, and any strategy named, MyCustomStrategy9, at which point you should be able to import the strategy to use as a reference.

                    After using the strategy as a reference, if you are still unable to get your strategy to work please upload a copy here and I will see if anything pops up.

                    Please let us know if you need further assistance.
                    Alan P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Alan,

                      Of course i don't have any indicator that name is same as your indicator's name or i don't have any strategy which is same name with your strategy. I have 1 bar type, 1 indicator and 2-3 strategies and names so different from yours. I don't understand why receiving an error when i try to import it.

                      You said that "The sample I provided does indeed use an indicator inside of a strategy". There is no code in strategy's OnBarUpdate. You are only using indicator to show on strategy analyzer result chart. Below your sample strategy's OnBarUpdate method;

                      protected override void OnBarUpdate()
                      {
                      if(CurrentBar<10) return;
                      //Add your custom strategy logic here.
                      }

                      I think there is misunderstanding between us. I'm using private global variable for indicator reference on DataLoaded like that;

                      1. Creting private SampleIndi sample; // private global variable inside strategy class

                      2. Inside strategy's OnStateChage method when State == DataLoaded;

                      sample = SampleIndi();
                      AddChartIndicator(sample);

                      3. Inside strategy's OnBarUpdate;

                      if (CurrentBar < ....) return;

                      if (sample.PlotXYZ[0] == ...) EnterLong() etc.

                      This is using an indicator plots but your sample only showing... I hope this will be clarify an issue.

                      Anyway i'll send you sample codes that produce an error as soon as possible.

                      Regards,
                      Aytac
                      Last edited by aytacasan; 02-17-2017, 05:44 PM.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Mizzouman1, Today, 07:35 AM
                      4 responses
                      18 views
                      0 likes
                      Last Post Mizzouman1  
                      Started by philmg, Today, 01:17 PM
                      1 response
                      6 views
                      0 likes
                      Last Post NinjaTrader_ChristopherJ  
                      Started by cre8able, Today, 01:01 PM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by manitshah915, Today, 12:59 PM
                      1 response
                      5 views
                      0 likes
                      Last Post NinjaTrader_Erick  
                      Started by ursavent, Today, 12:54 PM
                      1 response
                      7 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X