#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.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class Nikita : Strategy
{
#region Variables
// Wizard generated variables
private int movAvgLength = 200; // Default setting for MyInput0
// 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;
Add(SMA(Close, movAvgLength));
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
SMA MovAvg = SMA(movAvgLength);
RSI rsi = RSI(2, 1);
if(Close[0] > MovAvg[0] && rsi[0] <= 10)
{
EnterLong(DefaultQuantity, "Long");
}
if(Close[0] < MovAvg[0] && rsi[0] >= 90)
{
EnterShort(DefaultQuantity, "Short");
}
if (rsi[0] > 50)
ExitLong();
else if (rsi[0] < 50)
ExitShort();
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MovAvgLength
{
get { return movAvgLength; }
set { movAvgLength = Math.Max(1, value); }
}
#endregion
}
This is how I coded it:
#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.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// RSI(2) w/ MA.
/// </summary>
[Description("RSI(2) w/ MA. ")]
public class RSI2MA : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// 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()
{
//Enter long
if(CrossAbove(Close, SMA(200), 1) && RSI(2, 0)[0] <= 10);
{
EnterLong(DefaultQuantity, "Long");
}
//Enter short
if(CrossBelow(Close, SMA(200), 1) && RSI(2, 0)[0] >= 90);
{
EnterShort(DefaultQuantity, "Short");
}
//Exit long
if(RSI(2, 0)[0] > 50);
{
ExitLong("Exit Long", "");
}
//Exit short
if(RSI(2, 0)[0] < 50);
{
ExitShort("Exit Short ", "");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
Comment