ninjatrader/strategies/intraday-weakness/IntradayWeaknessBot.cs

94 lines
3.3 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class IntradayWeaknessBot : Strategy
{
private MIN lows;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Taken from chapter 8 of Short Term Trading Strategies That Work";
Name = "Intraday Weakness 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;
LongTermTrendPeriod = 200;
LowPeriod = 10;
DaysToExit = 5;
UseLimitOrders = false;
LimitOrderPercent = 1;
}
else if (State == State.DataLoaded)
{
lows = MIN(Close, LowPeriod);
longTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LowPeriod, LongTermTrendPeriod))
return;
if (BarsSinceEntryExecution() == DaysToExit)
ExitLong();
if (Close[0] == lows[0] && Close[0] > longTermTrend[0])
{
if (UseLimitOrders)
EnterLongLimit(Close[0] * (1 - (LimitOrderPercent / 100)));
else
EnterLong();
}
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Long Term Trend Period", GroupName = "Intraday Weakness Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Low Period", GroupName = "Intraday Weakness Bot", Order = 2)]
public int LowPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Days to Exit", GroupName = "Intraday Weakness Bot", Order = 3)]
public int DaysToExit { get; set; }
[NinjaScriptProperty]
[Display(Name = "Use Limit Orders", GroupName = "Intraday Weakness Bot", Order = 4)]
public bool UseLimitOrders { get; set; }
[NinjaScriptProperty]
[Display(Name = "Limit Order Percent", GroupName = "Intraday Weakness Bot", Order = 5)]
public double LimitOrderPercent { get; set; }
}
}