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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      79 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      63 views
      2 likes
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      54 views
      0 likes
      Last Post CarlTrading  
      Working...
      X