ninjatrader/strategies/internal-bar-strength-band/InternalBarStrengthBandBot.cs

84 lines
2.9 KiB
C#
Raw Normal View History

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class InternalBarStrengthBandBot : Strategy
{
private InternalBarStrength ibs;
private MAX highestHigh;
private SMA highMinusLowAverage;
private SMA sma;
private Series<double> highMinusLow;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Long only bot which bases trades on internal bar strength and lower band condition";
Name = "Internal Bar Strength Band 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;
LowerBandMultiplier = 2.5;
IBSEntryThreshold = 0.3;
}
else if (State == State.DataLoaded)
{
highMinusLow = new Series<double>(this);
ibs = InternalBarStrength();
highestHigh = MAX(High, 10);
highMinusLowAverage = SMA(highMinusLow, 25);
sma = SMA(200); // Regime filter.
}
}
protected override void OnBarUpdate()
{
highMinusLow[0] = High[0] - Low[0];
if (CurrentBar < BarsRequiredToTrade)
return;
double lowerBand = highestHigh[0] - (LowerBandMultiplier * highMinusLowAverage[0]);
if (Close[0] < lowerBand && ibs[0] < IBSEntryThreshold && Close[0] > sma[0])
EnterLong();
if (Close[0] > High[1])
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Lower Band Multiplier", GroupName = "Internal Bar Strength Band Bot", Order = 1)]
public double LowerBandMultiplier { get; set; }
[NinjaScriptProperty]
[Display(Name = "IBS Entry Threshold", GroupName = "Internal Bar Strength Band Bot", Order = 2)]
public double IBSEntryThreshold { get; set; }
}
}