I’m reading a file that contains a list of high volatility market events so I can set dark zones (non trading times) for my strategy. I can read and write files in the relative directory where NinjaTrader seems to want to put them.
The sample streamwriter and streamreader files use this line to define a path
path = NinjaTrader.Core.Globals.UserDataDir + “MyTestFile.txt”;
Where NinjaTrader.Core.Globals.UserDataDir translates to:
"C:\Program Files\NinjaTrader 8\bin\Users\MyWindows UserNameHere\ + “MyTestFile.txt”;
However, I prefer to read and write to a folder not associated with the program directory, something like
path = “C:\Users\MyWindows UserNameHere\Downloads\MarketEvents\MyTestFile.txt”;
So it turns out that that forward slashes are required in that absolute path, like this:
path = “C:/Users/MyWindows UserNameHere/Downloads/MarketEvents/MyTestFile.txt”;
And I’ll state the obvious. The default “read only” designation assigned to the folder of your choice needs to be unchecked by right clicking on the folder in the windows explorer and choosing “properties.”
Perhaps this tip will be useful to the community.
Spotter
Here is the code if you wish to copy/paste the useful parts:
//
// Copyright (C) 2016, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Xml.Serialization;
#endregion
// Add this to your declarations to use StreamWriter
using System.IO;
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
public class StreamwriterAbsolutePathExample : Indicator
{
private string path;
private StreamWriter sw; // a variable for the StreamWriter that will be used
private bool ReadWriteComplete;
protected override void OnStateChange()
{
if(State == State.SetDefaults)
{
Calculate = Calculate.OnBarClose;
Name = "StreamwriterAbsolutePathExample";
path = "C:/Users/YourWindowsUserNameHere/Downloads/MarketEvents/MyTestFile.txt"; // Define the Path to our test file
// you must create a folder called MarketEvents in your download folder and remove the "read only" property.
// Open a new Ninjascript output file before you apply this indicator so you can view the print statements.
ReadWriteComplete = false;
}
// Necessary to call in order to clean up resources used by the StreamWriter object
else if(State == State.Terminated)
{
if (sw != null)
{
sw.Close();
sw.Dispose();
sw = null;
}
}
}
protected override void OnBarUpdate()
{
if (ReadWriteComplete == false) //Because I want to read the file *one time* at the beginning of the strategy, instead of every bar
{
try
{
// uncomment this you want the stream writer to delete your existing file and make a new one
// when these lines are commented out, streamwriter will append lines to your existing file.
// if (File.Exists(path))
// {
// File.Delete(path);
// }
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
Print("Writing: This");
sw.WriteLine("is some text");
Print("Writing: is some text");
sw.WriteLine("to test");
Print("Writing: to test");
sw.WriteLine("Reading");
Print("Writing: Reading");
}
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
Print(sr.ReadLine());
}
}
}
catch (Exception e)
{
Print("The process failed: {0}" + e.ToString());
}
ReadWriteComplete = true;
}
}
}
}
#region NinjaScript generated code. …