153 lines
5.2 KiB
C#
153 lines
5.2 KiB
C#
#region Using declarations
|
|
using NinjaTrader.Gui;
|
|
using NinjaTrader.Gui.Chart;
|
|
using System.ComponentModel;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Windows.Media;
|
|
#endregion
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
{
|
|
[CategoryOrder("%B", 1)]
|
|
[CategoryOrder("Plots", 2)]
|
|
public class PercentB : Indicator
|
|
{
|
|
private Bollinger BollingerBands;
|
|
|
|
protected override void OnStateChange()
|
|
{
|
|
if (State == State.SetDefaults)
|
|
{
|
|
Description = @"Measures price in relation to the upper and lower Bollinger Bands";
|
|
Name = "%B";
|
|
Calculate = Calculate.OnBarClose;
|
|
IsOverlay = false;
|
|
DisplayInDataBox = true;
|
|
DrawOnPricePanel = false;
|
|
DrawHorizontalGridLines = false;
|
|
DrawVerticalGridLines = false;
|
|
PaintPriceMarkers = false;
|
|
ScaleJustification = ScaleJustification.Right;
|
|
IsSuspendedWhileInactive = true;
|
|
Period = 20;
|
|
StandardDeviations = 2.0;
|
|
UpperLevel = 1.0;
|
|
LowerLevel = 0.0;
|
|
UpperLevelStroke = new Stroke(Brushes.LimeGreen, 3);
|
|
LowerLevelStroke = new Stroke(Brushes.Red, 3);
|
|
AddPlot(new Stroke(Brushes.Yellow, 2), PlotStyle.Line, "%B");
|
|
}
|
|
else if (State == State.Configure)
|
|
{
|
|
BollingerBands = Bollinger(StandardDeviations, Period);
|
|
|
|
AddLine(UpperLevelStroke, UpperLevel, "Upper Level");
|
|
AddLine(LowerLevelStroke, LowerLevel, "Lower Level");
|
|
}
|
|
}
|
|
|
|
protected override void OnBarUpdate()
|
|
{
|
|
if (CurrentBar < Period)
|
|
return;
|
|
|
|
double upperBand = BollingerBands.Upper[0];
|
|
double lowerBand = BollingerBands.Lower[0];
|
|
|
|
Value[0] = (Close[0] - lowerBand) / (upperBand - lowerBand);
|
|
}
|
|
|
|
public override string DisplayName
|
|
{
|
|
get { return Name; }
|
|
}
|
|
|
|
#region Properties
|
|
|
|
[Browsable(true)]
|
|
[Range(1, int.MaxValue)]
|
|
[NinjaScriptProperty]
|
|
[Display(Name = "Moving Average Period", GroupName = "%B", Order = 1)]
|
|
public int Period { get; set; }
|
|
|
|
[Browsable(true)]
|
|
[Range(0.1, double.MaxValue)]
|
|
[NinjaScriptProperty]
|
|
[Display(Name = "Standard Deviations", GroupName = "%B", Order = 2)]
|
|
public double StandardDeviations { get; set; }
|
|
|
|
[Range(double.MinValue, double.MaxValue)]
|
|
[Display(Name = "Upper Level", GroupName = "Plots", Order = 2)]
|
|
public double UpperLevel { get; set; }
|
|
|
|
[Display(Name = "Upper Level Stroke", GroupName = "Plots", Order = 3)]
|
|
public Stroke UpperLevelStroke { get; set; }
|
|
|
|
[Range(double.MinValue, double.MaxValue)]
|
|
[Display(Name = "Lower Level", GroupName = "Plots", Order = 4)]
|
|
public double LowerLevel { get; set; }
|
|
|
|
[Display(Name = "Lower Level Stroke", GroupName = "Plots", Order = 5)]
|
|
public Stroke LowerLevelStroke { get; set; }
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
{
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
{
|
|
private PercentB[] cachePercentB;
|
|
public PercentB PercentB(int period, double standardDeviations)
|
|
{
|
|
return PercentB(Input, period, standardDeviations);
|
|
}
|
|
|
|
public PercentB PercentB(ISeries<double> input, int period, double standardDeviations)
|
|
{
|
|
if (cachePercentB != null)
|
|
for (int idx = 0; idx < cachePercentB.Length; idx++)
|
|
if (cachePercentB[idx] != null && cachePercentB[idx].Period == period && cachePercentB[idx].StandardDeviations == standardDeviations && cachePercentB[idx].EqualsInput(input))
|
|
return cachePercentB[idx];
|
|
return CacheIndicator<PercentB>(new PercentB(){ Period = period, StandardDeviations = standardDeviations }, input, ref cachePercentB);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
{
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
{
|
|
public Indicators.PercentB PercentB(int period, double standardDeviations)
|
|
{
|
|
return indicator.PercentB(Input, period, standardDeviations);
|
|
}
|
|
|
|
public Indicators.PercentB PercentB(ISeries<double> input , int period, double standardDeviations)
|
|
{
|
|
return indicator.PercentB(input, period, standardDeviations);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
{
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
{
|
|
public Indicators.PercentB PercentB(int period, double standardDeviations)
|
|
{
|
|
return indicator.PercentB(Input, period, standardDeviations);
|
|
}
|
|
|
|
public Indicators.PercentB PercentB(ISeries<double> input , int period, double standardDeviations)
|
|
{
|
|
return indicator.PercentB(input, period, standardDeviations);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|