ninjatrader/strategies/knife-catcher/KnifeCatcherBot.cs

76 lines
2.4 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class KnifeCatcherBot : Strategy
{
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Strategy taken from chapter 3 of Short Term Trading Strategies That Work";
Name = "Knife Catcher Bot";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
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;
BarsRequiredToTrade = 20;
IsInstantiatedOnEachOptimizationIteration = true;
DownDayStreak = 3;
BarsToExit = 5;
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < 3)
return;
bool isFallingKnife = true;
for (int i = 0; i < DownDayStreak; i++)
{
if (Close[i] >= Close[i + 1])
{
isFallingKnife = false;
break;
}
}
if (isFallingKnife)
EnterLong();
if (BarsSinceEntryExecution() >= BarsToExit)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Down Day Streak", GroupName = "Knife Catcher Bot", Order = 1)]
public int DownDayStreak { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Bars to Exit", GroupName = "Knife Catcher Bot", Order = 2)]
public int BarsToExit { get; set; }
}
}