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

80 lines
2.8 KiB
C#
Raw Normal View History

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
//This namespace holds Strategies in this folder and is required. Do not change it.
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;
}
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] < 0.3 && Close[0] > sma[0])
EnterLong();
if (Close[0] > High[1])// || Close[0] < sma[0])
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; }
}
}