View Full Version : Indicator for price above/ below MA?
monkey_boy
09-03-2009, 04:26 AM
Hi,
just started using NT today and trying to get the market analyzer set up.
Is there any way to add a column which will tell me at a glance if the current price is 'Above' or 'Below' a defined moving average?
I have the moving average columns set up, and would be happy to use a colour condition in this column in reference to the price (e.g. MA cell is green if below price, red if above), but it seems I can only set the colour conditions against a value (which obviously isn't practical versus a moving price), as opposed to the price.
Thanks in advance.
NinjaTrader_Jason
09-03-2009, 04:44 AM
Hello monkey_boy,
Unfortunately such a column is not supported and your findings are correct; color conditions can only use a numeric value.
However, you could create a custom indicator that uses the condition you mention. Subsequently load it in the Market Analyzer using an Indicator column.
More information regarding custom creating indicators can be found at the link below.
http://www.ninjatrader-support.com/HelpGuideV6/Overview18.html
monkey_boy
09-03-2009, 05:01 AM
Jason,
thanks for your quick reply.
I had already looked at the custom indicator route, but don't find the tutorial explanation particularly helpful at all, as I have only the most basic programming skills.
Is there any way to get the wizard to produce what I am looking for without having to start messing about with the code?
Thanks
NinjaTrader_Jason
09-03-2009, 05:12 AM
Yes, you should be able to setup such a condition using the Strategy Wizard. Please see the link below for information.
http://www.ninjatrader-support.com/HelpGuideV6/ConditionBuilder-IndicatorToValueComparisons.html
Instead of the numeric value, you would use the Close price.
fosch
08-16-2010, 09:38 PM
Jason,
But if monkey_boy wanted to add an indicator to the Market Analyzer, this would not work, right? That is since you are suggesting creating a strategy instead of an indicator. Just trying to understand your solution. Thanks.
Joe
NinjaTrader_Jason
08-17-2010, 05:43 AM
Hello Joe,
Unfortunately the NinjaScript wizard to create indicators does not allow you to create such a condition. That is why I referred to a strategy - the NinjaScript wizard upon creating a strategy does allow you to create such a condition. He could subsequently run the strategy in a chart.
You can create indicators that use such a condition as well, however you would need to create it outside the wizard.
http://www.ninjatrader-support.com/HelpGuideV6/Overview18.html
fosch
08-17-2010, 12:18 PM
Jason,
I thought about using something like this in the indicator code:
if CrossAbove(High[0], SMA(50), 1) to determine if a crossover occurs.
But how would I pass back the result? I thought I could just use Value.Set = true but this gives me an error?
NinjaTrader_Bertrand
08-17-2010, 12:40 PM
fosch, the Value.Set would expect a non boolean value, so try for example 1 for a CrossAbove and -1 for a CrossBelow.
fosch
08-17-2010, 01:01 PM
Thanks Bertrand. I tried your suggestion with the following code:
protected override void OnBarUpdate()
{
Value.Set = -1;
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CrossAbove(High[0], SMA(50), 1) || CrossBelow(Low[0], SMA(50), 1))
Value.Set = 1;
else
Value.Set = -1;
}
But the result when I tried to compile was the following error:
"Cannot assign to 'Set' becuase it is a 'method group'"
I could not find a description of the error, so I wanted to ask you what my error appears to be. Thanks.
Joe
NinjaTrader_Bertrand
08-17-2010, 01:11 PM
You would need to assign a value differently when using the DataSeries.Set() method, my change below should compile well -
{
Value.Set(-1);
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CrossAbove(High[0], SMA(50), 1) || CrossBelow(Low[0], SMA(50), 1))
Value.Set(1);
else
Value.Set(-1);
}
fosch
08-17-2010, 02:08 PM
Thanks again Bertrand. That worked. Looks like my syntax was incorrect.
A related question... when I add the following code:
if ((High[0] >= SMA(20)) || (Low[0] <= SMA(20)))
I get the following error:
"Operator '<=' cannot be applied to operands of type 'double'"
Any suggestions on how I can make this comparison if that is the case? I'm trying not to use the CrossOver() function, as I'm really interested in seeing if price "touches" a moving average.
Thanks.
NinjaTrader_Austin
08-17-2010, 02:15 PM
if ((High[0] >= SMA(20)) || (Low[0] <= SMA(20)))
Please try:
if ((High[0] >= SMA(20)[0]) || (Low[0] <= SMA(20)[0]))
// do something
The SMA() function returns a DataSeries, which is a big, long list of double values, but it looks like you are only interested in the most recent value, which can be accessed with the [0] indexer.
I wrote a reference page last summer for when to use the square brackets [], which can be found here (http://www.ninjatrader.com/support/forum/showthread.php?t=19346).
fosch
08-17-2010, 02:46 PM
Thanks Austin. I will try to put all of this together later tonight.
mystiq
05-15-2012, 09:40 AM
You would need to assign a value differently when using the DataSeries.Set() method, my change below should compile well -
{
Value.Set(-1);
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
if (CrossAbove(High[0], SMA(50), 1) || CrossBelow(Low[0], SMA(50), 1))
Value.Set(1);
else
Value.Set(-1);
}
please..can you save this as an indicator..(or instruct me on how to).so i can use..it is perfect for what i need to do on market analyzer...
NinjaTrader_Bertrand
05-15-2012, 09:46 AM
mystiq, I'll ask our NinjaScript trainee to work on a conversion / demo script for you as a courtesy.
NinjaTrader_JC
05-15-2012, 01:50 PM
Hello mystiq,
Here is that Indicator based on the code snippet from Bertrand. Happy trading.
To Import
1. Download the attached file to your desktop
2. From the Control Center window select the menu File > Utilities > Import NinjaScript
3. Select the downloaded file