namespace NinjaTrader.NinjaScript.Strategies
{
public class SimpleMovingAverage : Strategy
{
private SMA SMA1;
private SMA SMA2;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Simple Moving Average-Price Crossover";
Name = "SimpleMovingAverage";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 250;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
MovingAverage = 200;
} else if (State == State.Configure){
}else if (State == State.DataLoaded){
SMA1 = SMA(Convert.ToInt32(MovingAverage));
SMA1.Plots[0].Brush = Brushes.Blue;
AddChartIndicator(SMA1);
SMA2 = SMA(Convert.ToInt32(MovingAverage));
}}
protected override void OnBarUpdate()
{ if (CurrentBars[0] < 1) return;
// Set 1
if (CrossAbove(SMA1, Close, 1))
{ EnterLong(Convert.ToInt32(DefaultQuantity), @"Long"); }
// Set 2
if (CrossBelow(SMA2, Close, 1))
{ ExitLong(Convert.ToInt32(DefaultQuantity), @"Exit", @"Long"); }
Comment