NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 01-19-2007, 01:57 AM   #1
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Post imported post

NinjaScript does not provide convenience methods for reading and writing files. You must use classes within .NET framework for File IO.

The System.IO name space provides types for reading and writing to files.

http://msdn2.microsoft.com/en-us/library/system.io.aspx

Following is a conceptual sample of using the System.IO.StreamWriter class.

Create a method:
Quote:
privatevoid WriteFile(string fileName, string line)
{
// Creates the file path
string file = NinjaTrader.Cbi.Core.UserDataDir + @"log" + fileName;

// Writes out a line to the specified file name
try
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(file))
{
writer.WriteLine(line);
}
}
catch (System.Exception exp)
{
Log(
"File write error for file name '" + fileName + "' Error '" + exp.Message + "'", LogLevel.Warning);
}
}



Then call this method from within OnBarUpdate():

Quote:
protected overridevoid OnBarUpdate()
{
if (condition == true)
WriteFile(
"myFile", "The condition is true");
}



Ray
NinjaTrader_Ray is offline  
Reply With Quote
Old 03-30-2007, 10:56 AM   #2
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Post imported post

I got the following error when I tried to compile this:

Quote:
'NinjaTrader.Cbi.Globals' does not contain a definition for 'InstallDir' Strategy\Strategy12.cs 421 33
Any ideas on what to do about this?

Also, I need a way to flush and close the file at the end of back testing.

Is it possible that there is another method that would only be called when the Strategy Analyzer completes running backtesting, where someone clould put close commands and stuff like that so one doesn't have to restart NinjaTrader to close a file?

Thanks.

KBJ

KBJ is offline  
Reply With Quote
Old 03-30-2007, 04:05 PM   #3
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Post imported post

To fix the compile issue, please try
Code:
NinjaTrader.Cbi.Core.UserDataDir
NinjaTrader_Dierk is offline  
Reply With Quote
Old 03-31-2007, 01:45 AM   #4
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Post imported post

Is this, perhaps, version dependent? I am using the field test (6.0.0.9).

Applying your recommendation, I now get the following message:

Quote:
The type or namespace name 'Core' does not exist in the namespace 'NinjaTrader' (are you missing an assembly reference?)
Also, I need a way to flush and close the file at the end of back testing.

Does NinjaTrader have any method that could be defined in my application that would be called when the Strategy Analyzer completes a backtest, so that I may places code in that method that would close a file?

I have coded a work-around for problem I was experiencing, but I still need a way to close a file at the end of backtesting. My current method is to stop NinjaTrader and restart it after every test. This is slow and boring. Can you offer a faster way of doing this?
KBJ is offline  
Reply With Quote
Old 03-31-2007, 03:29 AM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Post imported post

Sorry. Try

Quote:
NinjaTrader.Cbi.Core.UserDataDir
Regarding flush and close the file...Could you eloborate on what you want to do? Not sure what you mean by stop NT, restart after every test.

Thanks
NinjaTrader_Ray is offline  
Reply With Quote
Old 03-31-2007, 10:53 AM   #6
KBJ
Senior Member
 
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
Post imported post

That worked.... thanks.

You edited your original posting, so now all my earlier comments look like they're imaginary. I'll have to be more careful what I complain about!

What I'm talking about with "closing" is this...

When I couldn't get the code snippet you have below to work, I went to MSDN and found a cose sample to write into a file using FileInto and CreateText to open the file and then WriteLine to do the output.

When using this approach, what I've found is that after running a back test from the Strategy Analyzer, if I try to do another backtest, I get this error message:

Quote:
3/31/2007 6:25:05 PM Strategy Error on calling 'OnBarUpdate' method for strategy 'Strategy12': The process cannot access the file 'Cocuments and SettingsNew UserMy DocumentsNinjaTrader 6trace.txt' because it is being used by another process.
The only way I've found to prevent this error is to close the file by exiting NinjaTrader after each and every backtest, which as you might imagine gets a little tedious.

So, I was thinking that if there were a way for my application to get control back one final time AFTER all of the backtest data has been processed by OnBarUpdate, perhaps by defining another method just for this purpose, then it could contain some code to close the file that was created.

Or, if on the very last call to OnBarUpdate, there were some indicator to signify that it was the last of the backtest data, then I could do the close before exiting (although this approach does not handle closing the file if the script aborts before its completely run.)

And as far as a "flush" goes, I guess that would be done implicitly by the close. I was just uncertain as to whether all of the data was being written to the file, thinking that perhaps some was still left in a buffer.

Anyway, those were my thoughts on it. Since I don't understand NinjaTrader's internals, I may have suggested a less than optimum solution.

Nevertheless, I feel that there should be functionality of this type available, and I'd hope that it wouldn't be something difficult to implement.

Thanks,

KBJ

KBJ is offline  
Reply With Quote
Old 04-02-2007, 03:27 AM   #7
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Post imported post

You could override the Dispose() method to close the file. This should get called when a backtest is complete.

See the Help Guide Dispose() method for additional information.

Ray
NinjaTrader_Ray is offline  
Reply With Quote
Old 12-14-2009, 05:05 PM   #8
ATI user
Senior Member
 
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
Default

Ray

I am trying to write an OIF, in NT7beta5, to the 'incoming' folder and have the wrong path...i.e. it is writing...somewhere....no errors in the Log

BTW your code is missing a space between 'private' and 'void'

code:


private void WriteFile(string fileName, string line )
{
// Creates the file path
string file = NinjaTrader.Cbi.Core.UserDataDir + @"incoming" + fileName;

// Writes out a line to the specified file name
try
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(file))
{
writer.WriteLine(line);
}
}
catch (System.Exception exp)
{
Log("File write error for file name '" + fileName + "' Error '" + exp.Message + "'", LogLevel.Warning);
}
}
ATI user is offline  
Reply With Quote
Old 12-14-2009, 05:18 PM   #9
ATI user
Senior Member
 
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
Default

actually I tried in 7 and 6.5 and in both cases it wrote the file to the NinjaTrader folder

and named if first 'logoif.txt' and then with the above code 'incomingoif.txt' and now 'oif.txt

so clearly that code appends a suffix

tried several iterations for path....no luck

please specify path to NinjaTrader7/incoming..

thanks
ATI user is offline  
Reply With Quote
Old 12-15-2009, 07:20 AM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

ATI user,

What you can do is print the string of the path to see what exactly it is coming out as. Then you can make amends/changes however you see fit to match your directory.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-15-2009, 08:36 AM   #11
ATI user
Senior Member
 
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
Default

thanks Josh

this code prints...
string file = NinjaTrader.Cbi.Core.UserDataDir + @"" + fileName;
Print (" string file = "+file);

C:\Users\Scott\Documents\NinjaTrader 7\oif.txt

I want it to print....to get the file in the 'incoming' folder

C:\Users\Scott\Documents\NinjaTrader 7\incoming\oif.txt

however, everything I try to change NinjaTrader.Cbi.Core.UserDataDir will not compile

and this code


string file = "C:\Users\Scott\Documents\NinjaTrader 7\incoming\oif.txt";

gets error 'unrecognized escape sequence'
Last edited by ATI user; 12-15-2009 at 08:42 AM.
ATI user is offline  
Reply With Quote
Old 12-15-2009, 08:40 AM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

It is just a matter of you manipulating the string. Before fileName part of your string concatenation, just add another for the \incoming directory.
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-15-2009, 09:01 AM   #13
ATI user
Senior Member
 
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
Default

that just adds it to the front of the filename

string file = C:\Users\Scott\Documents\NinjaTrader 7\incomingoif.txt

this code won't compile

string file = NinjaTrader.Cbi.Core.UserDataDir + "\incoming\"+ fileName;


I need something other than

NinjaTrader.Cbi.Core.UserDataDir

could you please try this and tell me the path/code I need...I have wasted hours on this

thanks
Last edited by ATI user; 12-15-2009 at 09:04 AM.
ATI user is offline  
Reply With Quote
Old 12-15-2009, 09:16 AM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

You need to use string literals for \. Please see this reference sample: http://www.ninjatrader-support2.com/...ad.php?t=19174
NinjaTrader_Josh is offline  
Reply With Quote
Old 12-15-2009, 09:53 AM   #15
ATI user
Senior Member
 
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
Default

right

this code works fine

string file = NinjaTrader.Cbi.Core.UserDataDir + @"\incoming\"+ fileName;
ATI user is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 09:37 AM.