Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Logging into a file

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

    Logging into a file

    I'm looking for a way to log to the same file from all my strategies.
    A few questions:
    • Is there a built-in way to do it?
    • I've shared a static StreamWriter between all running strategies, and use a lock() statement to avoid simultaneous access. When I backtest, however, I get an exception: The process cannot access the file because it is being used by another process. I guess this means that in backtesting, each strategy instance may run on a entire different process. Is this the case?
    • How about live trading? Does each instance run on a difference process? A different thread?


    Thanks,

    Boaz

    #2
    boaza,

    Here are some helpful guides on writing to an external file.

    NT reference example : http://www.ninjatrader.com/support/f...ead.php?t=3477

    Microsoft reference : http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

    I suspect your issue from question 2 is that the file is open for editing by one strategy when another strategy tries to access it. I would look closer at your protections for this to ensure they are working correctly. They should ideally stop accessing the file, i.e. close it, when any other strategy is accessing the file.

    As far as thread and processes go, I believe a limitation of indicators currently is that they run on 1 core, so even if they are individual threads they will have to wait for processor time. I will discuss this with development further. This will perhaps be updated in future versions.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Adam.
      I'm aware of how to sync access to file, and were the instances on different threads, a simple lock() would suffice. It is possible, although trickier to sync access between processes, and it will surely come with a performance hit.

      So I guess my question is:
      • During backtest, does each instance run or may run on a different thread or does it run on a different process?
      • Same for live trading.


      BTW,
      Code:
      File.AppendAllText()
      comes with an enormous performance penalty during backtest, that's why I was looking for a way to keep the file open between writes in the first place.

      Thanks,

      Boaz

      Comment


        #4
        Boaz, backtesting / optimization on a MultiCore machine could run several instances of the strategy so you would to guard your custom code for this scenario.

        Comment


          #5
          Bertrand,
          Generally speaking, multi-core has nothing to do with multi-threaded. I can run multiethreaded applications on a single-core machine an vice-versa.

          I know how to sync between different threads which belong to the same process.
          I can even sync between processes (e.g. using mutex).

          The thing is I don't know under which circumstances NT spawns threads, and under which it spawns processes.

          I this confidential / liable to change info?

          Comment


            #6
            Boaz,

            Generally 1 thread per core is optimal, however yes you can run multiple threads per core. Im not exactly sure about how NinjaTrader handles this, except that indicators/strategies will run on a single core when not being optimized. I'll ask development about this and get back to you.

            My understanding of Lock() is that it is for multi-threaded use, i.e. to prevent other threads from interfering with the resources being used by the thread that has the lock, not necessarily for say 10 strategies running separately on a single thread (if that is the case).
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Thanks Adam.
              As for the second part:
              1. It's OK to use lock() even on a single thread, e.g.
                Code:
                lock{ lock {x=0;}}
                , though useless, is fine.
              2. If all strategies run on the same thread, there is no need to sync writes to file, as lines from different strategies won't get intermingled anyway.


              Looking forward to your reply!

              Boaz

              Comment


                #8
                Boaz,

                Unfortunately this level of C# coding is unsupported. We cannot offer a best / highest performing solutions for each individual case. However, I do have some helpful information after talking to development.

                In live trading, all strategies run on a single thread. As such I don't believe the Lock will help you here. You can't open a file once its already open, instead you would need to somehow pass the StreamWriter object to the other strategies.

                In backtesting/optimization strategies will run multi-threaded. In this case, the Lock will make the other threads wait until one thread is done with the text file. Streamwriter won't let the other threads use the file.

                Some suggestions from the crew are :
                • Use a unique file name for each test and append them once finished.
                • Use some sort of context switch to open/close the text file.
                • Create a multi-instrument strategy for live trading.


                Anyway, best of luck with achieving your goal. Hopefully one of our community members can help.
                Adam P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                635 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                365 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                106 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                567 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                571 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X