#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("read the file")]
public class MyCustomStrategy : Strategy
{
// Variables
// Order
private double long_entry = 0;
// This sets the path in which the text file will be created.
private string path = "d:\ninjatest\order.txt";
// Creates a StreamReader object
private System.IO.StreamReader sr;
// This method is used to configure the strategy and is called once before any strategy method is called.
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (Historical)
return;
// Checks to see if the file exists
else if (File.Exists(path))
{
try
{
// Sets the file the StreamReader will read from
sr = new System.IO.StreamReader(path);
string line;
// Read lines and calculate the current day's OHLC from the file. While loop will go through the whole file.
while ((line = sr.ReadLine()) != null)
{
string [] split = line.Split(new Char [] {' '});
if ( line.startsWith("long_entry") )
long_entry = double.Parse( split[1] );
}
//Print( "entry: " + long_entry + " target: " + long_target + " stop: " + long_stop );
}
catch (Exception e)
{
// Outputs the error to the log
Log("You cannot write and read from the same file at the same time. Please remove SampleStreamWriter.", NinjaTrader.Cbi.LogLevel.Error);
Print(e.ToString());
throw;
}
}
// If file does not exist, let the user know
else
Print("File does not exist.");
EnterLongLimit(DefaultQuantity, long_entry, "");
} //end OnBarUpdate
// Necessary to call in order to clean up resources used by the StreamReader object
protected override void OnTermination()
{
// Disposes resources used by the StreamReader
if (sr != null)
{
sr.Dispose();
sr = null;
}
}
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
reading out a file
Collapse
X
-
reading out a file
i want nt to use information out of a file. i read the reading example and copy the relevant part to the strategy but it wont do as intended. whats wrong?
Code:Tags: None
-
Hello lifetime,
Welcome to the forum and I am happy to assist you.
I suspect the path is not correct. Instead of writing the code as
please append it asCode:private string path = "d:\ninjatest\order.txt";
Please refer to this sample code which demonstrates how to read from a text file.Code:private string path = @"d:\ninjatest\order.txt";
JoydeepNinjaTrader Customer Service
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, Yesterday, 10:06 AM
|
0 responses
17 views
0 likes
|
Last Post
by argusthome
Yesterday, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
16 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
14 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
9 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
36 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment