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

Writing to / reading from the same file

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

    Writing to / reading from the same file

    I'm trying to communicate with another platform by having NinjaTrader write to a few files and then have the other platform reading those files periodically. I could use a little help with the file commands.

    On the other platform, I've set it up to say:
    Can I read the file? No.
    Can I read the file? No.
    Can I read the file? Yes! Read file into memory then close!

    I need to do that same thing with NinjaScript. Right now I'm having it store lines to be written in a Queue<string>. I need to have it say:
    Can I write to the file? No.
    Can I write to the file? No.
    Can I write to the file? Yes! Dump the Queue into the file then close it!

    Here's what I have:

    WriteQueue.Enqueue(LineToExport);
    if (WriteQueue.Count >= 10)
    {
    while (WriteQueue.Count > 0)
    {
    LineToExport = WriteQueue.Dequeue();
    File.AppendAllText(path, LineToExport);
    }

    }

    Everything dies if the file is open currently. I'm pretty new at this language and just don't know how to get it there, or even if this System.IO.File class is the right way to go about it. Does this look like it could work? Do I just need to stick a try{} block around it and I'm good to go?

    #2
    Hello jchorton,

    Thank you for your post.

    You could use System.IO File properties to write to and read from a text file. System.IO File properties would be used when you only want to write/read a small amount of data.

    See the example in this help guide documentation for how to use System.IO File properties - https://ninjatrader.com/support/help..._propertie.htm

    Alternatively, you could also use a StreamReader to bring in the data stored in text files and allow you to do manipulations with them. A StreamWriter could be used to store information related to certain market conditions or trades outside of NinjaTrader.

    Please see the examples in the help guide documentation below for how a StreamReader or StreamWriter could be used.
    StreamReader - https://ninjatrader.com/support/help...o_read_fro.htm
    StreamWriter - https://ninjatrader.com/support/help...o_write_to.htm

    Let us know if we may further assist.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      jchorton,

      It seems your approach is:
      1. Write a file in NinjaTrader that is then read by the "other platform".
      2. Subsequently update/replace the NT file and read it again in the other platform.
      If that is so, here are some thoughts:
      • If the approach requires a single file that must be the same file each time it is used, you could use C# locking features as well as file sharing features to regulate access to the file. (A search will easily find documentation on these on Microsoft's site.)
      • If the approach does not require the same single file every use, consider simply writing NT files as needed (with some form of unique name each time, probably timestamp-based) and scanning for any new files in the other platform which can then be read as required.
      If you have a bit more detail on what you are trying to achieve (not how), that may make it easier to offer advice.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Thanks for the replies!

        Originally posted by jeronymite View Post
        If you have a bit more detail on what you are trying to achieve (not how), that may make it easier to offer advice.
        Sorry. I'm trying to get data from an exclusive NinjaTrader indicator into TradeStation. I managed to get the backdata over there, but the fellow I'm working for wants to see it coming in in real-time so he can watch how it moves along with the other indicators in our arsenal. (There's no playback feature over there!) The TradeStation file reader is very simple. It can read a file line by line. There are no special nor elegant features.

        My approach is to actually use a pair of files: One file that holds all the data and another file that only holds the most recent ~30 lines. The big file will be appended and the smaller file will be replaced every bar (or a few bars). The indicator in TradeStation will be able to fill its backdata with the big file and then switch over to the smaller file once it catches up. That way it would only have to scan through a small number of lines to find the desired DateTime stamp.

        It might be working at this point if I'm lucky. The market closed before I could test it!

        Comment


          #5
          It works!! I had to switch to using the StreamWriter instead of just File.Append calls. That gave me the chance to make sure the file looked exactly correct before releasing it to be read by the other program.

          In case anyone else needs to try this someday, this is the key code block I used to pull it off:

          // If we have something to write to our file...
          if (WriteQueue.Count > 0)
          {
          OpenCount = 0;
          while (OpenCount < 10)
          {
          try
          {
          // APPEND our file! If this line works, then it's our file until released
          SW = new StreamWriter(path, true);
          while (WriteQueue.Count > 0)
          {
          LineToExport = WriteQueue.Dequeue();
          SW.WriteLine(LineToExport);
          }

          // Now close our file
          SW.Dispose();
          SW = null;

          OpenCount = 10;
          }
          catch (Exception e)
          {
          OpenCount += 1;
          if (OpenCount == 10) Print(e.Message);
          }

          }
          }

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Waxavi, Today, 02:10 AM
          0 responses
          3 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by TradeForge, Today, 02:09 AM
          0 responses
          9 views
          0 likes
          Last Post TradeForge  
          Started by Waxavi, Today, 02:00 AM
          0 responses
          2 views
          0 likes
          Last Post Waxavi
          by Waxavi
           
          Started by elirion, Today, 01:36 AM
          0 responses
          4 views
          0 likes
          Last Post elirion
          by elirion
           
          Started by gentlebenthebear, Today, 01:30 AM
          0 responses
          4 views
          0 likes
          Last Post gentlebenthebear  
          Working...
          X