Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Overcoming Streamwriter Issues - Max String Length?

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

    Overcoming Streamwriter Issues - Max String Length?

    Hi,

    Within my strategy I use streamwriter to write various strings to a text file as and when they happen, to help with debugging and development. These are written within OnBarUpdate (for 10sec series & tick series) and OnExecutionUpdate. I've been experiencing occasional/intermittent streamwriter conflicts (file already in use) that causes my strategy to shut down (sometimes with open positions). I can't see any issues with the code and from research on this forum it seems like streamwriter is generally prone to issues. I'm considering the following approach to resolve:

    Rather than writing every update to a file, I store them in a String, with each update appending the new updates to the same string. Then only on each 10sec OnBarUpdate I write to the text file and reset the string to empty. This way, there's only one use of streamwriter every 10secs so no risk of conflict, however the downside is that the string itself could end up becoming large.

    Questions please:
    1. Is there a maximum length of the string?
    2. Are there any alternative approaches I should consider beyond string?

    Thanks in advance for any guidance

    ChainsawDR

    #2
    Hello ChainsawDR,

    The maximum string length in C# is 2^31 characters. That's because String.Length is a 32-bit integer However you would likely run into an out of memory exception before nearing that. Strings can be very large so the amount of data you are using from NinjaScript will unlikely reach the limits if you are resetting the string each time you write it.

    As long as what you are doing is less than that it should be fine.

    The conflicts you are running into would likely be caused due to using OnExecutionUpdate and OnBarUpdate at the same time, those two events don't happen in a specific order so you could have overlapping events. Moving the logic to exclusively OnBarUpdate should help to solve that problem.

    Comment


      #3
      Thank you Jesse! Super helpful!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      60 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X