Converting Indicator to Strategy help! - NinjaTrader Programming | futures io social day trading
futures io futures trading


Converting Indicator to Strategy help!
Updated: Views / Replies:3,034 / 12
Created: by MXASJ Attachments:2

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
 2  
 
Thread Tools Search this Thread
 

Converting Indicator to Strategy help!

  #1 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received

Converting Indicator to Strategy help!

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
Register to download File Type: zip ExportData.zip (2.2 KB, 9 views)
Register to download File Type: zip ExportChartData.zip (3.4 KB, 13 views)
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
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received


Problem solved by removing condition CurrentBar == 0

We now return to our regularly scheduled programming...

Reply With Quote
The following user says Thank You to MXASJ for this post:
 
  #4 (permalink)
Trading for Fun
College Point, NY (Queens)
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: AMP/CQG
Favorite Futures: YM
 
Posts: 63 since Nov 2009
Thanks: 5 given, 20 received

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)
Administrator: Retired Backtester
 Vendor: speedytradingservers.com 
Rennes France
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: IB/Kinetick
Favorite Futures: Futures
 
sam028's Avatar
 
Posts: 3,366 since Jun 2009
Thanks: 3,573 given, 3,982 received

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)
Reply With Quote
The following 2 users say Thank You to sam028 for this post:
 
  #6 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received

Thanks Sam,

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

Reply With Quote
 
  #7 (permalink)
Administrator: Retired Backtester
 Vendor: speedytradingservers.com 
Rennes France
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: IB/Kinetick
Favorite Futures: Futures
 
sam028's Avatar
 
Posts: 3,366 since Jun 2009
Thanks: 3,573 given, 3,982 received


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)
Reply With Quote
 
  #8 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received

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.

Reply With Quote
 
  #9 (permalink)
Elite Member
Calcutta, India
 
Futures Experience: Intermediate
Platform: ArthaChitra
 
Posts: 278 since Jun 2009
Thanks: 161 given, 259 received


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
The following 2 users say Thank You to bukkan for this post:
 
  #10 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received


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();


Reply With Quote

Reply



futures io > > > > > Converting Indicator to Strategy help!

Thread Tools Search this Thread
Search this Thread:

Advanced Search



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

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting mt4 indicator ? tradingstarpro MetaQuotes MetaTrader MQL Programming 8 May 26th, 2015 02:14 PM
Converting TOS Study to Ninja Strategy cjcornelius ThinkOrSwim 1 August 12th, 2011 07:10 PM
converting an NT6.5 indicator to NT7 aligator Traders Hideout 3 March 2nd, 2011 07:01 PM
Help converting an indicator to N7 tomonly23 NinjaTrader Programming 2 December 14th, 2010 02:02 PM
Converting Indicator From Mt4 to Ninja tradingstarpro NinjaTrader Programming 4 May 3rd, 2010 01:25 AM


All times are GMT -4. The time now is 07:10 AM.

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-16 in 0.12 seconds with 20 queries on phoenix via your IP 54.226.113.250