2024-12-18 15:45:35 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
2024-12-18 15:47:49 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-12-18 15:45:35 +00:00
|
|
|
#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;
|
2024-12-18 15:47:49 +00:00
|
|
|
|
|
|
|
LongTermTrendPeriod = 200;
|
2024-12-18 15:45:35 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
2024-12-18 15:47:49 +00:00
|
|
|
LongTermTrend = SMA(LongTermTrendPeriod);
|
2024-12-18 15:45:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }
|
|
|
|
}
|
2024-12-18 15:47:49 +00:00
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "Long-Term Trend Period", GroupName = "Engulfing Bot", Order = 1)]
|
|
|
|
public int LongTermTrendPeriod { get; set; }
|
2024-12-18 15:45:35 +00:00
|
|
|
}
|
|
|
|
}
|