OnStartUp error - NinjaTrader Programming | futures io social day trading
futures io futures trading


OnStartUp error
Updated: Views / Replies:1,993 / 15
Created: by Ragdoll 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
 

OnStartUp error

  #1 (permalink)
Elite Member
Walterboro, South Carolina
 
Futures Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Favorite Futures: futures
 
Posts: 13 since Aug 2013
Thanks: 1 given, 5 received

OnStartUp error

Hi all,

I need to buy a vowel. I have been working on a strategy that bases exits on the Pivots indicator. I was having some problems getting the indicator to properly report the pivots and s/r levels so I decided to write my own pivot and s/r level calculations. The problem is I'm getting a "**NT** Error on calling 'OnStartUp' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object."

I have troubleshot it down to the statements in bold in the code that follows. Warning, this is a work in progress. it compiles fine but pitches the error on enabling.

Any help you can provide on this would be greatly appreciated.
Thanks

Ragdoll

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// this is a strategythat uses pivot points and support / resistance levels to set exit and entry points
    /// </summary>
    [Description("this is a strategythat uses pivot points and support / resistance levels to set exit and entry points")]
    public class PivotPlay : Strategy
    {
        #region Variables
        // Wizard generated variables
        private double pct = 0.005; // Default setting for Pct
        // User defined variables (add any user defined variables below
		private bool firstTime;
		private bool islong;
		
		private double pvtu;
		private double pvtl;
		private double rl1u;
		private double rl1l;
		private double rl2u;
		private double rl2l;
		private double rl3u;
		private double rl3l;
		private double sl1u;
		private double sl1l;
		private double sl2u;
		private double sl2l;
		private double sl3u;
		private double sl3l;
		
		private double pvt;
		private double rl1;
        private double rl2;
		private double rl3;
		private double sl1;
		private double sl2;
		private double sl3;
		private double prevDayClose;
		private double prevDayHigh;
		private double prevDayLow;
		
		
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			firstTime = false;
			islong = false;
			CalculateOnBarClose = true;
			
					
		
        }
		protected override void OnStartUp()
		{
			
			prevDayClose = Bars.GetDayBar(1).Close; 
			prevDayHigh = Bars.GetDayBar(1).High; 
			prevDayLow = Bars.GetDayBar(1).Low;
			
			pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
			rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
			rl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R2[0];
			 rl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R3[0];
			 sl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S1[0];
			 sl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S2[0];
		     sl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S3[0];
			
			 pvtu = pvt + (pvt * pct);
			 pvtl = pvt - (pvt * pct);
			 rl1u = rl1 + (rl1 * pct);
			 rl1l = rl1 - (rl1 * pct);
			 rl2u = rl2 + (rl2 * pct);
			 rl2l = rl2 - (rl2 * pct);
			 rl3u = rl3 + (rl3 * pct);
			 rl3l = rl3 - (rl3 * pct);
			 sl1u = sl1 + (sl1 * pct);
			 sl1l = sl1 - (sl1 * pct);
			 sl2u = sl2 + (sl2 * pct);
			 sl2l = sl2 - (sl2 * pct);
			 sl3u = sl3 + (sl3 * pct);
			 sl3l = sl3 - (sl3 * pct);	
	
			Print("pvt is "+pvt);
			Print("pvtu is "+pvtu);
			Print("rl1l is "+rl1l);
	
		}
	    /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
		
				
			
			/// Rule 1;  enter long if close above first bar high...;
			if (Bars.FirstBarOfSession)
			{ return;
			}
			else
			{ int barOne = Bars.BarsSinceSession;
				if (Close[0] > High[barOne])
				{
				
				  islong = true;
				  firstTime = true;
				EnterLong("Entry");
				}
			}
		/// rule 2; 
			
			
			
			
			
			
			
		   if (islong)
		    { if (Close[0] > pvtu)
				{ SetStopLoss("Entry",CalculationMode.Price,pvtu,true);
					
					
				}else if ( Close[0] > rl1l)
				{ SetStopLoss("Entry",CalculationMode.Price,rl1l,true);
		 
			}else if ( Close[0] > rl1u)
				{ SetStopLoss("Entry",CalculationMode.Price,rl1,true);
				} 
			else
				{ 
					return;
				}
				
			}
			
		
		
			
        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public double Pct
        {
            get { return pct; }
            set { pct = Math.Max(0.001, value); }
        }
        #endregion
    }
}


Last edited by Ragdoll; September 16th, 2013 at 10:02 PM.
Reply With Quote
 
  #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,597 received
Forum Reputation: Legendary


You cannot access bar series in OnStartUp().

The following code section needs to be transferred to OnBarUpdate():

 
Code
prevDayClose = Bars.GetDayBar(1).Close;
prevDayHigh = Bars.GetDayBar(1).High;
prevDayLow = Bars.GetDayBar(1).Low;

pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
rl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R2[0];
rl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R3[0];
sl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S1[0];
sl2 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S2[0];
sl3 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).S3[0];

pvtu = pvt + (pvt * pct);
pvtl = pvt - (pvt * pct);
rl1u = rl1 + (rl1 * pct);
rl1l = rl1 - (rl1 * pct);
rl2u = rl2 + (rl2 * pct);
rl2l = rl2 - (rl2 * pct);
rl3u = rl3 + (rl3 * pct);
rl3l = rl3 - (rl3 * pct);
sl1u = sl1 + (sl1 * pct);
sl1l = sl1 - (sl1 * pct);
sl2u = sl2 + (sl2 * pct);
sl2l = sl2 - (sl2 * pct);
sl3u = sl3 + (sl3 * pct);
sl3l = sl3 - (sl3 * pct);


Also make sure that your only try to access Bars.GetDayBar(1), if a prior daily bar exists.

Reply With Quote
 
  #4 (permalink)
Elite Member
Walterboro, South Carolina
 
Futures Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Favorite Futures: futures
 
Posts: 13 since Aug 2013
Thanks: 1 given, 5 received

I tried that and got the following error:

**NT** Error on calling 'OnBarUpdate' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object.

thanks for the reply.


Reply With Quote
 
  #5 (permalink)
Elite Member
Walterboro, South Carolina
 
Futures Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Favorite Futures: futures
 
Posts: 13 since Aug 2013
Thanks: 1 given, 5 received

just tried again. same error message

Reply With Quote
 
  #6 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

First of all it's cleaner to just reuse some classes like

#variables

private Pivots myPivots;

#onbarupdate

myPivots = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20);

double r1 = myPivots.R1[0];
double s1 = myPivots.S1[0];

Your error message is most likely because you aren't listening to @Fat Tails and didn't check for CurrentBar or a session count before trying to call Bars.GetDayBar() 1 bar back. I haven't tested your code to know for sure.

Wrap that stuff around a (Bars.FirstBarOfSession && FirstTickOfBar) in #onbarupdate so it's only called at the beginning of a new session, and set a counter so you don't look to call it on the first session in the chart, wait until the second day to call it.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #7 (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,597 received
Forum Reputation: Legendary


Ragdoll View Post
I tried that and got the following error:

**NT** Error on calling 'OnBarUpdate' method for strategy 'PivotPlay/d93c1269d8904ff99817bfaa496ee2a4': Object reference not set to an instance of an object.

thanks for the reply.


Did you take off the Print instructions?

The NinjaTrader pivots indicator only returns values, once it has detected a day break. Therefore it does not return any values for the first day. Before you access the pivots you actually need to check, whether a value is returned. This can be done with

 
Code
if(Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1.ContainsValue(0))
  rl1 = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1[0];
else
  // do something here


Also follow the suggestion by @Big Mike to use an instance of mypivots, but do that in OnStartUp()!

 
Code
# variables
    private Pivots myPivots;

private override void OnStartUp()
{
    myPivots = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20);
........
}

// now it is easier to access the pivots in OnBarUpdate()

if(myPivots.R1.ContainsValue(0))
   rl1 = myPivots.R1[0];
else
   ........

Reply With Quote
 
  #8 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,238 since Jun 2009
Thanks: 29,350 given, 83,214 received

I reread your post and saw where you said the Print statement was giving you the errors.

I use a snippet like this when I am using indicators that take a few sessions to populate values, just to prevent me from taking signals due to missing or invalid data.

#variables
private int _totalsessions = 0;

#onbarupdate
if (Bars.FirstBarOfSession && FirstTickOfBar)
{
_totalsessions++;
}

if (_totalsessions < 5) return;

So I don't process any signal code unless we are in session 5 (day 5) or higher, when using weekly indicators like for example a weekly VWAP.

Mike

Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.

Need help?
1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first.
2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses.
3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make.
4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance.
5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers.
6)
Help using the forum? Watch this video to learn general tips on using the site.

If you want
to support our community, become an Elite Member.

Reply With Quote
 
  #9 (permalink)
Elite Member
Walterboro, South Carolina
 
Futures Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Favorite Futures: futures
 
Posts: 13 since Aug 2013
Thanks: 1 given, 5 received

My apologies for not being clear in my earlier post. I don't want to use the Pivots indicator. I want to write my own pivots based on the previous trading days high, low, and close. I know the formula and I have coded it in Thinkscript before. It should be fairly straight forward. I have cleaned up the code a bit to remove extraneous stuff and enclosed the code in the if section as @Big Mike suggested. I have two basic questions. How do I ensure the GetDayBar call returns data? And how do I put my code in a code box like you did in the previous post?

Here is the latest code:
I got the second question figured out

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// this is a strategythat uses pivot points and support / resistance levels to set exit and entry points
    /// </summary>
    [Description("this is a strategythat uses pivot points and support / resistance levels to set exit and entry points")]
    public class PivotPlay : Strategy
    {
        #region Variables
        // Wizard generated variables
        private double pct = 0.005; // Default setting for Pct
        // User defined variables (add any user defined variables below
		private bool firstTime;
		private bool islong;
		
		private double pvtu;
		private double pvtl;
		private double rl1u;
		private double rl1l;
		private double rl2u;
		private double rl2l;
		private double rl3u;
		private double rl3l;
		private double sl1u;
		private double sl1l;
		private double sl2u;
		private double sl2l;
		private double sl3u;
		private double sl3l;
		
		private double pvt;
		private double rl1;
        private double rl2;
		private double rl3;
		private double sl1;
		private double sl2;
		private double sl3;
		private double prevDayClose;
		private double prevDayHigh;
		private double prevDayLow;
		
		
        #endregion

        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
			firstTime = false;
			islong = false;
			CalculateOnBarClose = true;
			
					
		
        }
		protected override void OnStartUp()
		{
	
			
		
		}
	    /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
		if(Bars.FirstBarOfSession && FirstTickOfBar)
		{
		
			prevDayClose = Bars.GetDayBar(1).Close; 
			prevDayHigh = Bars.GetDayBar(1).High; 
			prevDayLow = Bars.GetDayBar(1).Low;
			
			pvt = (prevDayClose + prevDayHigh + prevDayLow) / 3;
			
			 pvtu = pvt + (pvt * pct);
			 pvtl = pvt - (pvt * pct);
			 rl1u = rl1 + (rl1 * pct);
			 rl1l = rl1 - (rl1 * pct);
			 rl2u = rl2 + (rl2 * pct);
			 rl2l = rl2 - (rl2 * pct);
			 rl3u = rl3 + (rl3 * pct);
			 rl3l = rl3 - (rl3 * pct);
			 sl1u = sl1 + (sl1 * pct);
			 sl1l = sl1 - (sl1 * pct);
			 sl2u = sl2 + (sl2 * pct);
			 sl2l = sl2 - (sl2 * pct);
			 sl3u = sl3 + (sl3 * pct);
			 sl3l = sl3 - (sl3 * pct);	
	
		}
		
			
			/// Rule 1;  enter long if close above first bar high...;
			if (Bars.FirstBarOfSession)
			{ return;
			}
			else
			{ int barOne = Bars.BarsSinceSession;
				if (Close[0] > High[barOne])
				{
				
				  islong = true;
				  firstTime = true;
				EnterLong("Entry");
				}
			}
		/// rule 2; 
			
			
			
		
			
			
			
		
		
			
        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public double Pct
        {
            get { return pct; }
            set { pct = Math.Max(0.001, value); }
        }
        #endregion
    }
}


Last edited by Ragdoll; September 16th, 2013 at 10:14 PM.
Reply With Quote
 
  #10 (permalink)
Elite Member
Walterboro, South Carolina
 
Futures Experience: Intermediate
Platform: ninjatrader, amibroker, thinkorswim
Favorite Futures: futures
 
Posts: 13 since Aug 2013
Thanks: 1 given, 5 received


OK,

I've had my RTFM moment and have solved the object problem with a little help from Bertrand. I added an if statement to check for a null on GetDayBar(1). The problem now is it is always null.

Reply With Quote

Reply



futures io > > > > > OnStartUp error

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
help NinjaTrader error eryo NinjaTrader Programming 20 December 22nd, 2016 07:56 PM
NT error jsengxx2 NinjaTrader 10 May 6th, 2013 07:27 AM
Error traderdp NinjaTrader 2 January 30th, 2013 03:59 PM
DashStyle error Big Mike NinjaTrader Programming 5 August 4th, 2012 06:53 AM
New OnStartUp() method in NT7 B8 aslan NinjaTrader Programming 7 February 5th, 2010 10:47 AM


All times are GMT -4. The time now is 10:39 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-10 in 0.13 seconds with 19 queries on phoenix via your IP 54.221.73.186