Go Back   NinjaTrader Support Forum > NinjaTrader 7 > 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: 96
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_RyanM1
NinjaTrader Customer Service
 
NinjaTrader_RyanM1's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,127
Thanks: 249
Thanked 434 times in 427 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_RyanM1 is offline  
Reply With Quote
Old 05-13-2011, 08:58 AM   #3
sergeysamsonov
Member
 
Join Date: Jul 2010
Posts: 96
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_RyanM1
NinjaTrader Customer Service
 
NinjaTrader_RyanM1's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,127
Thanks: 249
Thanked 434 times in 427 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_RyanM1 is offline  
Reply With Quote
Old 05-13-2011, 09:07 AM   #5
sergeysamsonov
Member
 
Join Date: Jul 2010
Posts: 96
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 Platform Technical Support 3 03-03-2010 04:41 AM
Market Analyzer with custom indicator problem? mgouv411 Platform Technical Support 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 Development 1 11-09-2008 08:36 AM
Printing custom string in Market Analyzer navinpai Platform Technical Support 2 07-30-2008 12:22 AM


All times are GMT -6. The time now is 07:55 PM.

Futures, foreign currency and options 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.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content. Visit the CFTC resources for education regarding the industry and signs of fraud.