Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Code Adjustment .786 Retracement Triangle

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Code Adjustment .786 Retracement Triangle

    Hi,

    i am trying to adjust the cod eto the gartley indicator so that it plots .786 retracement triangle patterns. Can someone have a look at the code and tell me why it wont plot. I am not sure if ZigZag is appropriate to catch the pattern,

    #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,ZigZagPerce ntDeviation,useHighLow).HighBar(0,1,barsBack);
    int SecondHigh=ZigZag(DeviationType.Percent,ZigZagPerc entDeviation,useHighLow).HighBar(0,2,barsBack);
    int FirstLow=ZigZag(DeviationType.Percent,ZigZagPercen tDeviation,useHighLow).LowBar(0,1,barsBack);
    int SecondLow=ZigZag(DeviationType.Percent,ZigZagPerce ntDeviation,useHighLow).LowBar(0,2,barsBack);
    int ThirdLow=ZigZag(DeviationType.Percent,ZigZagPercen tDeviation,useHighLow).LowBar(0,3,barsBack);
    int ThirdHigh=ZigZag(DeviationType.Percent,ZigZagPerce ntDeviation,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,ZigZagPercentDeviat ion,useHighLow)[FirstLow];
    A=ZigZag(DeviationType.Percent,ZigZagPercentDeviat ion,useHighLow)[FirstHigh];
    x.Set(ZigZag(DeviationType.Percent,ZigZagPercentDe viation,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,ZigZagPercentDeviat ion,useHighLow)[FirstHigh];
    A=ZigZag(DeviationType.Percent,ZigZagPercentDeviat ion,useHighLow)[FirstLow];
    x.Set(ZigZag(DeviationType.Percent,ZigZagPercentDe viation,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.Beari sh);
    patternType.Set((double)GartleyButterflyPatternTyp e.Bearish);
    idealProfitTarget.Set(B-0.618*AB);
    tag+="0.786 Bearish Retracement";

    DrawLine(tag+"AB",bautoscale,SecondHigh,B,SecondLo w,A,PenColor,DashStyle,PenWidth);
    DrawLine(tag+"XA",bautoscale,SecondLow,A,ThirdHigh ,X[0],PenColor,DashStyle,PenWidth);

    }
    else {
    //bullish
    ipatternType.Set(GartleyButterflyPatternType.Bulli sh);
    patternType.Set((double)GartleyButterflyPatternTyp e.Bullish);
    idealProfitTarget.Set(B+0.618*AB);
    tag+="0.786 Bullish Retracement";

    DrawLine(tag+"AB",bautoscale,SecondLow,B,SecondHig h,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.FromStrin g(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
    }
    }

    #2
    Hello mccallum28,
    Unfortunately we do not debug custom NinjaScript code. I will however leave the thread open for our fellow members who can further assist you.

    In this regard, let me specify it is always helpful to upload the indicator as a source file using the export feature in NinjaTrader.
    JoydeepNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by junkone, Today, 11:37 AM
    0 responses
    3 views
    0 likes
    Last Post junkone
    by junkone
     
    Started by quantismo, 04-17-2024, 05:13 PM
    5 responses
    34 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by proptrade13, Today, 11:06 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by love2code2trade, 04-17-2024, 01:45 PM
    4 responses
    34 views
    0 likes
    Last Post love2code2trade  
    Started by cls71, Today, 04:45 AM
    2 responses
    10 views
    0 likes
    Last Post eDanny
    by eDanny
     
    Working...
    X