Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using declarations
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Diagnostics.CodeAnalysis;
- using System.Windows;
- using System.Reflection;
- using System.Windows.Media;
- using System.Xml.Serialization;
- using NinjaTrader.Cbi;
- using NinjaTrader.Data;
- using NinjaTrader.Gui;
- using NinjaTrader.Gui.Chart;
- using NinjaTrader.Gui.Tools;
- using NinjaTrader.NinjaScript.DrawingTools;
- using NinjaTrader.NinjaScript.Indicators;
- #endregion
- // This namespace holds all indicators and is required. Do not change it.
- namespace NinjaTrader.NinjaScript.Indicators
- {
- /// <summary>
- /// detects bullish and bearish and sets value to 1 or -1 or 0 if not found
- /// </summary>
- public class grt : Indicator
- {
- #region Variables
- // Wizard generated variables
- private double tolerancePercent = 25; // Default setting for TolerancePercent
- // User defined variables (add any user defined variables below)
- private Brush upColor = Brushes.DeepSkyBlue;
- private Brush downColor = Brushes.OrangeRed;
- private double toffset;
- private Series<int> _match;
- private SimpleFont abcTextFont = new SimpleFont("Courier", 11) { Bold = true };
- private int abcTextOffsetLabel = 50;
- #endregion
- /// <summary>
- /// This method is used to configure the indicator and is called once before any bar data is loaded.
- /// </summary>
- private void Initialize()
- {
- AddPlot(new Stroke(Brushes.Orange), PlotStyle.Line, "Pattern");
- Calculate = Calculate.OnBarClose;
- IsOverlay = true;
- // NT8 REMOVED: PriceTypeSupported = true;
- _match = new Series<int>(this);
- }
- private int BARSBACK = 400;
- private int displacer_Index_ZZ;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Name = "";
- Description = "detects bullish and bearish and sets value to 1 or -1 or 0 if not found";
- Calculate = Calculate.OnBarClose;
- //IsOverlay = true;
- DisplayInDataBox = true;
- IsAutoScale = true;
- DrawOnPricePanel = true;
- DrawHorizontalGridLines = true;
- //DrawVerticalGridLines = true;
- PaintPriceMarkers = true;
- ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
- //Disable this property if your indicator requires custom values that cumulate with each new market data event.
- //See Help Guide for additional information.
- IsSuspendedWhileInactive = true;
- //customs
- Initialize();
- ShowZigZag = true;
- ZigZag_DeviationType = DeviationType.Points;
- ZigZag_DeviationValue = 0.5;
- ZigZag_UseHighLow = true;
- }
- else if (State == State.Configure)
- {
- Calculate = Calculate.OnBarClose; //!must
- }
- else if (State == State.DataLoaded)
- {
- zz1 = ZigZag(ZigZag_DeviationType, ZigZag_DeviationValue, ZigZag_UseHighLow);
- if (ShowZigZag)
- {
- }
- displacer_Index_ZZ = Calculate == Calculate.OnBarClose ? 1 : 0;
- }
- else if (State == State.Terminated)
- {
- }
- }
- /// <summary>
- /// Called on each bar update event (incoming tick)
- /// </summary>
- public Series<int> Match
- {
- get { return this._match; }
- }
- private double Diagonal(double A, double B)
- {
- return Math.Sqrt(A * A + B * B);
- }
- ZigZag zz1;
- protected override void OnBarUpdate()
- {
- zz1.Update();
- higshBarN.Clear();
- lowsBarN.Clear();
- highsBarVal.Clear();
- lowsBarVal.Clear();
- if (CurrentBar == Bars.Count - 1 - (Calculate == Calculate.OnBarClose ? 1 : 0))
- {
- int max = Bars.Count; //
- for (var i = max - 5; i >= 0; i--)
- {
- main(i);
- }
- }
- }
- List<int> higshBarN = new List<int>();
- List<int> lowsBarN = new List<int>();
- List<double> highsBarVal = new List<double>();
- List<double> lowsBarVal = new List<double>();
- List<int> drawnBars = new List<int>();
- private void main(int idx)
- {
- // Use this method for calculating your indicator values. Assign a value to each
- // plot below by replacing 'Close[0]' with your own formula.
- if (CurrentBar <= 10) return;
- int currentBar = CurrentBar - idx;
- int idx0 = idx; // + displacer_Index_ZZ + 0; ///1 because ZZ executed on last
- var ZZ_val = zz1[idx0];
- bool isZZok = zz1.IsValidDataPoint(idx0); // ZZ_val != 0;
- bool isZZ_HIGH = isZZok && ZZ_val == zz1.ZigZagHigh[idx0];
- bool isZZ_LOW = isZZok && ZZ_val == zz1.ZigZagLow[idx0];
- if (isZZ_HIGH && !higshBarN.Contains(currentBar))
- {
- Draw.Text(this, "t1" + currentBar, "*".ToString(), idx, High[idx]);
- higshBarN.Add(currentBar);
- highsBarVal.Add(ZZ_val);
- }
- if (isZZ_LOW && !lowsBarN.Contains(currentBar))
- {
- Draw.Text(this, "t2" + currentBar, "*".ToString(), idx, Low[idx]);
- lowsBarN.Add(currentBar);
- lowsBarVal.Add(ZZ_val);
- }
- double D = 0;
- double C = 0;
- double B = 0;
- double A = 0;
- double X = 0;
- double AB = 0;
- double BC = 0;
- double XA = 0;
- double CD = 0;
- double time_CD = 0;
- bool bBearish = false;
- bool bBullish = false;
- //int FirstHigh = zz1.HighBar(idx0, 1, BARSBACK);
- //int SecondHigh = zz1.HighBar(idx0, 2, BARSBACK);
- //int ThirdHigh = zz1.HighBar(idx0, 3, BARSBACK);
- //int FirstLow = zz1.LowBar(idx0, 1, BARSBACK);
- //int SecondLow = zz1.LowBar(idx0, 2, BARSBACK);
- //int ThirdLow = zz1.LowBar(idx0, 3, BARSBACK);
- int FirstHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 1];
- int SecondHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 2];
- int ThirdHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 3];
- int FirstLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 1];
- int SecondLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 2];
- int ThirdLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 3];
- if (FirstHighBN == -1 || SecondHighBN == -1 || FirstLowBN == -1 || SecondLowBN == -1) return;
- if (!isZZok)
- return;
- if (drawnBars.Contains(currentBar)) return;
- drawnBars.Add(currentBar);
- if (FirstHighBN > FirstLowBN)
- {
- // bullish case
- if (ThirdLowBN == -1) return;
- D = lowsBarVal[0]; // zz1[FirstLowBN];
- C = highsBarVal[0]; // zz1[FirstHighBN];
- B = lowsBarVal[1];
- A = highsBarVal[1];
- X = lowsBarVal[2];
- XA = Diagonal(A - X, SecondHighBN - ThirdLowBN);
- AB = Diagonal(B - A, SecondLowBN - SecondHighBN);
- BC = Diagonal(C - B, FirstHighBN - SecondLowBN);
- CD = Diagonal(D - C, FirstLowBN - FirstHighBN);
- time_CD = Math.Abs(FirstLowBN - FirstHighBN);
- bBullish = true;
- }
- else
- {
- // bearish case
- if (ThirdHighBN == -1) return;
- D = highsBarVal[0]; //zz1[FirstHigh];
- C = lowsBarVal[0];
- B = highsBarVal[1];
- A = lowsBarVal[1];
- X = highsBarVal[2];
- XA = Diagonal(A - X, SecondLowBN - ThirdHighBN);
- AB = Diagonal(B - A, SecondHighBN - SecondLowBN);
- BC = Diagonal(C - B, FirstLowBN - SecondHighBN);
- CD = Diagonal(D - C, FirstHighBN - FirstLowBN);
- time_CD = Math.Abs(FirstHighBN - FirstLowBN);
- bBearish = true;
- }
- p(currentBar, bBearish, bBullish);
- //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.618 * (1 + TolerancePercent / 100) * XA) return;
- if (AB <= 1.0 * (1 - TolerancePercent / 100) * time_CD || AB >= 1.0 * (1 + TolerancePercent / 100) * time_CD) return;
- if (BC <= 0.618 * (1 - TolerancePercent / 100) * AB || BC >= 0.786 * (1 + TolerancePercent / 100) * AB) return;
- if (CD <= 1.27 * (1 - TolerancePercent / 100) * BC || CD >= 1.618 * (1 + TolerancePercent / 100) * BC) return;
- // now conditions:
- string tag = currentBar.ToString();
- int dist = CurrentBar - currentBar;
- if (bBearish)
- {
- Match[0] = -1;
- // so at least when you call the indicator you get a value when Gartley Pattern matched
- // but you have to find out yourself if M or W pattern
- tag += "W";
- // Plots[0].Pen.Color,
- Draw.Line(this, tag + "CD", true, dist + FirstHighBN, D, dist + FirstLowBN, C, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "BC", true, dist + FirstLowBN, C, dist + SecondHighBN, B, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "AB", true, dist + SecondHighBN, B, dist + SecondLowBN, A, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "XA", true, dist + SecondLowBN, A, dist + ThirdHighBN, X, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- //cory
- Draw.Text(this, "1w" + currentBar, true, "1", dist + ThirdHighBN, X, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "2w" + currentBar, true, "2", dist + SecondLowBN, A, -abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "3w" + currentBar, true, "3", dist + SecondHighBN, B, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "4w" + currentBar, true, "4", dist + FirstLowBN, C, -abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "5w" + currentBar, true, "5", dist + FirstHighBN, D, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- }
- if (bBullish)
- {
- //bullish
- Match[0] = 1;
- tag += "M";
- // DrawLine(tag+"CD", false, FirstLow,D,FirstHigh,C,Plots[0].Pen.Color,Plots[0].Pen.DashStyle,System.Convert.ToInt32(Plots[0].Pen.Width));
- Draw.Line(this, tag + "CD", true, dist + FirstLowBN, D, dist + FirstHighBN, C, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "BC", true, dist + FirstHighBN, C, dist + SecondLowBN, B, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "AB", true, dist + SecondLowBN, B, dist + SecondHighBN, A, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- Draw.Line(this, tag + "XA", true, dist + SecondHighBN, A, dist + ThirdLowBN, X, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
- //cory
- Draw.Text(this, "1m" + currentBar, true, "1", dist + ThirdLowBN, X, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "2m" + currentBar, true, "2", dist + SecondHighBN, A, +abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "3m" + currentBar, true, "3", dist + SecondLowBN, B, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "4m" + currentBar, true, "4", dist + FirstHighBN, C, +abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- Draw.Text(this, "5m" + currentBar, true, "5", dist + FirstLowBN, D, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
- }
- }
- #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 Series<double> Pattern
- {
- get { return Values[0]; }
- }
- [Display(ResourceType = typeof(Custom.Resource), Name = "Show ZigZag", GroupName = "ZigZag parameters", Order = 10)]
- public bool ShowZigZag
- { get; set; }
- [Display(ResourceType = typeof(Custom.Resource), Name = "DeviationType", GroupName = "ZigZag parameters", Order = 15)]
- public DeviationType ZigZag_DeviationType
- { get; set; }
- [Range(0, int.MaxValue), NinjaScriptProperty]
- [Display(ResourceType = typeof(Custom.Resource), Name = "DeviationValue", GroupName = "ZigZag parameters", Order = 20)]
- public double ZigZag_DeviationValue
- { get; set; }
- [Display(ResourceType = typeof(Custom.Resource), Name = "UseHighLow", GroupName = "ZigZag parameters", Order = 25)]
- public bool ZigZag_UseHighLow
- { get; set; }
- [NinjaScriptProperty]
- [Display(Description = "", GroupName = "Parameters", Order = 1)]
- public double TolerancePercent
- {
- get { return tolerancePercent; }
- set { tolerancePercent = Math.Max(0.0, value); }
- }
- [XmlIgnore()]
- [NinjaScriptProperty]
- [Display(Name = "Bull Color", Description = "Select color for bull", GroupName = "Plots", Order = 1)]
- public Brush UpColor
- {
- get { return upColor; }
- set { upColor = value; }
- }
- // Serialize Color object
- [Browsable(false)]
- public string UpColorSerialize
- {
- get { return Serialize.BrushToString(upColor); }
- set { upColor = Serialize.StringToBrush(value); }
- }
- /// <summary>
- /// </summary>
- [XmlIgnore()]
- [NinjaScriptProperty]
- [Display(Name = "Bear Color", Description = "Select color for bear", GroupName = "Plots", Order = 1)]
- public Brush DownColor
- {
- get { return downColor; }
- set { downColor = value; }
- }
- // Serialize Color object
- [Browsable(false)]
- public string DownColorSerialize
- {
- get { return Serialize.BrushToString(downColor); }
- set { downColor = Serialize.StringToBrush(value); }
- }
- [NinjaScriptProperty]
- [Display(Name = "Text offset label", Description = "Represents the offset value in pixels from within the text box area that display the swing label.", GroupName = "Visualize patterns", Order = 1)]
- public int TextOffsetLabel
- {
- get { return abcTextOffsetLabel; }
- set { abcTextOffsetLabel = Math.Max(1, value); }
- }
- [XmlIgnore()]
- [NinjaScriptProperty]
- [Display(Name = "Text font", Description = "Represents the text font for the displayed swing information.", GroupName = "Visualize patterns", Order = 1)]
- public SimpleFont TextFont
- {
- get { return abcTextFont; }
- set { abcTextFont = value; }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement