Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Streamwriter not working in strategy (8.0.17.2)

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

    Streamwriter not working in strategy (8.0.17.2)

    I'm trying to adapt code from samplestreamwriter and it's not working in strategies, once I try to run the strategy is instantly disabled and can not access file. I used system.IO and streamreader before and had no problems at all.

    Error message:
    Strategy 'Samplestrategy': Error on calling 'OnBarUpdate' method on bar 20: Object reference not set to an instance of an object.

    Attached a sample strategy with the streamwriter.
    What is missing?
    Attached Files

    #2
    Hello aog076,

    'Object reference not set to an instance of an object' this error means a variable was used that had a null value without checking that it is not null first.

    For exampe;
    if (myObject != null)
    {
    // execute code
    }

    What is the exact line of code in OnBarUpdate() causing the error?

    I notice sw = File.AppendText(path); is in a condition but sw.WriteLine() is not.

    If two scripts attempted to open the same file, one would fail and the sw object would be null. Calling .WriteLine() with a null object would cause this error.


    I have an example that writes to file you may find helpful.
    Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea

      It might be a bug, if I use the same code in an indicator works fine, it writes on file on every barupdate while running indicator

      I copied from sampleStreamWriter indicator that was posted by NT team

      But when I try to use in a strategy but gives an error

      Attached sampleindicator, you can see that it works



      Attached Files

      Comment


        #4
        Hello aog076,

        If there is an error, then yes, there is a bug in the script.

        I was not able to confirm, which line is the exact line of code causing the error?

        Did you test the strategy I have provided you that writes to file? Is this also generating an error?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          The example RealtimeReplayHistoricalComparisonsExample works

          I'll be working from this script

          Thanks for your help

          Comment


            #6
            Hello aog076,

            Do you no longer want to correct the error in your script?

            It may be an easy fix..
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I just fixed it
              It's easy with the example provided

              Thanks

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              88 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              48 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              31 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              34 views
              0 likes
              Last Post TheRealMorford  
              Started by Mindset, 02-28-2026, 06:16 AM
              0 responses
              68 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Working...
              X