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:
Code:// Add this to your declarations to use StreamWriter using System.IO;
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;
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; }
Code:// 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:
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
-
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 martini, Yesterday, 04:45 PM
|
1 response
9 views
0 likes
|
Last Post Today, 06:45 AM | ||
Started by proptradingshop, 03-21-2024, 09:50 AM
|
5 responses
26 views
0 likes
|
Last Post Today, 05:17 AM | ||
Started by MaupinFinche, Today, 05:08 AM
|
0 responses
5 views
0 likes
|
Last Post
by MaupinFinche
Today, 05:08 AM
|
||
Started by Enkidu, Yesterday, 06:40 AM
|
9 responses
35 views
0 likes
|
Last Post
by Enkidu
Yesterday, 01:11 PM
|
||
Started by Mestor, 03-10-2023, 01:50 AM
|
12 responses
310 views
0 likes
|
Last Post
by z.franck
Today, 04:29 AM
|
Comment