london, England
Experience: Intermediate
Platform: Ninjatrader
Trading: CL, ES
Posts: 41 since Nov 2011
Thanks Given: 10
Thanks Received: 2
|
I am trying to adjust the code of the gartley indicator to plot XAB .786 Bullish, Bearish retracement triangles showing target 61.8 retracement of A-B. I must have errors in the code still, I would appreciate it if someone can point out whats missing.
/// <summary>
/// detects bullish and bearish and sets value to 1 or -1 or 0 if not found
/// </summary>
[Description("detects bullish and bearish and sets value to 1 or -1 or 0 if not found")]
public class RetracementAtempt1 : Indicator
{
#region Variables
// Wizard generated variables
private double zigZagPercentDeviation = 0.05; // Default setting for ZigZagPercentDeviation
private double tolerancePercent = 5; // Default setting for TolerancePercent
private IntSeries ipatternType;
private DataSeries patternType;
private DataSeries x;
private DataSeries idealProfitTarget;
private Color penColor = Color.Orange;
private int penWidth= 20;
private DashStyle dashStyle=System.Drawing.Drawing2D.DashStyle.Dash;
private int barsBack=250;
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
Overlay = true;
PriceTypeSupported = true;
ipatternType=new IntSeries(this);
patternType=new DataSeries(this);
x=new DataSeries(this);
idealProfitTarget=new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
private double Diagonal(double A,double B){
return Math.Sqrt(A*A+B*B);
}
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
patternType.Set(0);
double B=0;
double A=0;
//double x=0;
double AB=0;
double XA=0;
double time_CD=0;
bool useHighLow=false;
bool bBearish=false;
int FirstHigh=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).HighBar(0,1,barsBack);
int SecondHigh=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).HighBar(0,2,barsBack);
int FirstLow=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).LowBar(0,1,barsBack);
int SecondLow=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).LowBar(0,2,barsBack);
int ThirdLow=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).LowBar(0,3,barsBack);
int ThirdHigh=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow).HighBar(0,3,barsBack);
if(FirstHigh==-1 || SecondHigh==-1 || FirstLow==-1 || SecondLow==-1) return;
if(FirstLow<FirstHigh) {
// bullish case
if(SecondLow==-1) return;
B=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[FirstLow];
A=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[FirstHigh];
x.Set(ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[SecondLow]);
AB=Diagonal(B-A,FirstLow-FirstHigh);
XA=Diagonal(A-X[0],FirstHigh-SecondLow);
}
else {
// bearish case
if(ThirdHigh==-1) return;
B=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[FirstHigh];
A=ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[FirstLow];
x.Set(ZigZag(DeviationType.Percent,ZigZagPercentDeviation,useHighLow)[SecondHigh]);
AB=Diagonal(B-A,FirstLow-FirstHigh);
XA=Diagonal(A-X[0],SecondHigh-FirstLow);
bBearish=true;
}
//Print("Currentbar="+CurrentBar.ToString());
// Print(AB);
// Print(time_CD);
// Print(XA);
// Print(BC);
// Print(CD);
if(AB<=0.618*(1-TolerancePercent/100)*XA || AB>=0.786*(1+TolerancePercent/100)*XA) return;
// now conditions:
string tag=CurrentBar.ToString();
bool bautoscale=true;
if(bBearish) {
ipatternType.Set(GartleyButterflyPatternType.Bearish);
patternType.Set((double)GartleyButterflyPatternType.Bearish);
idealProfitTarget.Set(B-0.618*AB);
tag+="0.786 Bearish Retracement";
DrawLine(tag+"AB",bautoscale,SecondHigh,B,SecondLow,A,PenColor,DashStyle,PenWidth);
DrawLine(tag+"XA",bautoscale,SecondLow,A,ThirdHigh,X[0],PenColor,DashStyle,PenWidth);
}
else {
//bullish
ipatternType.Set(GartleyButterflyPatternType.Bullish);
patternType.Set((double)GartleyButterflyPatternType.Bullish);
idealProfitTarget.Set(B+0.618*AB);
tag+="0.786 Bullish Retracement";
DrawLine(tag+"AB",bautoscale,SecondLow,B,SecondHigh,A,PenColor,DashStyle,PenWidth);
DrawLine(tag+"XA",bautoscale,SecondHigh,A,ThirdLow,X[0],PenColor,DashStyle,PenWidth);
}
}
#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries X
{
get { return x; }
}
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()]
public DataSeries IdealProfitTarget
{
get { return idealProfitTarget; }
}
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()]
public IntSeries Value
{
get { return ipatternType; }
}
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()]
public DataSeries PatternType
{
get { return patternType; }
}
[Description("Pen Color Pattern")]
[GridCategory("Plot")]
[XmlIgnore()]
public Color PenColor
{
get { return penColor; }
set { penColor = value; }
}
// Serialize our Color object
[Browsable(false)]
public string PenColorSerialize
{
get { return NinjaTrader.Gui.Design.SerializableColor.ToString(penColor); }
set { penColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); }
}
[Description("Dash Style")]
[GridCategory("Plot")]
public DashStyle DashStyle
{
get { return dashStyle; }
set { dashStyle = value; }
}
[Description("Pen Width")]
[GridCategory("Plot")]
public int PenWidth
{
get { return penWidth; }
set { penWidth = Math.Max(0,value); }
}
[Description("Bars back")]
[GridCategory("Parameters")]
public int BarsBack
{
get { return barsBack; }
set { barsBack = Math.Max(5,value); }
}
[Description("")]
[GridCategory("Parameters")]
public double ZigZagPercentDeviation
{
get { return zigZagPercentDeviation; }
set { zigZagPercentDeviation = Math.Max(0.001, value); }
}
[Description("")]
[GridCategory("Parameters")]
public double TolerancePercent
{
get { return tolerancePercent; }
set { tolerancePercent = Math.Max(0.0, value); }
}
#endregion
}
}
|