Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
First time using streamwriter here so forgive me if this is elementary. I'm basically just writing a strategy that can output data to a file. I know we have the export data strategy in the file sharing section, but indicators without parameters don't work (i.e. VWAP, OBV,etc) so I was just trying to hardcode it. For example, in the code attached I'm merely trying to output the avaPivots to a file when the strategy is run. When I do, I keep getting the error:
**NT** Error on calling 'OnBarUpdate' method for strategy 'Export/30587ddcccd4e3a0f238d380a903a': Object reference not set to an instance of an object.
It's gotta be something from the sw.WriteLine() that I'm not doing properly. Thanks in advance for your help!
Can you help answer these questions from other members on NexusFi?
So far, I haven't had a need to use streamwriter etc. So I can only offer so much assistance without doing some research.
But, I don't see where you've newed up a streamwriter object. AFAIK, you have to create a streamwriter object in order to use it. I believe you'll need to use the keyword 'new' somewhere.
Thanks Userque. Here is the attached sample code I was working from the NT7 support examples. The only sight of "new" is in the "namespace NinjaTrader.Indicator"
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private SampleStreamWriter[] cacheSampleStreamWriter = null;
private static SampleStreamWriter checkSampleStreamWriter = new SampleStreamWriter();
/// <summary>
/// A quick sample demonstrating StreamWriter properties.
/// </summary>
/// <returns></returns>
public SampleStreamWriter SampleStreamWriter()
{
return SampleStreamWriter(Input);
}
/// <summary>
/// A quick sample demonstrating StreamWriter properties.
/// </summary>
/// <returns></returns>
public SampleStreamWriter SampleStreamWriter(Data.IDataSeries input)
{
if (cacheSampleStreamWriter != null)
for (int idx = 0; idx < cacheSampleStreamWriter.Length; idx++)
if (cacheSampleStreamWriter[idx].EqualsInput(input))
return cacheSampleStreamWriter[idx];
lock (checkSampleStreamWriter)
{
if (cacheSampleStreamWriter != null)
for (int idx = 0; idx < cacheSampleStreamWriter.Length; idx++)
if (cacheSampleStreamWriter[idx].EqualsInput(input))
return cacheSampleStreamWriter[idx];
Syntax is not my strong point... do I need to include something like this in my code even though its a strategy? I always ignored this code because of the comments "Do not change or remove"
The 'do not change' warning is only referring to one statement regarding the namespace.
Streamwriter is a C# thing. So you can google 'ninjatrader streamwriter' initially; and then 'c# streamwriter' to get even more information on it's use.
Streamwriter code should be similar for both indicators and strategies.
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166
Streamwriter needs to be disposed. I'd recommend wrapping everything in a using statement which will handle the disposal for you. Depending on how often you call your writer, this can be a performance penality but it will keep you safe and prevent memory leaks.