ninjatrader/strategies/engulfing/EngulfingBot.cs

60 lines
1.8 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class EngulfingBot : Strategy
{
private SMA LongTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Engulfing Bot";
Description = @"Simple strategy based on the engulfing candle pattern";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
LongTermTrendPeriod = 200;
}
else if (State == State.DataLoaded)
{
LongTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if (High[0] > High[1] && Close[0] < Low[1])
{
if (Close[0] >= LongTermTrend[0])
EnterLong();
else
EnterShort();
}
if (Position.MarketPosition == MarketPosition.Long && (Close[0] > Position.AveragePrice || Close[0] > High[1]))
ExitLong();
else if (Position.MarketPosition == MarketPosition.Short && (Close[0] < Position.AveragePrice || Close[0] < Low[1]))
ExitShort();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Long-Term Trend Period", GroupName = "Engulfing Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
}
}