Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to write cum. profit (currency) to file at a set time

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    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;
    Variables:
    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 the Try Catch used to write to the file, your information would go in the sw.WriteLine:
    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;
    			}
    The following is necessary to dispose of the resources used.
    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;
    			}
    		}
    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=3475

    Please let me know if I may be of further assistance.

    Comment


      #3
      Thanks for the help

      I will try use the pointers on how to use the code and will get back to you on whether the strategy is working as expected.

      Comment


        #4
        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


          #5
          Hello mbesha,

          Thank you for your response.

          It looks like it is still printing out the Open, High, Low and Close.

          Can you attach your indicator or strategy you are using here to your response so I may investigate this matter further?

          Comment


            #6
            Here it is as per your request

            Please find attached the code for the custom strategy as a *.txt file
            Attached Files

            Comment


              #7
              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.
              Your strategy is not running and it is likely you have another strategy that is still writing the example to the text file.

              Disable all running strategies and remove any indicator that uses StreamWriter or Reader and then test your strategy once more.

              Comment


                #8
                Thanks for the feedback. I will give it a try and I will get back to you on the results.

                Comment


                  #9
                  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


                    #10
                    Hello mbesha,

                    Sure, we have a reference sample that you can download at the following links.

                    StreamWriter:


                    StreamReader:
                    JCNinjaTrader Customer Service

                    Comment


                      #11
                      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


                        #12
                        Originally posted by mbesha View Post
                        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)?
                        Do you see the the same error or any error in the output window?

                        Comment


                          #13
                          Well, first I got the results I posted some time earlier in this thread and now upon checking on the output txt file, I noticed that it is not being updated any more. The last thing written on it was 20130513 1.2984 1.2984 1.2983 1.2983.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by andrewtrades, Today, 04:57 PM
                          1 response
                          4 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by chbruno, Today, 04:10 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post chbruno
                          by chbruno
                           
                          Started by josh18955, 03-25-2023, 11:16 AM
                          6 responses
                          436 views
                          0 likes
                          Last Post Delerium  
                          Started by FAQtrader, Today, 03:35 PM
                          0 responses
                          6 views
                          0 likes
                          Last Post FAQtrader  
                          Started by rocketman7, Today, 09:41 AM
                          5 responses
                          19 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X