NexusFi: Find Your Edge


Home Menu

 





Help with Streamwriter


Discussion in NinjaTrader

Updated
    1. trending_up 2,036 views
    2. thumb_up 3 thanks given
    3. group 3 followers
    1. forum 5 posts
    2. attach_file 2 attachments




 
Search this Thread

Help with Streamwriter

  #1 (permalink)
 nmk85roll 
chicago, il
 
Experience: Beginner
Platform: ninja Trader
Posts: 28 since Mar 2010
Thanks Given: 3
Thanks Received: 8

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!

Attached Files
Elite Membership required to download: test.txt
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
ZombieSqueeze
Platforms and Indicators
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Quant vue
Trading Reviews and Vendors
 
  #2 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


nmk85roll View Post
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!

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.

Reply With Quote
  #3 (permalink)
 nmk85roll 
chicago, il
 
Experience: Beginner
Platform: ninja Trader
Posts: 28 since Mar 2010
Thanks Given: 3
Thanks Received: 8



userque View Post
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];

SampleStreamWriter indicator = new SampleStreamWriter();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
Indicators.Add(indicator);
indicator.SetUp();

SampleStreamWriter[] tmp = new SampleStreamWriter[cacheSampleStreamWriter == null ? 1 : cacheSampleStreamWriter.Length + 1];
if (cacheSampleStreamWriter != null)
cacheSampleStreamWriter.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheSampleStreamWriter = tmp;
return indicator;
}
}
}
}


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"

Started this thread Reply With Quote
  #4 (permalink)
userque
Chicago IL
 
Posts: 180 since Apr 2016
Thanks Given: 573
Thanks Received: 130


nmk85roll View Post
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];

SampleStreamWriter indicator = new SampleStreamWriter();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
Indicators.Add(indicator);
indicator.SetUp();

SampleStreamWriter[] tmp = new SampleStreamWriter[cacheSampleStreamWriter == null ? 1 : cacheSampleStreamWriter.Length + 1];
if (cacheSampleStreamWriter != null)
cacheSampleStreamWriter.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheSampleStreamWriter = tmp;
return indicator;
}
}
}
}


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"

I've highlighted the 'new' keywords below.

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.


Reply With Quote
Thanked by:
  #5 (permalink)
 nmk85roll 
chicago, il
 
Experience: Beginner
Platform: ninja Trader
Posts: 28 since Mar 2010
Thanks Given: 3
Thanks Received: 8

Figured it out, thanks!!!!

Started this thread Reply With Quote
Thanked by:
  #6 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
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.

Example here.

Reply With Quote
Thanked by:




Last Updated on October 29, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts