NexusFi: Find Your Edge


Home Menu

 





Warning about setting properties of Plots outside of Initialize()


Discussion in NinjaTrader

Updated
    1. trending_up 2,545 views
    2. thumb_up 6 thanks given
    3. group 3 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,734

Plots are defined within the Initialize() method. However, some of us have been setting the properties of Plot objects in OnStartUp() and in OnBarUpdate().

There's nothing wrong with doing that, but the code must prevent a possible problem. If the indicator is being used by another indicator instead of being invoked directly, the indicator will not be plotted to a chart, there will be no ChartControl object for that indicator, and those Plot objects will not exist.

Trying to set the propery of an object that does not exist will cause a Null Reference Exception that will probably KO the program, even if there is an error handler to trap the exception.

There is an easy way to avoid this problem. Whenever doing anything outside of the Initialize() block with a Plot related property such as Pen.Width, Pen. DashStyle, Width, etc do all of those operations within a code block that only executes IF THE CHARTCONTROL OBJECT IS NOT NULL.

 
Code
if(ChartControl!=null

  {   Plot0.Pen.Width= 2;
       Plot0.Pen.DashStyle= Plot0Style;
       etc.
  }
.


Follow me on X Visit my NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Hungary Called for Magyar at 97pct, Ending 16-Year Orban …
Prediction Markets & Event Contracts
Powell in 48 Hours: Word Markets Give 78% on Inflation, …
Prediction Markets & Event Contracts
Prediction Markets Expiry Day: Trump Eyes War Exit, $230 …
Prediction Markets & Event Contracts
Weekend Update: First Qatari LNG Transit Attempted -- IR …
Traders Hideout
Bookmap Global Plus Lifetime + Lifetime Addons For Sale
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
7 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks
  #3 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Futures & Stocks
Posts: 9,887 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,114



Zondor View Post
Plots are defined within the Initialize() method. However, some of us have been setting the properties of Plot objects in OnStartUp() and in OnBarUpdate().

There's nothing wrong with doing that, but the code must prevent a possible problem. If the indicator is being used by another indicator instead of being invoked directly, the indicator will not be plotted to a chart, there will be no ChartControl object for that indicator, and those Plot objects will not exist.

Trying to set the propery of an object that does not exist will cause a Null Reference Exception that will probably KO the program, even if there is an error handler to trap the exception.

There is an easy way to avoid this problem. Whenever doing anything outside of the Initialize() block with a Plot related property such as Pen.Width, Pen. DashStyle, Width, etc do all of those operations within a code block that only executes IF THE CHARTCONTROL OBJECT IS NOT NULL.

 
Code
if(ChartControl!=null

  {   Plot0.Pen.Width= 2;
       Plot0.Pen.DashStyle= Plot0Style;
       etc.
  }


@ Zondor: Thank you for bringing up this subject. However, I have not seen this as a problem so far. To make my position clear, I would like to make a distinction between two different cases.

(1) Use of ChartControl

There is a number of indicators that make use of ChartControl either in OnStartUp() or in OnBarUpdate(), and it is clear that this will causes a problem if

-> there has been no null reference check for ChartControl
-> when that indicator is called by another indicator or strategy

For example, all my recent paint bar indicator, use a different way of coloring bars for candle sticks as opposed to OHLC bars, and I have therefore introduced a null reference check before I use ChartControl to identify the chart style, see code below

 
Code
if (ChartControl != null && ChartControl.ChartStyleType == ChartStyleType.CandleStick)
          candles = true;
  else
          candles = false;


(2) Accessing Plot Properties

However, I have never had any problem by accessing properties of the plots in OnStartUp() or OnBarUpdate(), even if there was no chart. For me a Plot is a Dataseries object to which a few numeric properties have been added and which even exists, if there is no chart and ChartControl takes the value null.

I access Plot in nearly all my indicators, and there has never been any problem in accessing the properties of a plot. This is quite different from ChartControl, where had to change the code of some indicators, which I had coded a few years ago.

Please correct me, if I am wrong.


Reply With Quote
  #4 (permalink)
 
Zondor's Avatar
 Zondor 
Portland Oregon, United States
 
Experience: Beginner
Platform: NinjatraderŽ
Broker: CQG, Kinetick
Trading: Gameplay KlownbineŽ Trading of Globex
Posts: 1,333 since Jul 2009
Thanks Given: 1,246
Thanks Received: 2,734

I never thought case 2 was a problem either, but I was getting NullReferenceException errors with one particular indicator that was being called by other indicators, where inserting the ChartControl condition solved the problem.

It might have had something to do with that particular indicator. However, in general, inserting that condition is, at worst, harmless.


Follow me on X Visit my NexusFi Trade Journal Started this thread Reply With Quote
  #5 (permalink)
 
Fat Tails's Avatar
 Fat Tails 
Berlin, Europe
Market Wizard
 
Experience: Advanced
Platform: NinjaTrader
Broker: Interactive Brokers
Trading: Futures & Stocks
Posts: 9,887 since Mar 2010
Thanks Given: 4,242
Thanks Received: 27,114


Zondor View Post
I never thought case 2 was a problem either, but I was getting NullReferenceException errors with one particular indicator that was being called by other indicators, where inserting the ChartControl condition solved the problem.

It might have had something to do with that particular indicator. However, in general, inserting that condition is, at worst, harmless.

Maybe I was not clear, I think that your example, setting plot properties, does not require a prior checking of ChartControl for null.

I only had to do that checking for a null reference, when I actually called ChartControl itself in the OnStartUp() or OnBarUpdate() sections. Some of my early indicators, including one of the first versions of the SuperTrend was not well-behaved for that reason....


Reply With Quote




Last Updated on March 24, 2012


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts