This example is for writing/reading some integer data of a strategy into a text file:
#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);
}
}

Comment