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

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

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by r68cervera, Today, 05:29 AM
        0 responses
        2 views
        0 likes
        Last Post r68cervera  
        Started by geddyisodin, Today, 05:20 AM
        0 responses
        3 views
        0 likes
        Last Post geddyisodin  
        Started by JonesJoker, 04-22-2024, 12:23 PM
        6 responses
        34 views
        0 likes
        Last Post JonesJoker  
        Started by GussJ, 03-04-2020, 03:11 PM
        12 responses
        3,239 views
        0 likes
        Last Post Leafcutter  
        Started by AveryFlynn, Today, 04:57 AM
        0 responses
        6 views
        0 likes
        Last Post AveryFlynn  
        Working...
        X