![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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:
Then call this method from within OnBarUpdate(): Quote:
Ray
Ray
NinjaTrader Customer Service |
||
|
|
|
|
|
#2 | |
|
Senior Member
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
|
I got the following error when I tried to compile this:
Quote:
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 |
|
|
|
|
|
|
#3 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
To fix the compile issue, please try
Code:
NinjaTrader.Cbi.Core.UserDataDir
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#4 | |
|
Senior Member
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
|
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:
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? |
|
|
|
|
|
|
#5 | |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
Sorry. Try
Quote:
Thanks
Ray
NinjaTrader Customer Service |
|
|
|
|
|
|
#6 | |
|
Senior Member
Join Date: Mar 2007
Location: , Florida, USA
Posts: 663
Thanks: 36
Thanked 7 times in 6 posts
|
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:
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 |
|
|
|
|
|
|
#7 |
|
Administrator
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
|
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
Ray
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
|
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); } } |
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
|
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 |
|
|
|
|
|
#10 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
|
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.
|
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
It is just a matter of you manipulating the string. Before fileName part of your string concatenation, just add another for the \incoming directory.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
|
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.
|
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
You need to use string literals for \. Please see this reference sample: http://www.ninjatrader-support2.com/...ad.php?t=19174
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Apr 2006
Location: , ,
Posts: 1,316
Thanks: 1
Thanked 7 times in 7 posts
|
right
this code works fine string file = NinjaTrader.Cbi.Core.UserDataDir + @"\incoming\"+ fileName; |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|