Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to write/read a file with data from strategy

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

    How to write/read a file with data from strategy

    Days ago, I was searching for how to write/read files on disk with some simple data coming from an automated strategy. The first step, I asked in this forum given the adjustments in C# within NT. Here: https://ninjatrader.com/support/foru...r-streamreader. For a non-professional C# programmer, the examples were not only confusing but also didn't apply to my question besides execution errors. So, I decided to search for more info about C# laguange in Microsoft and after many trials, I achieved to put together a simple method that I hope can be useful for anyone.

    This example is for writing/reading some integer data of a strategy into a text file:

    Code:
    #region Variables
            private string path                     = Cbi.Core.UserDataDir.ToString() + "Mydb.txt";     // This the path and the name of your text file
            private string[]                 dbarray = new string[x];                        // dbarray in string to file of x terms.  It's critical that you set exactly what you'll need to use as "x" terms
            private int[]                 dbarrayline = new int[x];                            // Dbarray as int data to use
            private int[]                 via = new int[z];                             // Via datab
            private int                              n = 0;                                    // Internal string db data counter
            // + all variable of your strategy​
    #endregion
    
    protected override void OnBarUpdate()
    {
        if (FirstTickOfBar )   // the moment that I need to read the file when start the strategy
        {
                    // Open the file to read from and convert to int
                    string[] readText = System.IO.File.ReadAllLines(path);
                    n = 0;
                    // Converting text data to int as a temporal array
                    foreach (string s in readText)
                    {
                        dbarrayline[n] = Convert.ToInt32(s);    // In case you needed to convert to some other type, you might use the Parse method in C#
                        n += 1;
                    }
                  // Now you have an array of int ready to use​
        }
        else
        {
                 // Your normal strategy activity.
        }
        // When the day has ended, your strategy is disabled and you need to collect some daily data to write into a file, then:
        protected override void OnTermination()                                
        {
                // Creating file
                // Updating string array  dbarray[n]
                n = 0;
                for( int a = 0; a <= x; a++ )
                {
                      dbarray[n] = via[a].ToString();   //  Here the int data contained in the array via[a] is converted into a string data and stored in the string array dbarray[n]
                       n +=1;                                      // Despite being in a loop, I use different counter for other goals. ​
                }​
                // Writing string array to file .   This was the way that I find with no errors in NT7
                System.IO.File.WriteAllLines(path, dbarray);​
        ​}
    }
    This is a very simple example that I put together. Obviously, it's not the most efficient but it works for me in simple terms.
    Last edited by pstrusi; 10-28-2022, 01:46 AM.

    #2
    Hello pstrusi,

    Thank you for sharing your work!

    There are also working reference sample examples using StreamWriter and StreamReader as well, which can also read / write information to a text file.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea, no problem. I did it cause I had the same problem days ago and for a non-professional C# programmer can be a little hard to understand the formal language of Microsoft and even choose the right method among several that do the same work. As you can see here ( https://ninjatrader.com/support/foru...r-streamreader ), I asked for help in this forum and I was told with the very same examples that you provided, but I got several errors applying them. so I decided to go on my own to Microsoft.
      Last edited by pstrusi; 10-25-2022, 09:40 AM.

      Comment


        #4
        Hello pstrusi,

        Moving forward, if you do get errors be sure to post a screenshot. We are happy to assist.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello pstrusi,
          I'm hoping you can assist in streaming the data into Sqlite or ArticDB, are you able to share tips on how to do this ?.
          I am looking for a way to bypass the manual import of the text file into Sqlite or Excel.
          Many thanks in advance.

          Comment


            #6
            hi Alhakeen,

            I'm sorry, I wish I could, but no only I'm retired now but also I wouldn't know what you need to do.

            No worries, in this forum there are many smart people, just ask it in the right section.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by MatthewLesko, Today, 07:38 AM
            0 responses
            3 views
            0 likes
            Last Post MatthewLesko  
            Started by several, 03-18-2025, 03:53 AM
            13 responses
            198 views
            1 like
            Last Post timko
            by timko
             
            Started by IDumpedCinderella, 03-12-2025, 11:34 PM
            3 responses
            59 views
            0 likes
            Last Post WaleeTheRobot  
            Started by fersanmito, Today, 03:45 AM
            0 responses
            10 views
            0 likes
            Last Post fersanmito  
            Started by kevinenergy, 11-28-2023, 11:04 AM
            3 responses
            78 views
            0 likes
            Last Post NinjaTrader_Jason  
            Working...
            X