I have this simple indicator which exports OHLC data to a text file.
Code: Select all
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
using System.IO;
namespace PowerLanguage.Indicator{
[UpdateOnEveryTickAttribute (false)]
public class ohlcExporter : IndicatorObject {
public ohlcExporter(object _ctx):base(_ctx){}
private IPlotObject plot1;
private StreamWriter outputStream;
private String outputTextFile;
string tickerName;
protected override void Create() {
plot1 = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
}
protected override void StartCalc() {
tickerName = Bars.Info.ASymbolInfo2.SymbolRoot;
outputTextFile = "c:\\" + tickerName + ".txt";
outputStream = new StreamWriter(outputTextFile, false);
outputStream.WriteLine("Instrument, Date, Open, High, Low, Close, Volume");
}
protected override void CalcBar(){
plot1.Set(Bars.Close[0]);
outputStream.Write(tickerName + ", ");
outputStream.Write(Bars.Time[0].Date.ToString("yyyyMMdd, "));
outputStream.Write(Bars.Open[0].ToString() + ", ");
outputStream.Write(Bars.High[0].ToString() + ", ");
outputStream.Write(Bars.Low[0].ToString() + ", ");
outputStream.Write(Bars.Close[0].ToString() + ", ");
outputStream.WriteLine(Bars.Volume[0].ToString());
if (Bars.LastBarOnChart) {
outputStream.Close();
}
}
}
}
I've tried changing the [UpdateOnEveryTickAttribute (false)] property to true. I've tried pasting all the .Write lines into the if (Bars.LastBarOnChart) condition. But no luck in either case.
Any guidance is appreciated.
(NOT SO IMPORTANT: The script seems to work fine (except for missing the last bar) on charts. But if I paste it into a market scanner, I sporadically get errors like cannot write to a closed stream, or file is being used by another process. This issue is not very pressing since I only have 2 tickers to look at for now, so I can manage with the indicator applied to a chart for the time being.)