Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to write cum. profit (currency) to file at a set time
Collapse
X
-
How to write cum. profit (currency) to file at a set time
Hi. I wanted to know how after a day of trading I can have the cumulative profit towards the close of the market written in a text file (day trader) . I read the thread on the streamwriter and have tried having a look at its code but I still don't know how exactly to incorporate it into my custom NT strategy. Do I just add the samplestreamwriter to Initialize() as I would do any other indicators? How do I modify the code in the sample code to achieve what I want to do? Do I have to write a specific file path to the file I want created (or do I have to create it to begin with?) or will the streamwriter generate the output file automatically? Please help me out.
-
Hello mbesha,
Thank you for your note.
You would not call the SampleStreamWriter into your strategy. You would implement the methods, properties and declarations from the SampleStreamWriter into your strategy.
The following are the necessary lines to include in your strategy:
Variables:Code:// Add this to your declarations to use StreamWriter using System.IO;
The following is the Try Catch used to write to the file, your information would go in the sw.WriteLine:Code:// This sets the path in which the text file will be created. private string path = Cbi.Core.UserDataDir.ToString() + "MyTestFile.txt"; // Creates a StreamWriter and a StreamReader object private System.IO.StreamWriter sw;
The following is necessary to dispose of the resources used.Code:/* 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. */ try { // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file. if (CurrentBar == 0) sw = File.AppendText(path); // This is the output of all lines. The output format is as follows: Date Open High Low Close sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]); } catch (Exception e) { // Outputs the error to the log Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error); throw; }
All this information is given in the SampleStreamWriter reference sample. I recommend playing with the sample to understand how it works and what parts of the code are needed for your purpose: http://www.ninjatrader.com/support/f...ead.php?t=3475Code:// Necessary to call in order to clean up resources used by the StreamWriter object protected override void OnTermination() { // Disposes resources used by the StreamWriter if (sw != null) { sw.Dispose(); sw = null; } }
Please let me know if I may be of further assistance.
-
Results aren't what I am expecting
Hi. I tried using this code.
if (ToDay(Time[0]) == 53000)
{
/* 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. */
try
{
// If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
if (CurrentBar == 0)
sw = File.AppendText(path);
// This is the output of all lines. The output format is as follows: Date Open High Low Close
sw.WriteLine(ToDay(Time[0]) + " NetProfit: " + Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit);
}
However, the result I got was this.
20130513 1.2956 1.2962 1.2956 1.2962
20130513 1.2962 1.2965 1.2962 1.2965
20130513 1.2965 1.2966 1.2965 1.2966
20130513 1.2966 1.2969 1.2966 1.2969
20130513 1.2969 1.297 1.2969 1.297
20130513 1.297 1.2971 1.297 1.2971
20130513 1.297 1.297 1.2961 1.2961
20130513 1.2961 1.2961 1.296 1.296
20130513 1.296 1.296 1.2959 1.2959
20130513 1.2959 1.2959 1.2958 1.2958
20130513 1.2958 1.2958 1.2957 1.2957
20130513 1.2958 1.2971 1.2958 1.2971
20130513 1.2971 1.2973 1.2971 1.2973
20130513 1.2973 1.2974 1.2973 1.2974
and the list goes on and on.
Could you please tell me what is wrong with my code?
Comment
-
Here it is as per your request
Please find attached the code for the custom strategy as a *.txt fileAttached Files
Comment
-
Hello mbesha,
Thank you for your response.
When running your strategy I see the following error in my Log tab of the Control Center:
Your strategy is not running and it is likely you have another strategy that is still writing the example to the text file.You cannot write and read from the same file at the same time. Please remove SampleStreamReader.
Disable all running strategies and remove any indicator that uses StreamWriter or Reader and then test your strategy once more.
Comment
-
Feedback
I checked my strategies and there is only one strategy that was using the StreamWriter so I quite frankly don't have a clue as to what is the root cause of the problem. Anyway, could you please tell me the code I should use to achieve my objective? It seems mine may be bugged so could you give me your version of how to do it? Thanks.
Comment
-
Thanks but could I get something more relevant
I have already checked the reference samples and if you check my code you can see that I used much of the code in the reference samples already with my own modifications (without much success though). Could you then point out what's wrong with my code (esp. with regards to the code for streamwriter/reader part)?
Comment
-
Do you see the the same error or any error in the output window?Originally posted by mbesha View PostI have already checked the reference samples and if you check my code you can see that I used much of the code in the reference samples already with my own modifications (without much success though). Could you then point out what's wrong with my code (esp. with regards to the code for streamwriter/reader part)?
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
79 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
45 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
29 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
32 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
64 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment