Function to write to a text file  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
Tartalognion
Posts: 23
Joined: 17 Mar 2010
Has thanked: 20 times
Been thanked: 2 times

Function to write to a text file

Postby Tartalognion » 25 Aug 2018

Hello everybody,

As the number of lines is limited in the output window, I've decided to create a function which creates a text file in order to store all the values I need to check.

So far, my function works but only for BarNumber 1 :arrow: :shock:

Here is my function:
using System;
using System.Drawing;
using System.Linq;
using System.IO; //Contient les Classes StreamReader & StreamWriter

namespace PowerLanguage
{
namespace Function
{
public sealed class _WriteTxtFile : FunctionSimple<System.String>
{
public _WriteTxtFile(CStudyControl _master) : base(_master) { }
public _WriteTxtFile(CStudyControl _master, int _ds) : base(_master, _ds) { }

public string fileName { get; set; }

public string strField { get; set; }

private StreamWriter writer;


protected override void Create()
{
// create variable objects and function objects
Output.WriteLine("Create");
}

protected override void StartCalc()
{
//Création du fichier de débuggage :
writer = new StreamWriter(fileName,true);
using (writer)
{
writer.WriteLine(strField);
}
Output.WriteLine("StartCalc");
}

protected override System.String CalcBar()
{
writer = new StreamWriter(fileName,true);
using (writer)
{
writer.WriteLine(strField);
}
Output.WriteLine("CalcBar");

return default(System.String);
}
}
}
}
====================
And here is an excerpt of the strategy code which uses this function:
(Sorry but my code is too long)
// Variable declaration:
private Function._WriteTxtFile writeToDebugger;

//procedures:
protected override void Create()
{
strFileName = @"C:\Programmation\Multicharts.NET\TxtFile\TestFunction.txt";
writeToDebugger = new Function._WriteTxtFile(this);
}
protected override void StartCalc()
{
writeToDebugger.fileName = strFileName;
writeToDebugger.strField = "BarNumber : " + Bars.CurrentBar + " Cours : " + Bars.Close[0];
}

protected override void CalcBar()
{
writeToDebugger.fileName = strFileName;
writeToDebugger.strField = "BarNumber : " + Bars.CurrentBar + " Cours : " + Bars.Close[0];
}
==============
As I told you, the file is created, but with only one line...corresponding to BarNumber 1.

I also noticed that only "Create" and "StartCalc" string words are written into the Ouput Editor.
No "CalcBar" string word written...
As if the CalcBar() procedure of my function was never executed...

From what I understand, the Create() procedure of the function is executed at the same time as the Create() procedure of the Strategy, the StartCalc() procedure of the function at the same time as the StartCalc() procedure of the Strategy and so on with CalcBar()... Am I right?

Thank you for your help.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Function to write to a text file  [SOLVED]

Postby Henry MultiСharts » 30 Aug 2018

Hello Tartalognion,

Since you are using a Simple function, you need to call it in your code each time you need to have it calculated. The methods are called sequentially in the order of creating the study objects, not all at the same time.

Tartalognion
Posts: 23
Joined: 17 Mar 2010
Has thanked: 20 times
Been thanked: 2 times

Re: Function to write to a text file

Postby Tartalognion » 30 Aug 2018

Hello Henry,
I rewrote my function as a series one and it now works perfectly. Thanks!


Return to “MultiCharts .NET”