PDA

View Full Version : Need Code Help for MA


tshirtdeal
06-15-2010, 12:06 PM
I am looking to make the MA scan for stocks off the narrowest range indicator,

I need to add a line of code to the indicator so that if the bar is a NR inside the MA can create a alert or filter for it.

So when the indicator plots on the chart or is "true" it has a value which I can then create the condition for scanning in MA.

does anyone know the line of code I need to add? I would assume something like "if true value = 1" or something like this or

"The indicator could plot 1 when conditions are true and 0 otherwise. You could then filter for when this custom indicator is 1"

if anyone knows the code I need to add I would appreciate it..

TIA
Reed

NinjaTrader_Tim
06-15-2010, 02:03 PM
Hi tshirtdeal,

You may want to consider editing the indicator itself and adding a plot within the same condition that colors the background.

Unfortunately, I cannot write this for you (copying this verbatim will cause errors without the proper plot setup in Initialize() and Properties ), but perhaps something like...

if (nrbar==0)
{

BackColor = nr7color;
PlotName.Set(1);
}
else
{
PlotName.Set(0);
}

tshirtdeal
06-15-2010, 02:27 PM
it's actually not the NR7 indicator (which paints the entire background to signal the candle) it is the narrowest range of X period, it actually paints the candle


it also has the option to make "inside candle" = true which is what I need MA to be able and scan my list for...

Here is the basics main code, if you have any ideas or how to do it I would really appreciate it, I know it has to be simple... but then again maybe not for all I know..

P.S - and "inside bar" would be defined as one in which the high and low are within the previous bar, I would actually like to change the code so that the bars range could be less than or equal to as well...

----------------------------------------

if (CurrentBar < period)
return;

if (insidePattern && High[0] >= High[1] || Low[0] <= Low[1])
return;

for (int index = 0; index < period; index++)
{
if (Range()[0] > Range()[index])
break;

if (index == period - 1)
BarColor = patternColor;

NinjaTrader_Tim
06-15-2010, 02:32 PM
Hi tshirtdeal,

If you are not a programmer yourself, any of our NinjaScript consultants could assist.
More info at - http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm

If you want more info on getting started with NinjaScript I can supply that info as well.

tshirtdeal
06-15-2010, 02:35 PM
okay, can you point me in the direction of creating and defining plots?

Thanks,

NinjaTrader_Tim
06-15-2010, 03:02 PM
Hi tshirtdeal,

This tutorial will guide you through creating a plot in the wizard (doing this will setup much of the process for you), and then show you how to set that plot to different values.

Tutorial at - http://www.ninjatrader-support.com/HelpGuideV6/helpguide.html?Overview23

tshirtdeal
06-15-2010, 03:49 PM
Hi Tim, thanks, can you do me one last favor and look at this code and see what I am doing wrong? What should my plot values be for the "inside bar?
--------------------

{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Bar, "Plot0"));
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = false;
upcolor = Color.Yellow;
insidebar = new DataSeries(this);
soundon = 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.
Plot0.Set(insidebar[1]);

if (CurrentBar < 1) return;

range1 = Math.Abs(Close[0]-Open[0]);
range2 = Math.Abs(Close[1]-Open[1]);


// Check for InsideBar pattern

if(High[0] <= High[1] && Low[0] >= Low[1])

{
BarColor = upcolor;
insidebar.Set(1);
Plot0.Set(1);
if(soundon)
{
if (!Historical)
PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\Alert4.wav");
}
}
else
insidebar.Set(0);
Plot0.Set(1);
}

#region Properties
[CategoryAttribute("Plots")]
[DescriptionAttribute("Inside BAr Color")]
[XmlIgnoreAttribute()]
[NinjaTrader.Gui.Design.DisplayNameAttribute("Inside BAr Color")]
public Color UpColor

{
get { return upcolor; }
set { upcolor = value; }
}

[BrowsableAttribute(false)]
public string UpColorSerialize


{
get { return NinjaTrader.Gui.Design.SerializableColor.ToString( upcolor); }
set { upcolor = NinjaTrader.Gui.Design.SerializableColor.FromStrin g(value); }
}

[Browsable(false)]
[XmlIgnore()]
public DataSeries Insidebar
{
get
{
Update();
return insidebar;

}
}

[NinjaTrader.Gui.Design.DisplayNameAttribute("Sound on?")]
[DescriptionAttribute("Sound on?")]
[CategoryAttribute("Parameters")]
public bool SoundOn

{
get { return soundon ; }
set { soundon = value; }
}
#endregion
}
}

NinjaTrader_RyanM
06-15-2010, 04:23 PM
Hello tshirtdeal,

The market analyzer is looking for only Plot components of indicators. You can't select a non-plotted data series from the market analyzer.

Below is just the part of your code that will assign a value of 1 when conditions are true and a zero otherwise. It assigns this to the only plot: Plot0. If you don't have a need for a second plot, then you can remove all the InsideBar references.


if(High[0] <= High[1] && Low[0] >= Low[1])
{
Plot0.Set(1);
}
 
else
{
Plot0.Set(0);
}

tshirtdeal
06-15-2010, 05:01 PM
I dont't know seems easy enough but it won't work, just error after erro,

can't I just create an inside bar formula with :

(High[0] <= High[1] && Low[0] >= Low[1])

or:

Plot0.Set(High[0] <= High[1] && Low[0] >= Low[1] ? 1 : 0);

i just need this simple thing to plot when a bar is inside (or equal) to the previous range for MA,

TIA

tshirtdeal
06-15-2010, 05:20 PM
can you explain to me what is wrong with this code? It creates the indicator and puts the indicator on my chart but does not work (it shows no bars for the inside candles, )

{
Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Bar, "Plot0"));
CalculateOnBarClose = true;
Overlay = false;
PriceTypeSupported = 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.
Plot0.Set(High[0] <= High[1] && Low[0] >= Low[1] ? 1 : 0);
}

tshirtdeal
06-15-2010, 06:58 PM
I just had a guy who does very great coding for big people make the code, it works for him but not for me...

very frustrating .... is it a problem with my NT platform?

what do I need to do?

NinjaTrader_Tim
06-16-2010, 06:02 AM
Hi tshirtdeal,

Are you getting any errors in the Log tab of the control center when applying this indicator?

tshirtdeal
06-16-2010, 07:38 AM
Hi Tim,

no errors, everything I have tried complies without errors and my friend who is a great NT script guy create something and sent me a grid pic of it working as well...

the indicators will not plot on the chart and I cannot get the MA to show a 1 if inside bar is true or 0 if not and be able to run condistions which is all I need, this really should be very simple, I am not a pro script guy but have made many of these simple type scripts, don't get it...

thanks for any help

NinjaTrader_Tim
06-16-2010, 07:45 AM
Hi tshirtdeal,

Can you export this using "File>Utilities>Export" so that I can briefly test it on my end.

tshirtdeal
06-16-2010, 08:12 AM
12152


thanks for the help!

tshirtdeal
06-16-2010, 08:32 AM
It is showing me the current price in the MA window, I need it show me a "1" for inside bar or a 0 for not an inside bar, his screen shot does that... don't know why it won't for me...

NinjaTrader_Tim
06-16-2010, 08:56 AM
Hi tshirtdeal,

Try restarting NinjaTrader and re-applying the indicator. You will see an error in the log tab.
The following link describes the steps to resolve this error - http://www.ninjatrader.com/support/forum/showthread.php?t=3170

tshirtdeal
06-16-2010, 09:42 AM
learn something new everyday I guess, seems to be working, i got to learn this stuff better, that was so frustrating for me:

anyway, this is what I did and seems to work... make sense to you?

if(High[0] <= High[Math.Min(CurrentBar, 1)] && Low[0] >= Low[Math.Min(CurrentBar, 1)])

{
Plot0.Set(1);
}
 
else
{
Plot0.Set(0);
}

NinjaTrader_Tim
06-16-2010, 10:05 AM
Hi tshirtdeal,

Yes, that should work for you, alternatively you can do...

if (CurrentBar < 1)
return;

if(High[0] <= High[1] && Low[0] >= Low[1])

tshirtdeal
06-16-2010, 11:00 AM
Thanks for all the help Tim, was going borderline crazy on that!