ninjatrader/strategies/rsi-divergence/RSIDivergenceBot.cs

85 lines
2.9 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class RSIDivergenceBot : Strategy
{
private RSI rsi;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Simple strategy based on RSI divergence";
Name = "RSI Divergence 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;
RSIPeriod = 14;
RSISmoothing = 3;
DivergencePeriod = 5;
ExitThreshold = 70;
}
else if (State == State.DataLoaded)
{
rsi = RSI(RSIPeriod, RSISmoothing);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < DivergencePeriod)
return;
if (rsi[0] > rsi[DivergencePeriod] && Close[0] < Close[DivergencePeriod])
EnterLong();
if (Position.MarketPosition == MarketPosition.Long && rsi[0] > ExitThreshold)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Period", GroupName = "RSI Divergence Bot", Order = 1)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Smoothing", GroupName = "RSI Divergence Bot", Order = 2)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Divergence Period", GroupName = "RSI Divergence Bot", Order = 3)]
public int DivergencePeriod { get; set; }
[NinjaScriptProperty]
[Range(0.1, 100)]
[Display(Name = "Exit Threshold", GroupName = "RSI Divergence Bot", Order = 4)]
public double ExitThreshold { get; set; }
}
}