NexusFi: Find Your Edge


Home Menu

 





Warning about setting properties of Plots outside of Initialize()


Discussion in NinjaTrader

Updated
    1. trending_up 2,571 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?
May 26 Deadline Dies at 7.5% as US Strikes Iran During C …
Prediction Markets & Event Contracts
$24.5 Billion Record Month: Prediction Markets Shatter A …
Prediction Markets & Event Contracts
Sundays Verdict: Lebanon Locked at 99.85% as Iran June 7 …
Prediction Markets & Event Contracts
$500M Riding on World Cup 2026: France/Spain Co-Favored …
Prediction Markets & Event Contracts
S&P 500 Holds the Line: No Fast Track for SpaceX, Op …
Stocks and ETFs
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Big Mike in Ecuador
196 thanks
Sober Journey With S&P
27 thanks
30 Sessions
20 thanks
BERN ALGOS algo trading journal
8 thanks
Volume Indicators
8 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,117



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,117


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