Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Can't Apply StreamWriter to two Charts

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

    Can't Apply StreamWriter to two Charts

    When I apply an indicator with StreamWriter to one chart it works, but when I try to apply that same indicator to a second chart, it does not load on the second chart but continues to work on the first chart. I get no error messages and nothing in the Log.

    Is it possible for StreamWriter to write to the same file from two different charts?

    regards,
    taddypole....


    ================================================== ===========

    Variables

    private System.IO.StreamWriter sw;

    OnStartUp()

    sw = new StreamWriter(@"c:\temp\recordedTrades.txt", false);

    #2
    Originally posted by Taddypole View Post
    When I apply an indicator with StreamWriter to one chart it works, but when I try to apply that same indicator to a second chart, it does not load on the second chart but continues to work on the first chart. I get no error messages and nothing in the Log.

    Is it possible for StreamWriter to write to the same file from two different charts?

    regards,
    taddypole....


    ================================================== ===========

    Variables

    private System.IO.StreamWriter sw;

    OnStartUp()

    sw = new StreamWriter(@"c:\temp\recordedTrades.txt", false);
    And if you were to use a different file name for the other chart it work?

    Also adding a try/catch with Print would return any errors to NT output window.
    Last edited by sledge; 05-11-2014, 07:27 AM. Reason: exception programming

    Comment


      #3
      Thanks Sledge,

      Yes, using a different file name does work, but I was hoping to be able to have either chart write to the same file. Is that possible?

      taddypole....

      Comment


        #4
        Originally posted by Taddypole View Post
        When I apply an indicator with StreamWriter to one chart it works, but when I try to apply that same indicator to a second chart, it does not load on the second chart but continues to work on the first chart. I get no error messages and nothing in the Log.

        Is it possible for StreamWriter to write to the same file from two different charts?

        regards,
        taddypole....


        ================================================== ===========

        Variables

        private System.IO.StreamWriter sw;

        OnStartUp()

        sw = new StreamWriter(@"c:\temp\recordedTrades.txt", false);
        You need to open the file correctly for concurrent access.

        ref: http://msdn.microsoft.com/en-us/libr...(v=VS.90).aspx

        Comment


          #5
          Hello Taddypole,

          Thank you for your post.

          Koganam provides the needed enumeration here to allow multiple scripts to access and write to the same file. If you have any questions on implementing this into your scripts please let me know.

          Comment


            #6
            Originally posted by NinjaTrader_PatrickH View Post
            Hello Taddypole,

            Thank you for your post.

            Koganam provides the needed enumeration here to allow multiple scripts to access and write to the same file. If you have any questions on implementing this into your scripts please let me know.
            Can you post the lines on how this script would need to be modified?


            Code:
                    #region Variables
            		// This sets the path in which the text file will be created.
            		private string path 			= "C:\\Users\\Forrest\\Desktop\\MyTestFile.txt";
            		private System.IO.StreamWriter sw;
                    #endregion
            
                    protected override void Initialize()
                    {
                        CalculateOnBarClose	= true;
                    }
            
                    protected override void OnBarUpdate()
                    {
            			try
            			{
            				if (CurrentBar == 0)
            					sw = File.AppendText(path);
            
            				sw.WriteLine(ToDay(Time[0]) + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
            			}
            			
            			catch (Exception e)
            			{
            				Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
            				throw;
            			}
            		}
                    }
            
            		protected override void OnTermination() 
            		{
            			if (sw != null)
            			{
            				sw.Dispose();
            				sw = null;
            			}
            		}

            Comment


              #7
              Hello forrestang,

              Thank you for your post.

              You would need to add the FileStream() to the following lines of code in the OnBarUpdate() Try Catch:
              Code:
              				if (CurrentBar == 0)
              				{
              					fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
              					sw = File.AppendText(path);
              fs would need to be defined as a FileStream in the variables section of the code:
              Code:
                      #region Variables
              		...
              		// Creates a StreamWriter and a StreamReader object
              		private System.IO.StreamWriter sw;
              		private FileStream fs;
                      #endregion

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello Taddypole,

                Thank you for your post.

                Koganam provides the needed enumeration here to allow multiple scripts to access and write to the same file. If you have any questions on implementing this into your scripts please let me know.
                I've pulled out all the relevant references to the filestream, streamwriters and streamreaders from the indicator that I'm having problems using on more than one chart. They are listed in the StreamWriter Conflict Code.txt attached.

                I've tried to address the fileShare enumeration issue that Koganam referred to but no luck. Also, I've attached a print of the output screen of the errors that I get when I try to load the indicator onto a second chart. Those are found in the output.txt attachment.

                Just not sure what I'm doing wrong.

                Regards,
                taddypole...
                Attached Files

                Comment


                  #9
                  taddypole, naturally this being a deeper C# topic there's limited support we could provide here, however running some quick testing with NG and ES 1 min charts the attached seems to work well for me write from two charts to the same file. Append mode needs to be done in Write mode, but feel for your logging that would be sufficient.
                  Attached Files
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    thank you Bertrand

                    I verified the your code can write to the same file from two different charts. So that's affirmed.

                    I then modified the code to read the Ninja Trader Log file. When I do that, I get an error. I've attached a screen shot in VS showing that the error is realted to the log file being in use.

                    In the FileStream used for logPath, I've enumerated the FileShare to be "Read". Shouldn't that allow me to read the Ninja Trader log file even though it is in use by Ninja Trader?
                    Attached Files

                    Comment


                      #11
                      Originally posted by Taddypole View Post
                      thank you Bertrand

                      I verified the your code can write to the same file from two different charts. So that's affirmed.

                      I then modified the code to read the Ninja Trader Log file. When I do that, I get an error. I've attached a screen shot in VS showing that the error is realted to the log file being in use.

                      In the FileStream used for logPath, I've enumerated the FileShare to be "Read". Shouldn't that allow me to read the Ninja Trader log file even though it is in use by Ninja Trader?
                      No. Not if the previous application that opened the file opened it for exclusive access. That is the same reason that you have to make specific access available when you arrange to open your own file, the reason for this thread in the first place.

                      Just think about it: if we could overrride restricted access by simply opening a filestream in a more permissive mode to access the same restricted file, then no file restrictions can ever be valid.

                      Comment


                        #12
                        Thanks Koganam

                        But what I knew is that I can read the file using NotePad... so I thought I should be able to also read it using FileStream. I did more searching on the web and found that someone suggested that FileShare needed to be ReadWrite and not just Read. This doesn't make sense to me but when I tried it, I no longer got the error.

                        I assume what this means is if Ninja has a ReadWrite FileShare on the log file, then I need to match it with my FileStream.

                        Just a guess....
                        regards,
                        taddypole...

                        Comment


                          #13
                          Originally posted by Taddypole View Post
                          Thanks Koganam

                          But what I knew is that I can read the file using NotePad... so I thought I should be able to also read it using FileStream. I did more searching on the web and found that someone suggested that FileShare needed to be ReadWrite and not just Read. This doesn't make sense to me but when I tried it, I no longer got the error.

                          I assume what this means is if Ninja has a ReadWrite FileShare on the log file, then I need to match it with my FileStream.

                          Just a guess....
                          regards,
                          taddypole...
                          That does surprise me. Logic would seem to imply that a file should be accessible if opened a second time in a more restrictive mode by a new filestream. Apparently not, it seems. The notion that all filestreams accessing the file must use the same mode is one that I did not realize, maybe because the only time I have ever used such multiple access has been with files that I created, so I always used the same mode for all access, even as I did not realize that it was actually required.

                          What you observed relative to Notepad would make sense too, as Notepad definitely opens files in ReadWrite mode.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by TraderBCL, Today, 04:38 AM
                          1 response
                          4 views
                          0 likes
                          Last Post bltdavid  
                          Started by martin70, 03-24-2023, 04:58 AM
                          14 responses
                          105 views
                          0 likes
                          Last Post martin70  
                          Started by Radano, 06-10-2021, 01:40 AM
                          19 responses
                          606 views
                          0 likes
                          Last Post Radano
                          by Radano
                           
                          Started by KenneGaray, Today, 03:48 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post KenneGaray  
                          Started by thanajo, 05-04-2021, 02:11 AM
                          4 responses
                          470 views
                          0 likes
                          Last Post tradingnasdaqprueba  
                          Working...
                          X