Jump to content

Bearish / Bullish .786 Retracement Code NT


rg83

Recommended Posts

Can someone help me, I am trying to adjust the code to show bullish and bearish .786 retracement triangles.

 

I have an error as it wont plot, any help would be great!

 

#region Using declarations

using System;

using System.ComponentModel;

using System.Diagnostics;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Xml.Serialization;

using NinjaTrader.Cbi;

using NinjaTrader.Data;

using NinjaTrader.Gui.Chart;

#endregion

 

namespace NinjaTrader.Indicator

{

/// <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

}

}

Link to comment
Share on other sites

You're welcome, mate. I see on the NT Forum that one of the CS techs has weighed-in.

 

Hopefully a user code guru will give you some help...either there or here.

 

Cheers!

 

Mick

 

By the way...what does the error code show when you try to compile...that screenshot of the error listing may motivate a code guru to jump in and assist (here and over there).

Edited by tryitagainmf
Link to comment
Share on other sites

Thanks Mick,

 

I was trying to adjust the code from a gartley indicator but my force is **** when it comes to coding...no error when compiling it just wont plot the patterns when they show.

 

I see you are in the isle of man, in jersey myself. Do you know of any trading rooms, arcades in the islands?

Link to comment
Share on other sites

tryitagainmf, you can insert the code below line # 37 or anywhere as long as it is not inside another block of code. From the looks of the code, the missing 'GartleyButterflyPatternType' code is probably not an enum but a class that returns two properties.

 

When I created 'GartleyButterflyPatternType' as an enum, line 129, 130, 140, 141 failed because it is expecting an INT and not an enum. So what I'm thinking is 'GartleyButterflyPatternType' is probably provided by anothing indicator.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...