![]() |
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
Im new to programming, and struggle to figure out the coding for this verry simple value.
I want to get a indicator reading of the spread between a fast and a slow moving average. (FastSMA - SlowSMA). I realize this probably is a verry simple code, therfore I hope someone can point me in the right direction?
|
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
Hello Europetrader,
Thanks for your post and I am happy to assist you. Please use this code Code:
double diff = SMA(13)[0] - SMA(34)[0];
Joydeep M.
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
Thank you. I managed to render the code without getting any errors, but when I try it out in the chart it is blank (and the log doesnt show any errors)?
I used the wizard to generate the SMA variables as I want to be able to change the averages easy.I have copied the code below. Appreciate it if you can take a look? ![]() Code:
#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 TAZ : Indicator
{
#region Variables
// Wizard generated variables
private int fastMA = 10; // Default setting for FastMA
private int slowMA = 40; // Default setting for SlowMA
// 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.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[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 Plot0
{
get { return Values[0]; }
}
[Description("ma10")]
[GridCategory("Parameters")]
public int FastMA
{
get { return fastMA; }
set { fastMA = Math.Max(1, value); }
}
[Description("ma40")]
[GridCategory("Parameters")]
public int SlowMA
{
get { return slowMA; }
set { slowMA = 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 TAZ[] cacheTAZ = null;
private static TAZ checkTAZ = new TAZ();
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public TAZ TAZ(int fastMA, int slowMA)
{
return TAZ(Input, fastMA, slowMA);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public TAZ TAZ(Data.IDataSeries input, int fastMA, int slowMA)
{
if (cacheTAZ != null)
for (int idx = 0; idx < cacheTAZ.Length; idx++)
if (cacheTAZ[idx].FastMA == fastMA && cacheTAZ[idx].SlowMA == slowMA && cacheTAZ[idx].EqualsInput(input))
return cacheTAZ[idx];
lock (checkTAZ)
{
checkTAZ.FastMA = fastMA;
fastMA = checkTAZ.FastMA;
checkTAZ.SlowMA = slowMA;
slowMA = checkTAZ.SlowMA;
if (cacheTAZ != null)
for (int idx = 0; idx < cacheTAZ.Length; idx++)
if (cacheTAZ[idx].FastMA == fastMA && cacheTAZ[idx].SlowMA == slowMA && cacheTAZ[idx].EqualsInput(input))
return cacheTAZ[idx];
TAZ indicator = new TAZ();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.FastMA = fastMA;
indicator.SlowMA = slowMA;
Indicators.Add(indicator);
indicator.SetUp();
TAZ[] tmp = new TAZ[cacheTAZ == null ? 1 : cacheTAZ.Length + 1];
if (cacheTAZ != null)
cacheTAZ.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheTAZ = 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.TAZ TAZ(int fastMA, int slowMA)
{
return _indicator.TAZ(Input, fastMA, slowMA);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.TAZ TAZ(Data.IDataSeries input, int fastMA, int slowMA)
{
return _indicator.TAZ(input, fastMA, slowMA);
}
}
}
// 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.TAZ TAZ(int fastMA, int slowMA)
{
return _indicator.TAZ(Input, fastMA, slowMA);
}
/// <summary>
/// Enter the description of your new custom indicator here
/// </summary>
/// <returns></returns>
public Indicator.TAZ TAZ(Data.IDataSeries input, int fastMA, int slowMA)
{
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.TAZ(input, fastMA, slowMA);
}
}
}
#endregion
|
|
|
|
|
|
#4 |
|
Senior Member
|
This is your current code that handles the OnBarUpdate() event:
Code:
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[0]);
}
Code:
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[0]);
Plot0.Set(diff);
}
I assure you that the piecemeal approach of trial-and-error, and asking specific questions and getting the resultant narrowly tailored answer, such as the one I give here, is much the slower process of trying to understand what is going on.
|
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks alot for the help. I will
|
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
|
Hello Eutopetrader,
Please go through the Plot class to get more idea on how NinjaTrader draws a data series http://www.ninjatrader.com/support/h...plot_class.htm @ koganam - Thanks for the debug.
Joydeep M.
NinjaTrader Customer Service |
|
|
|
![]() |
| Tags |
| sma, spread |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Plot SMA's | tacticaltrader | Indicator Development | 3 | 09-19-2011 11:48 PM |
| spread | werido | Suggestions And Feedback | 1 | 10-29-2009 09:12 AM |
| Spread | rastotrader | Automated Trading | 5 | 10-21-2009 12:30 PM |
| spread trading | SuperDriveGuy | SuperDOM and other Order Entry Windows | 4 | 07-31-2009 07:36 AM |
| The Spread | beastment | Strategy Analyzer | 2 | 01-28-2009 07:25 AM |