Warning about setting properties of Plots outside of Initialize() - NinjaTrader Programming | futures io social day trading
futures io futures trading


Warning about setting properties of Plots outside of Initialize()
Updated: Views / Replies:1,071 / 4
Created: by Zondor Attachments:0

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors – all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you don’t need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

Warning about setting properties of Plots outside of Initialize()

  #1 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

Warning about setting properties of Plots outside of Initialize()

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.
  }
.

Reply With Quote
The following 6 users say Thank You to Zondor for this post:
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary



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.


Last edited by Fat Tails; March 24th, 2012 at 11:55 AM.
Reply With Quote
 
  #4 (permalink)
Elite Member
Portland Oregon, United States
 
Futures Experience: Beginner
Platform: Ninjatrader®
Broker/Data: CQG, Kinetick
Favorite Futures: Gameplay Klownbine® Trading of Globex
 
Zondor's Avatar
 
Posts: 1,327 since Jul 2009
Thanks: 1,246 given, 2,635 received

It's a new one on me, too

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.

Reply With Quote
 
  #5 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,651 since Mar 2010
Thanks: 4,226 given, 25,600 received
Forum Reputation: Legendary


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

Reply



futures io > > > > > Warning about setting properties of Plots outside of Initialize()

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Properties baruchs NinjaTrader Programming 23 March 18th, 2017 11:02 AM
want to get chart info in initialize() tulanch NinjaTrader Programming 8 December 24th, 2011 05:36 PM
Draw object Properties kenz987 NinjaTrader Programming 1 November 10th, 2010 10:48 AM
Trading Hours in NT Properties togier Traders Hideout 4 August 18th, 2010 08:44 AM
Null Plots kenz987 NinjaTrader Programming 7 May 10th, 2010 11:29 PM


All times are GMT -4. The time now is 11:03 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-11 in 0.10 seconds with 19 queries on phoenix via your IP 54.145.16.43