Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Streamwriter, filename, unique for each chart, instrument, bartype, value & pane

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Streamwriter, filename, unique for each chart, instrument, bartype, value & pane

    Here is a little code gist I made for building a filename for use with streamwriter.
    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...

    Code:
    // 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
    Last edited by balltrader; 02-17-2021, 09:49 AM. Reason: bar type affects bar value https://ninjatrader.com/support/helpGuides/nt8/barsperiod.htm

    #2
    Hello balltrader,

    Thank you for providing this sample for the community. This would likely be the way that we would suggest as well rather than using a GUID. That would so that the file name is more descriptive in case you need to use it in any way or locate it.




    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    656 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    371 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    109 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    574 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    579 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X