ninjatrader/strategies/rsi-power-zones/RSIPowerZonesBot.cs

98 lines
3.5 KiB
C#
Raw Normal View History

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class RSIPowerZonesBot : Strategy
{
private RSI rsi;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Taken from chapter 2 of Buy the Fear, Sell the Greed";
Name = "RSI PowerZones Bot";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 2;
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;
RSIPeriod = 4;
RSISmoothing = 1;
FirstEntryThreshold = 30;
SecondEntryThreshold = 25;
ExitThreshold = 55;
LongTermTrendPeriod = 200;
}
else if (State == State.DataLoaded)
{
rsi = RSI(RSIPeriod, RSISmoothing);
longTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, RSIPeriod))
return;
if (Position.MarketPosition == MarketPosition.Flat &&
Close[0] > longTermTrend[0] && rsi[0] < FirstEntryThreshold)
EnterLong();
if (Position.MarketPosition == MarketPosition.Long && rsi[0] < SecondEntryThreshold)
EnterLong();
if (Position.MarketPosition == MarketPosition.Long && rsi[0] > ExitThreshold)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "RSI PowerZones Bot", Order = 1)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Smoothing", GroupName = "RSI PowerZones Bot", Order = 2)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "1st Entry Threshold", GroupName = "RSI PowerZones Bot", Order = 3)]
public int FirstEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "2nd Entry Threshold", GroupName = "RSI PowerZones Bot", Order = 4)]
public int SecondEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Exit Threshold", GroupName = "RSI PowerZones Bot", Order = 5)]
public int ExitThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Long Term Trend Period", GroupName = "RSI PowerZones Bot", Order = 6)]
public int LongTermTrendPeriod { get; set; }
}
}