PDA

View Full Version : EMA cross over


Marple
08-12-2009, 09:26 AM
Hi,

I am new to ninja trader and now I want to make an indicator (or strategy:confused:) that indicates when the EMA10 crosses EMA5. I have made a strategy but that one does nothing. How can I procede? It would be best if it could change the color of a candle, but some kind of arrow would do as well.

This is what I made and didn't work:
namespace NinjaTrader.Strategy
{
/// <summary>
/// This strategy puts a marker when the EMA 10 crosses the EMA 5
/// </summary>
[Description("This strategy puts a marker when the EMA 10 crosses the EMA 5")]
public class TrendChange : Strategy
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{

CalculateOnBarClose = true;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(EMA(10), EMA(5), 1))
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, 0, Color.Red);
}

// Condition set 2
if (CrossBelow(EMA(10), EMA(5), 1))
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Lime);
}
}

#region Properties
#endregion
}
}

NinjaTrader_Austin
08-12-2009, 09:37 AM
// Condition set 1
if (CrossAbove(EMA(10), EMA(5), 1))
{
DrawArrowDown("My down arrow" + CurrentBar, false, 0, 0, Color.Red);
}

// Condition set 2
if (CrossBelow(EMA(10), EMA(5), 1))
{
DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Lime);
}

The two bolded 0's above in your code tell the strategy to plot the arrows at a price of 0. You can change those to Close[0] or something to plot the arrows where you're more likely to see them.

DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0], Color.Red);

Marple
08-12-2009, 09:50 AM
The two bolded 0's above in your code tell the strategy to plot the arrows at a price of 0. You can change those to Close[0] or something to plot the arrows where you're more likely to see them.

DrawArrowDown("My down arrow" + CurrentBar, false, 0, Close[0], Color.Red);


Thanks for this very fast response. Now it is working.

BTW: Is it also possible to change the color of a candle in stead of drawing an arrow?

eDanny
08-12-2009, 09:57 AM
The simple answer is:

if(CrossAbove(EMA(10), EMA(5), 1))
BarColor = Color.Red;

NinjaTrader_Austin
08-12-2009, 09:57 AM
Yes, the property to change for candle color is BarColor. Here is the link for the help guide entry for BarColor (http://www.ninjatrader-support.com/HelpGuideV6/BarColor.html).


// Sets the bar color to yellow
BarColor = Color.Yellow;
// Sets the bar color to its default color as defined in the chart properties dialog
BarColor = Color.Empty;


// sets bar color to green if the close is greater than the open
if (Close[0] > Open[0])
BarColor = Color.Green;