PDA

View Full Version : Hull MA of Macd Historigram


Learning1
07-04-2007, 11:09 AM
I am new to NT (from Tradestation) and am in the midst of learning NinjaScript. For a 1st project I am altering a MACD to add a Hull MA to the Diff Historigram of Macd.

I have copied Macd, Changed names, added a period for the HMA, and added a plot line to the Initialize() section of the indicator as well as the HMA logic to the OnBarUpdate() section. This is where I've run into a challenge.

I called the plot "DiffAv" in the initialize section, but when I compile the code, I get the error message "The name 'DiffAv' does not exist in the current context (I got the same error message on the indicator SMAVolume tutorial). Despite a few hours puzzling this through and trying different approaches, I keep coming back to the same error. It likely is something simple I am just missing, but any help would be greatly appreciated. I have attached the indicator for reference.

Guy

NinjaTrader_Ray
07-04-2007, 11:19 AM
Welcome to NinjaTrader and the world of OO programming.

The simple solution is to add the following code under the "Properties" region.


/// <summary>
/// </summary>
[Browsable(false)]
[XmlIgnore()]
public DataSeries DiffAv
{
get { return Values[3]; }
}


A simple explanation is as follows:

- When you Add() a plot in Initialize(), you are adding an object that defines the visualization of the plot, NOT the actual data that is plotted.

- By adding a plot via the Add() method, NT internally adds a dataseries object held in the Values[] collection that holds the actual data to be plotted.

- A property is then created that (code you were missing above) that accesses the DataSeries object

I also see another problem with your code:

DiffAv.Set(HMA(Diff, HMAPeriod));

Should be:

DiffAv.Set(HMA(Diff, HMAPeriod)[0]);

What you are doing is setting the actual indicator itself vs setting the value that the indicator holds at the current bar.

Learning1
07-04-2007, 11:38 AM
Thanks a ton Ray!

This makes sense now and thanks also for the welcome. I added the code you suggested and and the indicator compiled. That said, I ran into another snag ... When I added it to a chart, NinjaTrader stopped working and crashed. I restarted it and tried again only to have the same result. If there is an error in the indicator code, it seems odd that it would crash the program. Any suggestions?

GUY

NinjaTrader_Ray
07-04-2007, 11:50 AM
The problem is:

/// <summary>
/// </summary>
[Description("Number of bars for HMADiff")]
[Category("Parameters")]
public int HMAPeriod
{
get { return HMAPeriod; }
set { HMAPeriod = Math.Max(1, value); }
}

The "setter" is recursive meaning, its an endless loop which eventually will cause NT to crash. For clarification -

- NinjaScript indicators run compiled not interpreted, the same as the rest of the application source code
- The advantage is that its runs as fast as possible and does not go through an interpreter
- The downside is the application will inherit bugs such as endless loops
- Normally we can trap endless loops at run time but .NET 2.0 no longer allows this
- We have added for the next release an internal check that scans for recursive properties and warns the user of them

To fix:

Under variables change HMAperiod to hmaPeriod (note the case)
In the setter, change to hmaPeriod = Math.Max() ....

NinjaTrader_Ray
07-04-2007, 11:51 AM
Forgot...

Also change the "getter" to

return hmaPeriod;

Learning1
07-04-2007, 12:11 PM
I made the changes to the hmaPeriod and also found an old reference to HMADiff which I later changed to DiffAv. I got a new error when I changed the setter to hmaPeriod = Math.Max ();}, but if I leave it at Math.Max (value, 1);} the indicator now compiles and runs on the chart without crashing. However ... it does not display anything on the chart ... Where do you think I went wrong?

Learning1
07-04-2007, 12:33 PM
I figured it out. I had earlier removed the macd plot. When I added it back, everything worked. Thanks tons for the help!!! The learning curve feels a bit steep, but I appreciate your assistance over some of the speed bumps!

GUY

NinjaTrader_Ray
07-04-2007, 12:39 PM
NinjaScript is C#, a full blown programming language. The benefit is that you have a lot more flexibility to express your ideas than you do with a proprietary language. Not to mention, your effort is portable since C# is a real language that can be used outside of NinjaTrader.

AO76
07-07-2007, 12:20 PM
Why Value[3] and not Value[0]? Shouldn't we be passing the most recent Value?

I'm trying to create my own indicators and it has been extremely frustrating with Ninja !! Something that would take me a couple minutes with Ensign is taking me hours.

edit: I figued it out. Thanks.

whitmark
07-07-2007, 01:12 PM
In this context, Values[3] (not Value[3]) refers to a dataseries object in the Values collection that is referenced by a slot integer and not some value three bars ago. Each time you add a plot, you will want to make sure that it will have a corresponding entry in the "Properties" section with a get/return Values statement that references a unique slot integer. For example, if my custom indicator has four plots it should also have a separate get/return statements referencing Values[0], Values[1], Values[2], Values[3]. Of course, if you use the indicator wizard, this is done automatically for you. Open up any multi-plot standard indicator like KeltnerChannel and you'll see what I mean. HTH

Regards,

Whitmark

P.S. Nice to see an Ensign user working with NinjaTrader

AO76
07-07-2007, 01:14 PM
Thanks Whitmark -- I just figured it out after staring at it for far too long !!!

Thanks.

whitmark
07-07-2007, 01:25 PM
No problem . . . it can be tricky at first to know when those [ ]'s refer to collection indices (e.g., Values[0], Plots[0], Lines[0]) and dataseries n bars ago (e.g., Close[0], Typical[0], Volume[0]). You might find this thread useful going forward in your development endeavors.

http://www.ninjatrader-support.com/vb/showthread.php?t=2297

Regards,

Whitmark