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

Error while using StreamWriter

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

    Error while using StreamWriter

    Hi,

    I am sometimes getting an error while using my indicator which says "Error on calling 'OnMarketData' method on bar 1589: writeFile() error: The process cannot access the file 'C:\full path' because it is being used by another process"

    Now for writeFile() method I am using below:

    public static void writeFile()
    {
    try
    {
    string slFilePath = NinjaTrader.Core.Globals.UserDataDir.ToString() + @"\Directory";

    if (!System.IO.Directory.Exists(slFilePath))
    {
    System.IO.Directory.CreateDirectory(slFilePath);
    }
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(slFilePath+ System.IO.Path.DirectorySeparatorChar + "filename.csv", false))
    {
    lock(Globals.dZoneLockObject)
    {
    foreach (Zone box in Globals.Boxes)
    {
    sw.WriteLine(box.serialize());
    }
    }
    }
    }
    catch (Exception e)
    {
    throw new Exception("writeFile() error: " + e.Message);
    }

    }

    Any suggestion will be appreciated around why am I getting this massage. Thanks in advance.

    #2
    Hello asmmbillah,

    That can happen if more than one operation is happening with the file at once. OnMarketData is called very frequently so I could potentially see that being a problem being used from that override. Are you writing tick events or something at a fixed interval? To avoid the problem you can try to slow down the operation you are doing, that could include items like counting a number of ticks before writing or using OnBarUpdate events.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply. I am writing in tick events. Can you please explain further or an example of what you said about "To avoid the problem you can try to slow down the operation you are doing, that could include items like counting a number of ticks before writing or using OnBarUpdate events."? thanks in advance.

      Comment


        #4
        Hello asmmbillah,

        Another way to put that would be to space out the times you are writing to file. If you are writing on each tick you will likely hit problems like this, you would likely need to accumulate data for a period of time an then write the data at set intervals. That would allow the operation time to open the file, write the data and close it.

        How you do that would be up to you, one way would be to use a counter. For example you could accumulate data for 100 ticks and then after that write the data and clear the counter. It would only be writing data for each 100 ticks and would give it more time in between writes.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thanks again for your reply. Do you guys have any indicator which is publicly available and uses the concept you suggested to get more indepth idea about your suggestion? You used to have free indicaors within your echosystem, anything there uses your suggestion? thanks in advance.

          Comment


            #6
            Hello asmmbillah,

            Not that I am aware of, that was just the first thought that came to mind for slowing down the number of times you are writing to file. You can slow it down however you want to the point I was trying to make was that it needs to not be happening on each tick.

            You could use a secondary series as another way to do that, we do have samples for using secondary series. https://ninjatrader.com/support/help...barsinprogress

            In that sample it shows how to execute code a specific intervals, you could do the same for your writing of data.

            If you want the data for each tick like you have it writing now you would also need to accumulate the data between each write. If you space the writes out then some kind of additional storage would be needed for the result you are gathering on each tick. Most likely a List would need to be used for that so you can loop over it and write all of the data at once each X interval and clear it.


            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Not sure if this would work for what you are doing, but create an array/series with the value of each tick as an index and at the close of every bar write out the last bars data, create a new array/series and null out the old one after it was written. That way your writes would be limited to the bar cycle and it would minimize file I/O and avoid these issues rather than trying to write realtime.

              Just a thought. Good luck.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by jclose, Today, 09:37 PM
              0 responses
              6 views
              0 likes
              Last Post jclose
              by jclose
               
              Started by WeyldFalcon, 08-07-2020, 06:13 AM
              10 responses
              1,414 views
              0 likes
              Last Post Traderontheroad  
              Started by firefoxforum12, Today, 08:53 PM
              0 responses
              11 views
              0 likes
              Last Post firefoxforum12  
              Started by stafe, Today, 08:34 PM
              0 responses
              11 views
              0 likes
              Last Post stafe
              by stafe
               
              Started by sastrades, 01-31-2024, 10:19 PM
              11 responses
              169 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Working...
              X