ninjatrader/indicators/TwoB.cs

275 lines
9.0 KiB
C#
Raw Normal View History

2024-08-08 20:24:06 +00:00
#region Using declarations
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Windows.Media;
using System.Xml.Serialization;
#endregion
namespace NinjaTrader.NinjaScript.Indicators
{
public class TwoBPattern
{
public double Level { get; set; }
public bool Is2BUp { get; set; }
public bool Is2BDown { get; set; }
public int StartBarIndex { get; set; }
public int EndBarIndex { get; set; }
2024-08-10 01:25:40 +00:00
public double PivotLevel { get; set; }
public int PivotBarIndex { get; set; }
2024-08-08 20:24:06 +00:00
}
public class TwoB : Indicator
{
[XmlIgnore]
public Series<TwoBPattern> CompletedPattern;
private List<TwoBPattern> twoBs;
private PivotPoints pivots;
private double pivotHigh;
private double pivotLow;
2024-08-10 01:25:40 +00:00
private int pivotHighBarIndex;
private int pivotLowBarIndex;
2024-08-08 20:24:06 +00:00
private double mostRecentPivotHigh;
private double mostRecentPivotLow;
2024-08-10 01:25:40 +00:00
private int mostRecentPivotHighBarIndex;
private int mostRecentPivotLowBarIndex;
2024-08-08 20:24:06 +00:00
private double twoBUpLevel;
private int twoBUpStartBarIndex;
private int twoBUpEndBarIndex;
private double twoBDownLevel;
private int twoBDownStartBarIndex;
private int twoBDownEndBarIndex;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Based on the 2B reversal pattern, credit to Victor Sperandeo";
Name = "2B";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
PivotPeriod = 10;
TwoBUpStroke = new Stroke(Brushes.LimeGreen, 3);
TwoBDownStroke = new Stroke(Brushes.Red, 3);
2024-08-10 01:25:40 +00:00
PivotStroke = new Stroke(Brushes.Yellow, 3);
2024-08-08 20:24:06 +00:00
// For use during backtesting in order to limit the number of patterns processed.
MaxPatterns = 0;
}
else if (State == State.DataLoaded)
{
CompletedPattern = new Series<TwoBPattern>(this, MaximumBarsLookBack.Infinite);
twoBs = new List<TwoBPattern>();
pivots = PivotPoints(PivotPeriod, false);
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < PivotPeriod)
return;
PivotPoint pivot = pivots.Pivot[0];
if (pivot != null)
{
if (pivot.IsPivotHigh)
2024-08-10 01:25:40 +00:00
{
2024-08-08 20:24:06 +00:00
pivotHigh = pivot.Level;
2024-08-10 01:25:40 +00:00
pivotHighBarIndex = pivot.StartBarIndex;
}
2024-08-08 20:24:06 +00:00
else if (pivot.IsPivotLow)
2024-08-10 01:25:40 +00:00
{
2024-08-08 20:24:06 +00:00
pivotLow = pivot.Level;
2024-08-10 01:25:40 +00:00
pivotLowBarIndex = pivot.StartBarIndex;
}
2024-08-08 20:24:06 +00:00
}
if (Open[0] < pivotHigh && High[0] > pivotHigh && mostRecentPivotHigh != pivotHigh)
{
mostRecentPivotHigh = pivotHigh;
2024-08-10 01:25:40 +00:00
mostRecentPivotHighBarIndex = pivotHighBarIndex;
2024-08-08 20:24:06 +00:00
twoBDownLevel = Low[0];
twoBDownStartBarIndex = CurrentBar;
}
else if (Open[0] > pivotLow && Low[0] < pivotLow && mostRecentPivotLow != pivotLow)
{
mostRecentPivotLow = pivotLow;
2024-08-10 01:25:40 +00:00
mostRecentPivotLowBarIndex = pivotLowBarIndex;
2024-08-08 20:24:06 +00:00
twoBUpLevel = High[0];
twoBUpStartBarIndex = CurrentBar;
}
if (Close[0] > twoBUpLevel)
{
twoBUpEndBarIndex = CurrentBar;
TwoBPattern twoBUp = new TwoBPattern
{
Level = twoBUpLevel,
Is2BUp = true,
StartBarIndex = twoBUpStartBarIndex,
2024-08-10 01:25:40 +00:00
EndBarIndex = twoBUpEndBarIndex,
PivotBarIndex = mostRecentPivotLowBarIndex,
PivotLevel = mostRecentPivotLow,
2024-08-08 20:24:06 +00:00
};
twoBs.Add(twoBUp);
CompletedPattern[0] = twoBUp;
twoBUpLevel = double.MaxValue;
}
else if (Close[0] < twoBDownLevel)
{
twoBDownEndBarIndex = CurrentBar;
TwoBPattern twoBDown = new TwoBPattern
{
Level = twoBDownLevel,
Is2BDown = true,
StartBarIndex = twoBDownStartBarIndex,
2024-08-10 01:25:40 +00:00
EndBarIndex = twoBDownEndBarIndex,
PivotBarIndex = mostRecentPivotHighBarIndex,
PivotLevel = mostRecentPivotHigh,
2024-08-08 20:24:06 +00:00
};
twoBs.Add(twoBDown);
CompletedPattern[0] = twoBDown;
twoBDownLevel = double.MinValue;
}
if (MaxPatterns > 0 && twoBs.Count > MaxPatterns)
twoBs.RemoveAt(0);
}
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
base.OnRender(chartControl, chartScale);
foreach (var twoB in twoBs.ToArray())
{
int startX = chartControl.GetXByBarIndex(ChartBars, twoB.StartBarIndex);
int endX = twoB.EndBarIndex == 0 ? ChartPanel.X + ChartPanel.W : chartControl.GetXByBarIndex(ChartBars, twoB.EndBarIndex);
var stroke = twoB.Is2BUp ? TwoBUpStroke : TwoBDownStroke;
DrawHorizontalLine(startX, endX, chartScale.GetYByValue(twoB.Level), stroke);
2024-08-10 01:25:40 +00:00
int pivotX = chartControl.GetXByBarIndex(ChartBars, twoB.PivotBarIndex);
DrawHorizontalLine(pivotX, startX, chartScale.GetYByValue(twoB.PivotLevel), PivotStroke);
2024-08-08 20:24:06 +00:00
}
}
private void DrawHorizontalLine(int startX, int endX, int y, Stroke stroke)
{
SharpDX.Direct2D1.Brush dxBrush = stroke.Brush.ToDxBrush(RenderTarget);
RenderTarget.DrawLine(
new SharpDX.Vector2(startX, y),
new SharpDX.Vector2(endX, y),
dxBrush, stroke.Width, stroke.StrokeStyle
);
dxBrush.Dispose();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Range(1, int .MaxValue)]
[Display(Name = "Pivot Period", GroupName = "2B", Order = 1)]
public int PivotPeriod
{ get; set; }
[Display(Name = "2B Up Stroke", GroupName = "2B", Order = 2)]
public Stroke TwoBUpStroke { get; set; }
[Display(Name = "2B Down Stroke", GroupName = "2B", Order = 3)]
public Stroke TwoBDownStroke { get; set; }
2024-08-10 01:25:40 +00:00
[Display(Name = "Pivot Stroke", GroupName = "2B", Order = 4)]
public Stroke PivotStroke { get; set; }
2024-08-08 20:24:06 +00:00
[Browsable(false)]
public int MaxPatterns { get; set; }
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TwoB[] cacheTwoB;
public TwoB TwoB(int pivotPeriod)
{
return TwoB(Input, pivotPeriod);
}
public TwoB TwoB(ISeries<double> input, int pivotPeriod)
{
if (cacheTwoB != null)
for (int idx = 0; idx < cacheTwoB.Length; idx++)
if (cacheTwoB[idx] != null && cacheTwoB[idx].PivotPeriod == pivotPeriod && cacheTwoB[idx].EqualsInput(input))
return cacheTwoB[idx];
return CacheIndicator<TwoB>(new TwoB(){ PivotPeriod = pivotPeriod }, input, ref cacheTwoB);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TwoB TwoB(int pivotPeriod)
{
return indicator.TwoB(Input, pivotPeriod);
}
public Indicators.TwoB TwoB(ISeries<double> input , int pivotPeriod)
{
return indicator.TwoB(input, pivotPeriod);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TwoB TwoB(int pivotPeriod)
{
return indicator.TwoB(Input, pivotPeriod);
}
public Indicators.TwoB TwoB(ISeries<double> input , int pivotPeriod)
{
return indicator.TwoB(input, pivotPeriod);
}
}
}
#endregion