I had 1 indicator script loaded on multiple chart windows, and even on multiple instances on the same chart using various parameter settings.
But when streamwriter tried writing to the same file from the other instances of the indicator, it complained about file locks already in use by another instance..
I wasn't exactly sure if there was a way to get a GUID of the instance, but then I thought, I could build up the filename based on instrument, bar type, bar type value and pane panel number.
You may find it interesting, or suggest better ways to accomplish this...
// problem is if you have this indicator on multiple charts and or multiple instances on same chart
// you'd need a unique filename for each streamwriter object, or else you will get errors in console log about file is already in use
// let's build a unique filename for each instance
string _indicator_instance = "";
// this should get the indicator name
_indicator_instance += "." + this.Name;
// this should get the inst
_indicator_instance += "." + Bars.Instrument.MasterInstrument.ToString();
// this should get the type of the bars, ie if minute bars, or seconds, etc
_indicator_instance += "." + Bars.BarsPeriod.BarsPeriodTypeName;
// this should get the periodicty of the bars, ie if minute bars, 5 etc
/// EDIT: for regular bar types, ie minutes, seconds, use .Value .i.e 15 seconds
_indicator_instance += "." + Bars.BarsPeriod.Value;
/// EDIT: for other bar types, reference : https://ninjatrader.com/support/helpGuides/nt8/barsperiod.htm
//_indicator_instance += "." + Bars.BarsPeriod.BaseBarsPeriodValue;
// this should get the panel where the insicator is located on the chart?
_indicator_instance += ".Panel_" + this.Panel;
// also need a timestamp when this file was created, so reloading the chart doesn't overwrite previous files
_indicator_instance += DateTime.Now.ToString(".yyyy_MM_dd__HH_mm_ss");
// now try it appended to filename.
StWrFilePath = Environment.GetFolderPath(Environment.SpecialFolde r.MyDocuments) + @"\Z_NT_DebugData" + _indicator_instance + ".txt";
StWr = new StreamWriter(StWrFilePath, false); // instantiate SW object and use that file, do not append, overwrite

Comment