Initial commit of 2B indicator
This commit is contained in:
parent
0909bee23c
commit
84e719c68b
249
indicators/TwoB.cs
Normal file
249
indicators/TwoB.cs
Normal file
@ -0,0 +1,249 @@
|
||||
#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; }
|
||||
}
|
||||
|
||||
public class TwoB : Indicator
|
||||
{
|
||||
[XmlIgnore]
|
||||
public Series<TwoBPattern> CompletedPattern;
|
||||
|
||||
private List<TwoBPattern> twoBs;
|
||||
|
||||
private PivotPoints pivots;
|
||||
|
||||
private double pivotHigh;
|
||||
private double pivotLow;
|
||||
|
||||
private double mostRecentPivotHigh;
|
||||
private double mostRecentPivotLow;
|
||||
|
||||
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);
|
||||
|
||||
// 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)
|
||||
pivotHigh = pivot.Level;
|
||||
else if (pivot.IsPivotLow)
|
||||
pivotLow = pivot.Level;
|
||||
}
|
||||
|
||||
if (Open[0] < pivotHigh && High[0] > pivotHigh && mostRecentPivotHigh != pivotHigh)
|
||||
{
|
||||
mostRecentPivotHigh = pivotHigh;
|
||||
|
||||
twoBDownLevel = Low[0];
|
||||
twoBDownStartBarIndex = CurrentBar;
|
||||
}
|
||||
else if (Open[0] > pivotLow && Low[0] < pivotLow && mostRecentPivotLow != pivotLow)
|
||||
{
|
||||
mostRecentPivotLow = pivotLow;
|
||||
|
||||
twoBUpLevel = High[0];
|
||||
twoBUpStartBarIndex = CurrentBar;
|
||||
}
|
||||
|
||||
if (Close[0] > twoBUpLevel)
|
||||
{
|
||||
twoBUpEndBarIndex = CurrentBar;
|
||||
TwoBPattern twoBUp = new TwoBPattern
|
||||
{
|
||||
Level = twoBUpLevel,
|
||||
Is2BUp = true,
|
||||
StartBarIndex = twoBUpStartBarIndex,
|
||||
EndBarIndex = twoBUpEndBarIndex
|
||||
};
|
||||
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,
|
||||
EndBarIndex = twoBDownEndBarIndex
|
||||
};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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; }
|
||||
|
||||
[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
|
Loading…
Reference in New Issue
Block a user