ninjatrader/strategies/vol-panics/VolPanicsBot.cs

83 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-10-04 13:06:52 +00:00
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class VolPanicsBot : Strategy
{
private SMA movingAverage;
private RSI rsi;
private double entryPrice;
2024-10-04 13:06:52 +00:00
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Vol Panics Bot";
Description = @"Taken from chapter 5 of Buy the Fear, Sell the Greed (2018) by Larry Connors";
2024-10-04 13:06:52 +00:00
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 2;
2024-10-04 13:06:52 +00:00
MovingAveragePeriod = 5;
RSIPeriod = 4;
RSISmoothing = 1;
EntryThreshold = 70;
EnableAggressiveEntries = true;
2024-10-04 13:06:52 +00:00
}
else if (State == State.DataLoaded)
{
movingAverage = SMA(MovingAveragePeriod);
rsi = RSI(RSIPeriod, RSISmoothing);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(MovingAveragePeriod, RSIPeriod))
return;
if (Position.MarketPosition == MarketPosition.Flat && Close[0] > movingAverage[0] && rsi[0] > EntryThreshold)
{
EnterShort();
entryPrice = Close[0];
}
if (Position.MarketPosition == MarketPosition.Short && EnableAggressiveEntries && Close[0] > entryPrice)
2024-10-04 13:06:52 +00:00
EnterShort();
if (Position.MarketPosition == MarketPosition.Short && Close[0] < movingAverage[0])
ExitShort();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Moving Average Period", GroupName = "Vol Panics Bot", Order = 1)]
public int MovingAveragePeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "Vol Panics Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Smoothing", GroupName = "Vol Panics Bot", Order = 3)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Entry Threshold", GroupName = "Vol Panics Bot", Order = 4)]
public int EntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Enable Aggressive Entries", GroupName = "Vol Panics Bot", Order = 5)]
public bool EnableAggressiveEntries { get; set; }
2024-10-04 13:06:52 +00:00
}
}