retesere20

---previoius-vers-gartl22-jhb

Aug 5th, 2019
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region Using declarations
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Windows;
  9. using System.Reflection;
  10. using System.Windows.Media;
  11. using System.Xml.Serialization;
  12. using NinjaTrader.Cbi;
  13. using NinjaTrader.Data;
  14. using NinjaTrader.Gui;
  15. using NinjaTrader.Gui.Chart;
  16. using NinjaTrader.Gui.Tools;
  17. using NinjaTrader.NinjaScript.DrawingTools;
  18. using NinjaTrader.NinjaScript.Indicators;
  19. #endregion
  20.  
  21. // This namespace holds all indicators and is required. Do not change it.
  22. namespace NinjaTrader.NinjaScript.Indicators
  23. {
  24. /// <summary>
  25. /// detects bullish and bearish and sets value to 1 or -1 or 0 if not found
  26. /// </summary>
  27. public class grt : Indicator
  28. {
  29. #region Variables
  30. // Wizard generated variables
  31. private double tolerancePercent = 25; // Default setting for TolerancePercent
  32. // User defined variables (add any user defined variables below)
  33. private Brush upColor = Brushes.DeepSkyBlue;
  34. private Brush downColor = Brushes.OrangeRed;
  35. private double toffset;
  36. private Series<int> _match;
  37. private SimpleFont abcTextFont = new SimpleFont("Courier", 11) { Bold = true };
  38. private int abcTextOffsetLabel = 50;
  39.  
  40. #endregion
  41.  
  42. /// <summary>
  43. /// This method is used to configure the indicator and is called once before any bar data is loaded.
  44. /// </summary>
  45. private void Initialize()
  46. {
  47. AddPlot(new Stroke(Brushes.Orange), PlotStyle.Line, "Pattern");
  48. Calculate = Calculate.OnBarClose;
  49. IsOverlay = true;
  50. // NT8 REMOVED: PriceTypeSupported = true;
  51. _match = new Series<int>(this);
  52. }
  53.  
  54. private int BARSBACK = 400;
  55. private int displacer_Index_ZZ;
  56.  
  57.  
  58. protected override void OnStateChange()
  59. {
  60. if (State == State.SetDefaults)
  61. {
  62. Name = "";
  63. Description = "detects bullish and bearish and sets value to 1 or -1 or 0 if not found";
  64. Calculate = Calculate.OnBarClose;
  65. //IsOverlay = true;
  66. DisplayInDataBox = true;
  67. IsAutoScale = true;
  68. DrawOnPricePanel = true;
  69. DrawHorizontalGridLines = true;
  70. //DrawVerticalGridLines = true;
  71. PaintPriceMarkers = true;
  72. ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
  73. //Disable this property if your indicator requires custom values that cumulate with each new market data event.
  74. //See Help Guide for additional information.
  75. IsSuspendedWhileInactive = true;
  76.  
  77. //customs
  78. Initialize();
  79. ShowZigZag = true;
  80. ZigZag_DeviationType = DeviationType.Points;
  81. ZigZag_DeviationValue = 0.5;
  82. ZigZag_UseHighLow = true;
  83. }
  84. else if (State == State.Configure)
  85. {
  86. Calculate = Calculate.OnBarClose; //!must
  87. }
  88. else if (State == State.DataLoaded)
  89. {
  90. zz1 = ZigZag(ZigZag_DeviationType, ZigZag_DeviationValue, ZigZag_UseHighLow);
  91. if (ShowZigZag)
  92. {
  93.  
  94. }
  95. displacer_Index_ZZ = Calculate == Calculate.OnBarClose ? 1 : 0;
  96. }
  97. else if (State == State.Terminated)
  98. {
  99.  
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// Called on each bar update event (incoming tick)
  105. /// </summary>
  106.  
  107. public Series<int> Match
  108. {
  109. get { return this._match; }
  110. }
  111.  
  112. private double Diagonal(double A, double B)
  113. {
  114. return Math.Sqrt(A * A + B * B);
  115. }
  116.  
  117. ZigZag zz1;
  118.  
  119. protected override void OnBarUpdate()
  120. {
  121. zz1.Update();
  122.  
  123. higshBarN.Clear();
  124. lowsBarN.Clear();
  125. highsBarVal.Clear();
  126. lowsBarVal.Clear();
  127.  
  128. if (CurrentBar == Bars.Count - 1 - (Calculate == Calculate.OnBarClose ? 1 : 0))
  129. {
  130. int max = Bars.Count; //
  131. for (var i = max - 5; i >= 0; i--)
  132. {
  133. main(i);
  134. }
  135. }
  136. }
  137.  
  138. List<int> higshBarN = new List<int>();
  139. List<int> lowsBarN = new List<int>();
  140. List<double> highsBarVal = new List<double>();
  141. List<double> lowsBarVal = new List<double>();
  142.  
  143. List<int> drawnBars = new List<int>();
  144. private void main(int idx)
  145. {
  146. // Use this method for calculating your indicator values. Assign a value to each
  147. // plot below by replacing 'Close[0]' with your own formula.
  148. if (CurrentBar <= 10) return;
  149. int currentBar = CurrentBar - idx;
  150.  
  151. int idx0 = idx; // + displacer_Index_ZZ + 0; ///1 because ZZ executed on last
  152. var ZZ_val = zz1[idx0];
  153. bool isZZok = zz1.IsValidDataPoint(idx0); // ZZ_val != 0;
  154.  
  155. bool isZZ_HIGH = isZZok && ZZ_val == zz1.ZigZagHigh[idx0];
  156. bool isZZ_LOW = isZZok && ZZ_val == zz1.ZigZagLow[idx0];
  157.  
  158. if (isZZ_HIGH && !higshBarN.Contains(currentBar))
  159. {
  160. Draw.Text(this, "t1" + currentBar, "*".ToString(), idx, High[idx]);
  161. higshBarN.Add(currentBar);
  162. highsBarVal.Add(ZZ_val);
  163. }
  164. if (isZZ_LOW && !lowsBarN.Contains(currentBar))
  165. {
  166. Draw.Text(this, "t2" + currentBar, "*".ToString(), idx, Low[idx]);
  167. lowsBarN.Add(currentBar);
  168. lowsBarVal.Add(ZZ_val);
  169. }
  170.  
  171. double D = 0;
  172. double C = 0;
  173. double B = 0;
  174. double A = 0;
  175. double X = 0;
  176. double AB = 0;
  177. double BC = 0;
  178. double XA = 0;
  179. double CD = 0;
  180. double time_CD = 0;
  181. bool bBearish = false;
  182. bool bBullish = false;
  183.  
  184. //int FirstHigh = zz1.HighBar(idx0, 1, BARSBACK);
  185. //int SecondHigh = zz1.HighBar(idx0, 2, BARSBACK);
  186. //int ThirdHigh = zz1.HighBar(idx0, 3, BARSBACK);
  187. //int FirstLow = zz1.LowBar(idx0, 1, BARSBACK);
  188. //int SecondLow = zz1.LowBar(idx0, 2, BARSBACK);
  189. //int ThirdLow = zz1.LowBar(idx0, 3, BARSBACK);
  190.  
  191. int FirstHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 1];
  192. int SecondHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 2];
  193. int ThirdHighBN = higshBarN.Count < 3 ? -1 : currentBar - higshBarN[higshBarN.Count - 3];
  194. int FirstLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 1];
  195. int SecondLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 2];
  196. int ThirdLowBN = lowsBarN.Count < 3 ? -1 : currentBar - lowsBarN[lowsBarN.Count - 3];
  197.  
  198. if (FirstHighBN == -1 || SecondHighBN == -1 || FirstLowBN == -1 || SecondLowBN == -1) return;
  199.  
  200. if (!isZZok)
  201. return;
  202. if (drawnBars.Contains(currentBar)) return;
  203. drawnBars.Add(currentBar);
  204.  
  205. if (FirstHighBN > FirstLowBN)
  206. {
  207. // bullish case
  208. if (ThirdLowBN == -1) return;
  209. D = lowsBarVal[0]; // zz1[FirstLowBN];
  210. C = highsBarVal[0]; // zz1[FirstHighBN];
  211. B = lowsBarVal[1];
  212. A = highsBarVal[1];
  213. X = lowsBarVal[2];
  214.  
  215. XA = Diagonal(A - X, SecondHighBN - ThirdLowBN);
  216. AB = Diagonal(B - A, SecondLowBN - SecondHighBN);
  217. BC = Diagonal(C - B, FirstHighBN - SecondLowBN);
  218. CD = Diagonal(D - C, FirstLowBN - FirstHighBN);
  219. time_CD = Math.Abs(FirstLowBN - FirstHighBN);
  220. bBullish = true;
  221. }
  222. else
  223. {
  224. // bearish case
  225. if (ThirdHighBN == -1) return;
  226. D = highsBarVal[0]; //zz1[FirstHigh];
  227. C = lowsBarVal[0];
  228. B = highsBarVal[1];
  229. A = lowsBarVal[1];
  230. X = highsBarVal[2];
  231.  
  232. XA = Diagonal(A - X, SecondLowBN - ThirdHighBN);
  233. AB = Diagonal(B - A, SecondHighBN - SecondLowBN);
  234. BC = Diagonal(C - B, FirstLowBN - SecondHighBN);
  235. CD = Diagonal(D - C, FirstHighBN - FirstLowBN);
  236. time_CD = Math.Abs(FirstHighBN - FirstLowBN);
  237. bBearish = true;
  238. }
  239.  
  240. p(currentBar, bBearish, bBullish);
  241. //Print("Currentbar="+CurrentBar.ToString());
  242.  
  243. // Print(AB);
  244. // Print(time_CD);
  245. // Print(XA);
  246. // Print(BC);
  247. // Print(CD);
  248.  
  249. if (AB <= 0.618 * (1 - TolerancePercent / 100) * XA || AB >= 0.618 * (1 + TolerancePercent / 100) * XA) return;
  250. if (AB <= 1.0 * (1 - TolerancePercent / 100) * time_CD || AB >= 1.0 * (1 + TolerancePercent / 100) * time_CD) return;
  251.  
  252. if (BC <= 0.618 * (1 - TolerancePercent / 100) * AB || BC >= 0.786 * (1 + TolerancePercent / 100) * AB) return;
  253. if (CD <= 1.27 * (1 - TolerancePercent / 100) * BC || CD >= 1.618 * (1 + TolerancePercent / 100) * BC) return;
  254. // now conditions:
  255. string tag = currentBar.ToString();
  256.  
  257. int dist = CurrentBar - currentBar;
  258. if (bBearish)
  259. {
  260. Match[0] = -1;
  261. // so at least when you call the indicator you get a value when Gartley Pattern matched
  262. // but you have to find out yourself if M or W pattern
  263. tag += "W";
  264.  
  265. // Plots[0].Pen.Color,
  266. Draw.Line(this, tag + "CD", true, dist + FirstHighBN, D, dist + FirstLowBN, C, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  267. Draw.Line(this, tag + "BC", true, dist + FirstLowBN, C, dist + SecondHighBN, B, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  268. Draw.Line(this, tag + "AB", true, dist + SecondHighBN, B, dist + SecondLowBN, A, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  269. Draw.Line(this, tag + "XA", true, dist + SecondLowBN, A, dist + ThirdHighBN, X, downColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  270. //cory
  271. Draw.Text(this, "1w" + currentBar, true, "1", dist + ThirdHighBN, X, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  272. Draw.Text(this, "2w" + currentBar, true, "2", dist + SecondLowBN, A, -abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  273. Draw.Text(this, "3w" + currentBar, true, "3", dist + SecondHighBN, B, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  274. Draw.Text(this, "4w" + currentBar, true, "4", dist + FirstLowBN, C, -abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  275. Draw.Text(this, "5w" + currentBar, true, "5", dist + FirstHighBN, D, +abcTextOffsetLabel, downColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  276.  
  277. }
  278. if (bBullish)
  279. {
  280. //bullish
  281. Match[0] = 1;
  282. tag += "M";
  283.  
  284. // DrawLine(tag+"CD", false, FirstLow,D,FirstHigh,C,Plots[0].Pen.Color,Plots[0].Pen.DashStyle,System.Convert.ToInt32(Plots[0].Pen.Width));
  285. Draw.Line(this, tag + "CD", true, dist + FirstLowBN, D, dist + FirstHighBN, C, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  286. Draw.Line(this, tag + "BC", true, dist + FirstHighBN, C, dist + SecondLowBN, B, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  287. Draw.Line(this, tag + "AB", true, dist + SecondLowBN, B, dist + SecondHighBN, A, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  288. Draw.Line(this, tag + "XA", true, dist + SecondHighBN, A, dist + ThirdLowBN, X, upColor, Plots[0].DashStyleHelper, System.Convert.ToInt32(Plots[0].Width));
  289. //cory
  290. Draw.Text(this, "1m" + currentBar, true, "1", dist + ThirdLowBN, X, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  291. Draw.Text(this, "2m" + currentBar, true, "2", dist + SecondHighBN, A, +abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  292. Draw.Text(this, "3m" + currentBar, true, "3", dist + SecondLowBN, B, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  293. Draw.Text(this, "4m" + currentBar, true, "4", dist + FirstHighBN, C, +abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  294. Draw.Text(this, "5m" + currentBar, true, "5", dist + FirstLowBN, D, -abcTextOffsetLabel, upColor, abcTextFont, TextAlignment.Center, Brushes.Azure, Brushes.Azure, 5);
  295. }
  296.  
  297. }
  298.  
  299. #region Properties
  300. [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
  301. [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
  302. public Series<double> Pattern
  303. {
  304. get { return Values[0]; }
  305. }
  306.  
  307. [Display(ResourceType = typeof(Custom.Resource), Name = "Show ZigZag", GroupName = "ZigZag parameters", Order = 10)]
  308. public bool ShowZigZag
  309. { get; set; }
  310.  
  311. [Display(ResourceType = typeof(Custom.Resource), Name = "DeviationType", GroupName = "ZigZag parameters", Order = 15)]
  312. public DeviationType ZigZag_DeviationType
  313. { get; set; }
  314.  
  315. [Range(0, int.MaxValue), NinjaScriptProperty]
  316. [Display(ResourceType = typeof(Custom.Resource), Name = "DeviationValue", GroupName = "ZigZag parameters", Order = 20)]
  317. public double ZigZag_DeviationValue
  318. { get; set; }
  319.  
  320. [Display(ResourceType = typeof(Custom.Resource), Name = "UseHighLow", GroupName = "ZigZag parameters", Order = 25)]
  321. public bool ZigZag_UseHighLow
  322. { get; set; }
  323.  
  324.  
  325. [NinjaScriptProperty]
  326. [Display(Description = "", GroupName = "Parameters", Order = 1)]
  327. public double TolerancePercent
  328. {
  329. get { return tolerancePercent; }
  330. set { tolerancePercent = Math.Max(0.0, value); }
  331. }
  332. [XmlIgnore()]
  333. [NinjaScriptProperty]
  334. [Display(Name = "Bull Color", Description = "Select color for bull", GroupName = "Plots", Order = 1)]
  335. public Brush UpColor
  336. {
  337. get { return upColor; }
  338. set { upColor = value; }
  339. }
  340.  
  341. // Serialize Color object
  342. [Browsable(false)]
  343. public string UpColorSerialize
  344. {
  345. get { return Serialize.BrushToString(upColor); }
  346. set { upColor = Serialize.StringToBrush(value); }
  347. }
  348.  
  349. /// <summary>
  350. /// </summary>
  351. [XmlIgnore()]
  352.  
  353. [NinjaScriptProperty]
  354. [Display(Name = "Bear Color", Description = "Select color for bear", GroupName = "Plots", Order = 1)]
  355. public Brush DownColor
  356. {
  357. get { return downColor; }
  358. set { downColor = value; }
  359. }
  360.  
  361. // Serialize Color object
  362. [Browsable(false)]
  363. public string DownColorSerialize
  364. {
  365. get { return Serialize.BrushToString(downColor); }
  366. set { downColor = Serialize.StringToBrush(value); }
  367. }
  368. [NinjaScriptProperty]
  369. [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)]
  370. public int TextOffsetLabel
  371. {
  372. get { return abcTextOffsetLabel; }
  373. set { abcTextOffsetLabel = Math.Max(1, value); }
  374. }
  375. [XmlIgnore()]
  376. [NinjaScriptProperty]
  377. [Display(Name = "Text font", Description = "Represents the text font for the displayed swing information.", GroupName = "Visualize patterns", Order = 1)]
  378. public SimpleFont TextFont
  379. {
  380. get { return abcTextFont; }
  381. set { abcTextFont = value; }
  382. }
  383. #endregion
  384. }
  385. }
RAW Paste Data