Here is the code
namespace NinjaTrader.NinjaScript.Indicators
{
public class SampleStreamWriter : Indicator
{
private string path;
private StreamWriter sw; // a variable for the StreamWriter that will be used
private bool UserDrawing;
private string RectangleTag;
private double RectangleStartPrice;
private double RectangleEndPrice;
private DateTime RectangleStartTime;
private DateTime RectangleEndTime;
protected override void OnStateChange()
{
if(State == State.SetDefaults)
{
Calculate = Calculate.OnBarClose;
Name = "Sample stream writer";
path = NinjaTrader.Core.Globals.UserDataDir + "MyTestFile.txt"; // Define the Path to our test file
}
// 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()
{
// Loops through the DrawObjects collection via a threadsafe list copy
sw = File.AppendText(path); // Open the path for writing
foreach (DrawingTool draw in DrawObjects.ToList())
{
UserDrawing = draw.IsUserDrawn;
//Print("******************" + UserDrawing);
if (UserDrawing == true && draw is DrawingTools.Rectangle)
{
// Indicates if this is a manually drawn or script generated line
Print("Line Object: " + draw.Tag + " Manually Drawn: " + draw.IsUserDrawn);
DrawingTools.Rectangle temp = draw as DrawingTools.Rectangle;
RectangleTag = draw.Tag;
RectangleStartPrice = temp.StartAnchor.Price;
RectangleEndPrice = temp.EndAnchor.Price;
RectangleStartTime = temp.StartAnchor.Time;
RectangleEndTime = temp.EndAnchor.Time;
sw.WriteLine(RectangleTag + "," + RectangleStartPrice + "," + RectangleEndPrice + "," + RectangleStartTime + "," + RectangleEndTime); // Append a new line to the file
}
}
sw.Close(); // Close the file to allow future calls to access the file again.
}
}
}
Thanks a lot
Comment