ninjatrader/strategies/WilliamsPercentRBot.cs

107 lines
3.6 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class WilliamsPercentRBot : Strategy
{
private WilliamsR williamsR;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Williams %R Bot";
Name = "Williams %R 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;
BarsRequiredToTrade = 20;
IsInstantiatedOnEachOptimizationIteration = true;
WilliamsPercentRPeriod = 2;
EntryThreshold = -90;
ExitThreshold = -30;
UseNewHighExit = false;
UseTrendFilter = false;
TrendFilterPeriod = 20;
}
else if (State == State.Configure)
{
AddDataSeries(BarsPeriodType.Day, 1);
}
else if (State == State.DataLoaded)
{
williamsR = WilliamsR(WilliamsPercentRPeriod);
longTermTrend = SMA(BarsArray[1], TrendFilterPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < WilliamsPercentRPeriod)
return;
if (UseTrendFilter && CurrentBars[1] < TrendFilterPeriod)
return;
if (williamsR[0] < EntryThreshold)
{
if (UseTrendFilter && Close[0] < longTermTrend[0])
return;
EnterLong();
}
else if (williamsR[0] > ExitThreshold || (UseNewHighExit && Close[0] > High[1]))
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Williams %R Period", GroupName = "Williams %R Bot", Order = 1)]
public int WilliamsPercentRPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Entry Threshold", GroupName = "Williams %R Bot", Order = 2)]
public int EntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Exit Threshold", GroupName = "Williams %R Bot", Order = 3)]
public int ExitThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Use New High Exit", GroupName = "Williams %R Bot", Order = 4)]
public bool UseNewHighExit { get; set; }
[NinjaScriptProperty]
[Display(Name = "Use Trend Filter", GroupName = "Williams %R Bot", Order = 5)]
public bool UseTrendFilter { get; set; }
[NinjaScriptProperty]
[Display(Name = "Trend Filter Period", GroupName = "Williams %R Bot", Order = 6)]
public int TrendFilterPeriod { get; set; }
}
}