View Full Version : how about I/0 in ninja?
silverness
10-23-2010, 08:06 PM
hello,first,thanks a lot for this useful and unique software.I am studying it.
can supporter tell me the I/0 in this software.
for example, I want do some stactistic,and must output data into a txt file.
pls tell me the syntax.thanks a lot.
MXASJ
10-24-2010, 12:59 AM
Ironically today I fixed a bug in a NT7 Strategy I wrote a few months ago to do just that. I use it with R. You might find it useful or get some ideas from it.
It exports DateTime, O,H,L,C data to txt or csv files, and in the code are notes on how to get it to exort indicator values as well.
FWIW the bug was an exception was thrown if a space was used in the path variable ("My Documents", for example).
Good luck!
silverness
10-24-2010, 02:20 AM
yes ,I need this ,thanks a lot.I will try it.
silverness
10-24-2010, 02:28 AM
first ,thanks for your post ,and I have had a idea about how to export DTOHLCV data to a txt file or csv file.
I have tried to open the attachment you post.but cannot.
I would like to get some information like this:
print("d:\testdata.txt",sma(15)[20]),can ninjatrader do this and the how about the syntax?
MXASJ
10-24-2010, 02:53 AM
It is a NT 7 Strategy. In NT 7, go to File>Utilities>Import Ninjascript, and point to that zip file.
Then go to the "Strategies" tab, right click and select New Strategy, select ExportData and use the parameters you want. Then tick the Enable box if you are connected to your historical data source. As it is a Strategy you can also apply it to an Instrument List.
If you have an output window it will tell you were the data is being written.
Disable the strategy once all the data has been loaded to write and close the file.
If you are on NT6.5 it won't work as-is but will require some code changes.
It uses System.IO and StreamWriter.
silverness
10-24-2010, 03:00 AM
thanks a lot.
NinjaTrader_Austin
10-24-2010, 01:10 PM
silverness, the reference sample for writing data to a file can be found here (http://www.ninjatrader.com/support/forum/showthread.php?t=3475).
superarrow
11-17-2010, 11:23 PM
MXASJ,
Thanks for the script. Just what I had been looking for. But this file is exactly as the last one you uploaded here (http://www.ninjatrader.com/support/forum/showthread.php?t=27942&highlight=data+export+script). Would you be able to share the script you alluded to in above so indicator values can be retrieved too?
Cheers.
MXASJ
11-18-2010, 12:43 AM
How to do it is in the notes in the code. Here is OnBarUpdate:
protected override void OnBarUpdate()
{
// This is the output of all lines. The output format is: DateTime Open High Low Close Volume
// If you wanted to output indicator data you could add that to string "data" as well, for example:
//string data = (Time[0] + dataSeparator + Open[0] + dataSeparator + High[0] + dataSeparator + Low[0] + dataSeparator + Close[0] + dataSeparator + EMA(14)[0]);
string data = (Time[0] + dataSeparator + Open[0] + dataSeparator + High[0] + dataSeparator + Low[0] + dataSeparator + Close[0] + dataSeparator + Volume[0]);
if (splitDateTime == true)
{//Replace the space in DateTime with the DataSeparator
data = data.Replace(@" ",dataSeparator);
}
sw.WriteLine(data);
}
In the commented-out example it records EMA(14) instead of volume. If you wanted just DateTime, Close, and EMA you would write:
string data = (Time[0] + dataSeparator + Close[0] + dataSeparator + EMA(14)[0]);
superarrow
11-18-2010, 10:30 AM
Got it to work, thanks MX.
djkiwi
12-14-2010, 12:56 PM
Hi Max, great job on this. Being able to exporting chart data to excel has saved so much time. I had a couple of questions on it though.
1. I was having problems saving my own file. When I selected usedefaultfilename as "false" and put my own file name in it said "invalid path, although I hadn't changed the path at all. Was wondering if I had done something wrong.
2. I'd like to analyze delta volume as part of an excel analysis I'm doing. So I put in this line (See the reference to Gom's deltamomentum indicator at the end. When I compile it there is no problem, but when I view the output the field is showing all zeros. Any help on this would be appreciated. Thanks. DJ
string data = (Time[0] + dataSeparator + Open[0] + dataSeparator + High[0] + dataSeparator + Low[0] + dataSeparator + Close[0] + dataSeparator + HMA(10)[0] + dataSeparator + GomDeltaMomentum().DeltaMomo[0]);
NinjaTrader_Josh
12-14-2010, 01:20 PM
djkiwi,
First step I would take is to eliminate any potential writing to file issues and first just see via a Print() call what exactly those values are.
MXASJ
12-14-2010, 03:28 PM
Thanks djkiwi,
I just tried setting useDefaultFileName to false, and tried both "test" and "test.txt" as filenames and it works OK. Here is my Output Window:
**NT** Enabling NinjaScript strategy 'ExportData/dd9bb80e79014c789233fbeb304d8564' ...
2010-12-15 06:14:35 Running Export Data
Writing to file: D:\Data\test.txt
Disable to close the file and allow access by other programs.
**NT** Disabling NinjaScript strategy 'ExportData/dd9bb80e79014c789233fbeb304d8564'
2010-12-15 06:14:40 Terminating ExportData on $USDJPY 1 Minute Data
File written to: D:\Data\test.txt
**NT** Enabling NinjaScript strategy 'ExportData/dd9bb80e79014c789233fbeb304d8564'...
2010-12-15 06:15:08 Running Export Data
Writing to file: D:\Data\test.txt.txt
Disable to close the file and allow access by other programs.
**NT** Disabling NinjaScript strategy 'ExportData/dd9bb80e79014c789233fbeb304d8564'
2010-12-15 06:15:11 Terminating ExportData on $USDJPY 1 Minute Data
File written to: D:\Data\test.txt.txt
The "Invalid Path" error is part of a Try/Catch block in OnStartUp(). I hate to sound like a support guy but can you give me some steps so I can try to reproduce it on my end? ;).
And Josh is right on the Print() statement as a good way to troubleshoot. I don't have the Gom stuff on my computer so I can't test here.
Raw cs file of the latest code of the ExportData strat is attached. It adds an enum for DateTime output format. Not 100% tested yet.
djkiwi
12-14-2010, 04:05 PM
Hi Guys, thanks for the fast responses. Well I did the output window part and this is what it says, looks like a security attributes issue. Interesting that it does not cause a problem writing the default file but when I try to write to a different file it causes a problem.
Max, please note that the GOMI thing does in fact work ok. Some of my data was missing so was not seeing it.
Thanks DJ.
Exception Details: System.IO.IOException: A required privilege is not held by the client.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at System.IO.File.AppendText(String path)
at NinjaTrader.Strategy.ExportData.OnStartUp()
**NT** Error on calling 'OnStartUp' method for strategy 'ExportData/d0ecbec61f4e447388cabced73c86122': Object reference not set to an instance of an object.
NinjaTrader_Josh
12-14-2010, 04:14 PM
That error message indicates you don't have permission to access the file you are trying to write to. That was received on your StreamWriter lines and not to do with the Print() method.
Suggest you use some try-catch blocks to isolate out exactly which line in your code threw this error if it is not immediately evident through your code where you ran into it. http://www.ninjatrader.com/support/forum/showthread.php?t=9825
MXASJ
12-14-2010, 04:30 PM
The try/catch block in OnStartUp() is working as expected I think. If you are using your own filename it is string dest, and here is the OnStartUp() logic:
protectedoverridevoid OnStartUp()
{
....
// If file at 'path' doesn't exist it will create the file. If it does exist, it will append the file.
try
{
if (useDefaultFilename == false)
{
sw = File.AppendText(dest);
Print(DateTime.Now + " Running Export Data");
Print("Writing to file: " + dest);
Print("Disable to close the file and allow access by other programs.");
}
else
{
sw = File.AppendText(defnamePath);
Print(DateTime.Now + " Running Export Data");
Print("Writing to file: " + defnamePath);
Print("Disable to close the file and allow access by other programs.");
}
}
catch (Exception e)
{
Print(DateTime.Now + " Exception caught in path and file variables");
Print("Please check your path and file name and try again");
Print ("Exception Details: " + e.ToString());
MessageBox.Show("Invalid Path. Please check your Path and Filename variables.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
sw.Close();
pathException = true;
Disable();
}
}
So the line in the Output Window after "Please check your path and file name and try again" should tell you what's up.
A common one is the DirectoryNotFound exception. I've not seen the one you are getting. Perhaps try to create a txt file in Notepad and Save As using the same file/path and see if your system allows it?
MXASJ
12-14-2010, 04:43 PM
FYI I found an error in the raw cs file posted earlier where I had and unused variable declared that messed with the Open[0] output.
This is unrelated to the Permissions issue but here is the updated code if anyone stumbles on the this thread later.
MXASJ
12-14-2010, 05:36 PM
djkiwi I've added another Print() statement in the exception handler that might help. It will now also print the full path+ file name as part of the exception report.
Give this one a try and let me know how you go. NT7 compile.
silverness
12-14-2010, 05:51 PM
I have read c# help guide and the reply for these helpful friend.
thanks a lot and I have catch the point.
best regards!
djkiwi
12-17-2010, 12:15 PM
Hi MXAS, thanks for your post on this, still trying to fix this issue. For now am happy just renaming the default file using the great little utility. I do have another mystery I was hoping you or Josh could provide some pointers.
Basically I am trying to export historical volume delta data from April to current and cannot seem to export the historical. I've attached two charts. The first is the current 01-11 CL contract showing volume delta data (at the bottom of the chart) derived from recorded Zen tick data using the gomi recorder. The second is the 05/10 CL contract and shows the volume delta based on historical tick data I loaded in myself.
The difference is when I export the data from 05-10 chart 2 the volume delta data does not show in the text file, only zeros show. All of the other data appears ok in the file though just not the volume delta data. Even though I can see it on the chart I cannot seem to get the data out. The 01-11 CL contract delta data shows up no problem. I know the historical volume delta data is stored in the mdocuments folder, so maybe it has something to do where the data is stored.
Any ideas on how to resolve this issue would be appreciated. Thanks. DJ
MXASJ
12-17-2010, 05:49 PM
Can't help you with that one, mate. There are no error checks in the code for database querries so you will need to use Print() statements to find out what syntax you need for that indicator to return what you want it to return.
My guess is something is wrong in this: GomDeltaMomentum().DeltaMomo[0]);
silverness
12-18-2010, 12:16 AM
anyone pls telll me.when I use the command Console.Writeline,where do the ninja print the result?thanks a lot.
NinjaTrader_Austin
12-18-2010, 02:48 PM
Silverness, Console.WriteLine is only applicable if you are using/developing a console application and/or debugging from Visual Studio. If you'd like to display something in NinjaTrader, please use the Print() function, which will send output to the output window (Tools -> Output Window).
richbois
01-03-2011, 05:02 PM
Hi MXASJ
Thank you for this indicator as it would solve a lot of my problems
But i have and issue i changed my computer clock to read what i think you wanted 19:01 01/03/2011
i put the indi on the chart and left the default settings as the were and created a D:/DATA folder
why is not been written or did i miss an instruction
thank you in advance
MXASJ
01-04-2011, 01:43 AM
Version 3.32 of the ExportData Strategy is the latest and is attached. You should not have to change your system clock as the Output DateTime format can now be selected in a drop-down list. It includes common formats and customizable one for your your Matlab or R preferences.
You should open the Output window when you enable the Strategy as most if not all error information can be found there.
richbois
01-04-2011, 08:12 AM
I did reset my clock to Windows default
tried strategy and got this error
Failed to call method 'Initialize' for indicator 'Atlasline_indicator': 'BarsInProgress' property can't be accessed from within 'Initialize' method
any clue????
MXASJ
01-04-2011, 03:58 PM
Can't help as that appears to be a problem with your code or your usage of the Atlas Line Indicator.
richbois
01-05-2011, 05:53 AM
now i removed the Atlas_line indicator i dont get any errors in the output window but i still dont get any file created
MXASJ
01-05-2011, 06:18 AM
On this Strategy statements will be sent to the output window if it is working, and if it is not. If no statements are printed to the output window, it means the stratgey has not been enabled.
Here is one that starts up OK:
**NT** Enabling NinjaScript strategy 'ExportData/87ab77398f0a4e4eab3ddb5355cf1e9f' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=True CancelExitOrdersOnDisable=False MaxRestarts=4 in 5 minutes
2011-01-05 21:12:29 Running Export Data
Writing to file: D:\Data\FGBL_03_11_1_Minute.txt
Disable to close the file and allow access by other programs.
Here is what happens when I Disable the Strat:
**NT** Enabling NinjaScript strategy 'ExportData/87ab77398f0a4e4eab3ddb5355cf1e9f' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=True CancelExitOrdersOnDisable=False MaxRestarts=4 in 5 minutes
2011-01-05 21:12:29 Running Export Data
Writing to file: D:\Data\FGBL_03_11_1_Minute.txt
Disable to close the file and allow access by other programs.
**NT** Disabling NinjaScript strategy 'ExportData/87ab77398f0a4e4eab3ddb5355cf1e9f'
2011-01-05 21:12:55 Terminating ExportData on FGBL 03-11 1 Minute Data
File written to: D:\Data\FGBL_03_11_1_Minute.txt
And if I try to write to Drive Z: I get a pop up window and this in the Output:
**NT** Enabling NinjaScript strategy 'ExportData/87ab77398f0a4e4eab3ddb5355cf1e9f' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=True CancelExitOrdersOnDisable=False MaxRestarts=4 in 5 minutes
2011-01-05 21:13:23 Exception caught in path and file variables
Path Z:\Data\FGBL_03_11_1_Minute.txt is invalid.
Please check your path and file name and try again
Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'Z:\Data\FGBL_03_11_1_Minute.txt'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append)
at System.IO.File.AppendText(String path)
at NinjaTrader.Strategy.ExportData.OnStartUp()
**NT** Error on calling 'OnStartUp' method for strategy 'ExportData/87ab77398f0a4e4eab3ddb5355cf1e9f': Object reference not set to an instance of an object.
Which tells me the path is no good.
What did you get in the Output window?
richbois
01-05-2011, 07:40 AM
i get nothing in the output window
i must admit ii never used a strategy before so maybe I am doing something wrong there
all i do is open the strategy the same way i would an indicator
I hope that is correct
richbois
01-05-2011, 07:44 AM
ok i see what i was doing wrong
you have to enable the strategy seperatly in the parameters duhhhhhhhhhh
now i have a file created
Thank you very much for your help