NexusFi: Find Your Edge


Home Menu

 





Bearish, Bullish Retracement Ind


Discussion in NinjaTrader

Updated
    1. trending_up 2,431 views
    2. thumb_up 1 thanks given
    3. group 1 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread

Bearish, Bullish Retracement Ind

  #1 (permalink)
 
ryangillespie's Avatar
 ryangillespie 
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
}
}

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Exit Strategy
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
MC PL editor upgrade
MultiCharts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Diary of a simple price action trader
26 thanks
Just another trading journal: PA, Wyckoff & Trends
24 thanks
Tao te Trade: way of the WLD
22 thanks
My NQ Trading Journal
16 thanks
HumbleTraders next chapter
9 thanks
  #3 (permalink)
 
learning0101's Avatar
 learning0101 
Houston, Texas
 
Experience: Intermediate
Platform: NinjaTrader,TOS,Etrade,St
Broker: NT:( tos,etrade,CS
Trading: stocks,options,futures,forex
Frequency: Never
Duration: Never
Posts: 287 since Aug 2011
Thanks Given: 3,613
Thanks Received: 222


Hi @ryangillespie
you may get more response/help if you post here

Reply With Quote
Thanked by:




Last Updated on October 17, 2012


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts