NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


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 01-04-2010, 04:35 AM   #1
germano
Member
 
Join Date: Mar 2009
Posts: 56
Thanks: 0
Thanked 0 times in 0 posts
Default Two moving averages indicator.

Hi… and Happy New Year to all.

I have a question.

I want to build an indicator composed of two equals moving averages, namely: SMA1 (28) [0] and SMA2 (28) [0]. But the second moving average has moved forward by 7 periods.
My code move forward both moving averages, not just one.
Someone can tell me where is the error?

Thank you for your help.
Germano

The code:

publicclass TwoEqualsSMA : Indicator
{
#region Variables
privateint period = 28; // Default setting for Period
privateint displace = 7; // Default setting for Displace
#endregion

protectedoverridevoid Initialize()
{
// 1° Moving average
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Value_SMA1"));

// 2° Moving average
Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "Value_SMA2"));
Displacement = displace;

CalculateOnBarClose =
true;
Overlay =
true;
}

protectedoverridevoid OnBarUpdate()
{
Value_SMA1.Set(SMA(period )[
0]);
Value_SMA2.Set(SMA(period )[
0]);
}
germano is offline  
Reply With Quote
Old 01-04-2010, 05:04 AM   #2
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,404
Thanks: 252
Thanked 972 times in 955 posts
Default

Germano, the displacement would be used for both plots in your indicator.

Try for example this for more control on it -

Code:
 
protected override void Initialize()
{
// 1° Moving average 
Add(new Plot(Color.FromKnownColor(KnownColor.Red), PlotStyle.Line, "Value_SMA1"));
// 2° Moving average 
Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Line, "Value_SMA2"));
//Displacement = displace; 
CalculateOnBarClose = true;
Overlay = true;
}
protected override void OnBarUpdate()
{
if (CurrentBar < displace) return;
 
Value_SMA1.Set(SMA(period )[0]);
Value_SMA2.Set(SMA(period )[displace]);
}
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-04-2010, 05:25 AM   #3
germano
Member
 
Join Date: Mar 2009
Posts: 56
Thanks: 0
Thanked 0 times in 0 posts
Default

Bertrand, thank you for your help.
Now, the indicator works perfectly.

Thank you again.
Germano
germano 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
Syntax for two moving averages germano Indicator Development 4 11-04-2009 07:38 AM
Displaced moving averages ggloor Charting 6 10-11-2009 02:11 AM
Three moving averages germano Indicator Development 2 07-24-2009 12:39 PM
Displaced moving averages T2020 Charting 2 01-20-2009 09:13 AM
Nested moving averages? Burga1 Indicator Development 2 11-16-2007 08:51 AM


All times are GMT -6. The time now is 11:52 AM.