Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 05-13-2011, 07:54 AM   #1
sergeysamsonov
Member
 
Join Date: Jul 2010
Posts: 94
Thanks: 4
Thanked 0 times in 0 posts
Default market analyzer and custom indicator

Hello

I have developed simple indicator that has values -1.0, 0.0, and 1.0 (float):

if (golong && direction != 1)
{
Plot_Observer.Set(1.0);
direction = 1;
}
else if (goshort && direction != -1)
{
Plot_Observer.Set(-1.0);
direction = -1;
}
else
{
Plot_Observer.Set(0.0);
direction = 0;

}

If I use this indicator in chart it works just fine (it really very simple), but in market analyzer it shows strange values ranging -100 to 100 (floats). What can be wrong?

here is the entire indicator, with conditions removed:

********************
#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.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class Observer : Indicator
{
#region Variables
// Wizard generated variables
private int period = 1; // Default setting for Period
// User defined variables (add any user defined variables below)
#endregion

private int direction;

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.ForestGreen), PlotStyle.Square, "Plot_Observer"));
Add(new Line(Color.FromKnownColor(KnownColor.SandyBrown), 0, "ZeroLine"));
Overlay = false;
CalculateOnBarClose = false;
direction = 0;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar<50) return;


bool golong = true, goshort = true;

// conditions were removed ...

if (golong && direction != 1)
{
Plot_Observer.Set(1.0);
direction = 1;
}
else if (goshort && direction != -1)
{
Plot_Observer.Set(-1.0);
direction = -1;
}
else
{
Plot_Observer.Set(0.0);
direction = 0;

}
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot_Observer
{
get { return Values[0]; }
}

[Description("")]
[GridCategory("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private Observer[] cacheObserver = null;

private static Observer checkObserver = new Observer();

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Observer Observer(int period)
{
return Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Observer Observer(Data.IDataSeries input, int period)
{
if (cacheObserver != null)
for (int idx = 0; idx < cacheObserver.Length; idx++)
if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
return cacheObserver[idx];

lock (checkObserver)
{
checkObserver.Period = period;
period = checkObserver.Period;

if (cacheObserver != null)
for (int idx = 0; idx < cacheObserver.Length; idx++)
if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
return cacheObserver[idx];

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

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

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.Observer Observer(int period)
{
return _indicator.Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.Observer Observer(Data.IDataSeries input, int period)
{
return _indicator.Observer(input, period);
}
}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.Observer Observer(int period)
{
return _indicator.Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.Observer Observer(Data.IDataSeries input, int period)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

return _indicator.Observer(input, period);
}
}
}
#endregion


*******************
sergeysamsonov is offline  
Reply With Quote
Old 05-13-2011, 08:23 AM   #2
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 428 times in 423 posts
Default

Hello sergeysamsonov,

Do you have enough bars to load for the indicator? You need more than the default 50.

What is direction? I couldn't get your code to compile as you posted it and had to move it to the variables region from Initialize().
NinjaTrader_RyanM is offline  
Reply With Quote
Old 05-13-2011, 08:58 AM   #3
sergeysamsonov
Member
 
Join Date: Jul 2010
Posts: 94
Thanks: 4
Thanked 0 times in 0 posts
Default

I removed direction, it was not important or relevant, same problem, please try to recompile now, I still get the same problem:

#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.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
[Description("Enter the description of your new custom indicator here")]
public class Observer : Indicator
{
#region Variables
// Wizard generated variables
private int period = 1; // Default setting for Period
// User defined variables (add any user defined variables below)
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.ForestGreen), PlotStyle.Square, "Plot_Observer"));
Add(new Line(Color.FromKnownColor(KnownColor.SandyBrown), 0, "ZeroLine"));
Overlay = false;
CalculateOnBarClose = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar<50) return;


bool golong = true, goshort = true;


golong = golong && RSI(14,1)[0]>50;
goshort = goshort && RSI(14,1)[0]<50;



if (golong)
{
Plot_Observer.Set(1.0);
}
else if (goshort)
{
Plot_Observer.Set(-1.0);
}
else
{
Plot_Observer.Set(0.0);
}
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot_Observer
{
get { return Values[0]; }
}

[Description("")]
[GridCategory("Parameters")]
public int Period
{
get { return period; }
set { period = Math.Max(1, value); }
}
#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private Observer[] cacheObserver = null;

private static Observer checkObserver = new Observer();

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Observer Observer(int period)
{
return Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Observer Observer(Data.IDataSeries input, int period)
{
if (cacheObserver != null)
for (int idx = 0; idx < cacheObserver.Length; idx++)
if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
return cacheObserver[idx];

lock (checkObserver)
{
checkObserver.Period = period;
period = checkObserver.Period;

if (cacheObserver != null)
for (int idx = 0; idx < cacheObserver.Length; idx++)
if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
return cacheObserver[idx];

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

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

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.Observer Observer(int period)
{
return _indicator.Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.Observer Observer(Data.IDataSeries input, int period)
{
return _indicator.Observer(input, period);
}
}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.Observer Observer(int period)
{
return _indicator.Observer(Input, period);
}

/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.Observer Observer(Data.IDataSeries input, int period)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

return _indicator.Observer(input, period);
}
}
}
#endregion
sergeysamsonov is offline  
Reply With Quote
Old 05-13-2011, 09:00 AM   #4
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 428 times in 423 posts
Default

I didn't have any issues with the code, although I did have to change the # of bars back in the market analyzer indicator settings. What is yours currently set to?
NinjaTrader_RyanM is offline  
Reply With Quote
Old 05-13-2011, 09:07 AM   #5
sergeysamsonov
Member
 
Join Date: Jul 2010
Posts: 94
Thanks: 4
Thanked 0 times in 0 posts
Default

thank you, changed number of bars and problem got solved
sergeysamsonov is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
True Custom Filter for Market Analyzer tonyh Market Analyzer 3 03-03-2010 04:41 AM
Market Analyzer with custom indicator problem? mgouv411 Market Analyzer 1 07-24-2009 11:34 AM
How Do You Get A Custom Indicator Return Value For Market Analyzer doug_p Indicator Development 1 01-23-2009 07:59 AM
Custom Market Analyzer Column shawnj General Programming 1 11-09-2008 08:36 AM
Printing custom string in Market Analyzer navinpai Market Analyzer 2 07-30-2008 12:22 AM


All times are GMT -6. The time now is 10:39 AM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.