ninjatrader/strategies/TripleMovingAverageBot.cs

97 lines
3.3 KiB
C#
Raw Permalink Normal View History

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class TripleMovingAverageBot : Strategy
{
private EMA fastMA;
private EMA mediumMA;
private EMA slowMA;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"";
Name = "Triple Moving Average Bot";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
IsInstantiatedOnEachOptimizationIteration = true;
FastMAPeriod = 10;
MediumMAPeriod = 20;
SlowMAPeriod = 100;
LongTermTrendPeriod = 20;
}
else if (State == State.Configure)
{
AddDataSeries(BarsPeriodType.Day, 1);
}
else if (State == State.DataLoaded)
{
fastMA = EMA(FastMAPeriod);
mediumMA = EMA(MediumMAPeriod);
slowMA = EMA(SlowMAPeriod);
longTermTrend = SMA(BarsArray[1], LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBar < Math.Max(FastMAPeriod, Math.Max(MediumMAPeriod, SlowMAPeriod)))
return;
if (CurrentBars[1] < 0)
return;
if (fastMA[0] > mediumMA[0] && mediumMA[0] > slowMA[0] && Close[0] > longTermTrend[0])
EnterLong();
else if (fastMA[0] < mediumMA[0] && mediumMA[0] < slowMA[0])
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Fast MA Period", GroupName = "Triple Moving Average Bot", Order = 1)]
public int FastMAPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Medium MA Period", GroupName = "Triple Moving Average Bot", Order = 2)]
public int MediumMAPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Slow MA Period", GroupName = "Triple Moving Average Bot", Order = 3)]
public int SlowMAPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Long Term Trend Period", GroupName = "Triple Moving Average Bot", Order = 4)]
public int LongTermTrendPeriod { get; set; }
}
}