NexusFi: Find Your Edge


Home Menu

 





Converting Indicator to Strategy help!


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one MXASJ with 6 posts (2 thanks)
    2. looks_two sam028 with 3 posts (3 thanks)
    3. looks_3 bukkan with 1 posts (2 thanks)
    4. looks_4 kandlekid with 1 posts (0 thanks)
    1. trending_up 6,024 views
    2. thumb_up 7 thanks given
    3. group 4 followers
    1. forum 12 posts
    2. attach_file 2 attachments




 
Search this Thread

Converting Indicator to Strategy help!

  #1 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Hi All,

I've updated my ExportChartData indicator in the downloads/Ninja/Misc section to add some new functionality. Based on some code I found from Mike and some comments from Sam, I'm now trying to convert that Indicator to a Strategy.

I'm getting an exception in the OnBarUpdate method, "Object reference not set to an instance of an object."

Below is the OnBarUpdate area of the Strategy code:

 
Code
                            
protected override void Initialize()
{
CalculateOnBarClose true;
 
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
/* The try-catch block is used for error handling.
In this case it is used to ensure you only have one stream object operating on the same file at any given moment. */
string dest path+filename;
string defname path + @"\" +Instrument.FullName+BarsPeriod.Id+BarsPeriod.Value+".txt";
defname = defname.Replace(' ','_'); // Replaces non ISO file characters in Futures names
defname = defname.Replace('-','_'); // Replaces non ISO file characters in Futures names
 
 
 
// If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
 
if (CurrentBar == 0
&& useDefaultFilename == false)
{
sw = File.AppendText(dest);
Print(dest);

if (CurrentBar == 0
&& useDefaultFilename == true)
{
sw = File.AppendText(defname);
Print(defname);
}
 
 
string sep = DataSeparator.ToString();// Cast required so the Date/Time separator and normal data separator are the same
 
// This is the output of all lines. The output format is as follows: DateTime Open High Low Close Volume
string data = (Time[0] + sep + Open[0] + sep + High[0] + sep + Low[0] + sep + Close[0] + sep + Volume[0]);
 
if (splitDateTime == true)
{//Replace the space in DateTime with the DataSeparator
data = data.Replace(' ',DataSeparator);
}
 
sw.WriteLine(data);
sw.Close(); 
 
 

And I've attached the zip file for both the Indicator ExportChartData (works fine) and Strategy ExportData (exception thrown). Any help on this would be greatly appreciated.

FWIW my next steps in the evolution of the code is to try an incorporate Enums for the output file types by playing some more with manipulating DateTime objects and strings, and look at ways to optionally export indicator data as well.

But I need it to work with the current features first!

Thanks for your time and input.

Attached Files
Elite Membership required to download: ExportData.zip
Elite Membership required to download: ExportChartData.zip
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Increase in trading performance by 75%
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
REcommedations for programming help
Sierra Chart
Exit Strategy
NinjaTrader
How to apply profiles
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
11 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #3 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


Problem solved by removing condition CurrentBar == 0

We now return to our regularly scheduled programming...

Started this thread Reply With Quote
Thanked by:
  #4 (permalink)
kandlekid
College Point, NY (Queens)
 
Posts: 63 since Nov 2009
Thanks Given: 5
Thanks Received: 20

Interesting, looks like the compiler took 0 as null and assumed you were comparing CurrentBar to it. And CurrentBar being an int (I believe) and null being a reference ...

Reply With Quote
  #5 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629

MX, you should avoid putting your file stuff in the OnBarUpdate(), and put it in Initialize().
OnBarUpdate() is called every bar update (really ), so it have to be as light as possible, for performance aspects.
So, in your example, you'll just have to write in the file, not open/create it, and not closing it after each bar !!!

So:
- the creation/init of the file: in Initialize() (or OnStartUp() with NT7)
- the close of it in Dispose() (or OnTermination() with NT7).

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
Thanked by:
  #6 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Thanks Sam,

Problem is Instrument.FullName+BarsPeriod.Id+BarsPeriod.Value can only be called in the OnBarUpdate()... I think.

Started this thread Reply With Quote
  #7 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629


MXASJ View Post
Thanks Sam,

Problem is Instrument.FullName+BarsPeriod.Id+BarsPeriod.Value can only be called in the OnBarUpdate()... I think.

Hmm, I'm not sure, and can't test it now.
But if it's the problem, you can:
- use a random, or whatever file name
- store your Instrument.FullName+BarsPeriod.Id+BarsPeriod.Value" in a public variable, inside OnBarUpdate()
- rename your file at the end ( File.Move(...)), in Dispose().

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
  #8 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800

Thanks Sam,

I'm not sure if it is an issue, but I'm always open to ways to code things better. I'm at C# 101 right now . Feel free to make it better.

I just ran that 'strat against the S&P500 list that comes with Ninja and my own instrument list of all IShares ETFs (daily data, one year back), and all was fine (one bad ticker exception on the S&P list that I'll look at tomorrow). Personally I think that is pretty cool. I don't do equities but building an S&P500 correlation or covariance matrix using R and Ninja that updates daily would be a good demonstration tool of what can be done for free these days. A good reminder that "edge" is fleeting.

My next journey with this is the enum/DateTime one, neither of which I fully understand but using a non-production utility 'strat (like this) instead of a live trading 'strat takes some pressure off.

Started this thread Reply With Quote
  #9 (permalink)
 bukkan 
Calcutta, India
 
Experience: Intermediate
Platform: ArthaChitra
Posts: 278 since Jun 2009
Thanks Given: 161
Thanks Received: 271


MXASJ View Post
Thanks Sam,

Problem is Instrument.FullName+BarsPeriod.Id+BarsPeriod.Value can only be called in the OnBarUpdate()... I think.

in NT7 you can put that in OnStartUp()

Reply With Quote
Thanked by:
  #10 (permalink)
 MXASJ 
Asia
 
Experience: Beginner
Platform: NinjaTrader, TOS
Posts: 796 since Jun 2009
Thanks Given: 109
Thanks Received: 800


Thanks bukkan and Sam,

The code below works fine with the OnStartUp() and OnTermination() methods. Is whats left in OnBarUpdate() OK? Thanks for taking the time to educate me in writing better code.

 
Code
                            
protected override void Initialize()/////////////////////////////////////////////////////////////////////////////////////////////////////////
{
CalculateOnBarClose true;
 
}
 
protected 
override void OnStartUp()/////////////////////////////////////////////////////////////////////////////////////////////////////////
{
string dest path+filename;
string defname path + @"\" +Instrument.FullName+"_"+BarsPeriod.Value+"_"+BarsPeriod.Id+".txt";
defname = defname.Replace(' ','_'); // Replaces non-ISO file characters in Futures names
defname = defname.Replace('-','_'); // Replaces non-ISO file characters in Futures names
 
// If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
 
if (useDefaultFilename == false)
{
sw = File.AppendText(dest);

else
{
sw = File.AppendText(defname);
}
}
 
protected override void OnBarUpdate()///////////////////////////////////////////////////////////////////////////////////////////////////////
{
 
string sep = DataSeparator.ToString();// Cast required so the Date/Time separator and normal data separator are the same
 
// This is the output of all lines. The output format is: DateTime Open High Low Close Volume
string data = (Time[0] + sep + Open[0] + sep + High[0] + sep + Low[0] + sep + Close[0] + sep + Volume[0]);
 
if (splitDateTime == true)
{//Replace the space in DateTime with the DataSeparator
data = data.Replace(' ',DataSeparator);
}
 
sw.WriteLine(data);
 

 
protected override void OnTermination()//////////////////////////////////////////////////////////////////////////////////////////////////////
{
sw.Close();


Started this thread Reply With Quote




Last Updated on September 1, 2014


© 2024 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 - Privacy Policy - Downloads - Top
no new posts