Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. //
  2. // Copyright (C) 2019, NinjaTrader LLC <www.ninjatrader.com>.
  3. // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
  4. //
  5. #region Using declarations
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.ComponentModel.DataAnnotations;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Xml.Serialization;
  17. using NinjaTrader.Cbi;
  18. using NinjaTrader.Gui;
  19. using NinjaTrader.Gui.Chart;
  20. using NinjaTrader.Gui.SuperDom;
  21. using NinjaTrader.Data;
  22. using NinjaTrader.NinjaScript;
  23. using NinjaTrader.Core.FloatingPoint;
  24. using NinjaTrader.NinjaScript.DrawingTools;
  25. #endregion
  26.  
  27. // This namespace holds indicators in this folder and is required. Do not change it.
  28. namespace NinjaTrader.NinjaScript.Indicators
  29. {
  30. /// <summary>
  31. /// The TSI (True Strength Index) is a momentum-based indicator, developed by William Blau.
  32. /// Designed to determine both trend and overbought/oversold conditions, the TSI is
  33. /// applicable to intraday time frames as well as long term trading.
  34. /// </summary>
  35. public class TSI : Indicator
  36. {
  37. private double constant1;
  38. private double constant2;
  39. private double constant3;
  40. private double constant4;
  41. private Series<double> fastEma;
  42. private Series<double> fastAbsEma;
  43. private Series<double> slowEma;
  44. private Series<double> slowAbsEma;
  45.  
  46. protected override void OnStateChange()
  47. {
  48. if (State == State.SetDefaults)
  49. {
  50. Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionTSI;
  51. Name = NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameTSI;
  52. Fast = 3;
  53. IsSuspendedWhileInactive = true;
  54. Slow = 14;
  55.  
  56. AddPlot(Brushes.DarkCyan, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNameTSI);
  57. }
  58. else if (State == State.Configure)
  59. {
  60. constant1 = (2.0 / (1 + Slow));
  61. constant2 = (1 - (2.0 / (1 + Slow)));
  62. constant3 = (2.0 / (1 + Fast));
  63. constant4 = (1 - (2.0 / (1 + Fast)));
  64. }
  65. else if (State == State.DataLoaded)
  66. {
  67. fastAbsEma = new Series<double>(this);
  68. fastEma = new Series<double>(this);
  69. slowAbsEma = new Series<double>(this);
  70. slowEma = new Series<double>(this);
  71. }
  72. }
  73.  
  74. protected override void OnBarUpdate()
  75. {
  76. if (CurrentBar == 0)
  77. {
  78. fastAbsEma[0] = 0;
  79. fastEma[0] = 0;
  80. slowAbsEma[0] = 0;
  81. slowEma[0] = 0;
  82. Value[0] = 0;
  83. }
  84. else
  85. {
  86. double momentum = Input[0] - Input[1];
  87. slowEma[0] = momentum * constant1 + constant2 * slowEma[1];
  88. fastEma[0] = slowEma[0] * constant3 + constant4 * fastEma[1];
  89. slowAbsEma[0] = Math.Abs(momentum) * constant1 + constant2 * slowAbsEma[1];
  90. fastAbsEma[0] = slowAbsEma[0] * constant3 + constant4 * fastAbsEma[1];
  91. Value[0] = fastAbsEma[0] == 0 ? 0 : 100 * fastEma[0] / fastAbsEma[0];
  92. }
  93. }
  94.  
  95. #region Properties
  96.  
  97. [Range(1, int.MaxValue), NinjaScriptProperty]
  98. [Display(ResourceType = typeof (Custom.Resource), Name = "Fast", GroupName = "NinjaScriptParameters", Order = 0)]
  99. public int Fast
  100. { get; set; }
  101.  
  102. [Range(1, int.MaxValue), NinjaScriptProperty]
  103. [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptParameters", Order = 1)]
  104. public int Slow
  105. { get; set; }
  106. #endregion
  107. }
  108. }
  109.  
  110. #region NinjaScript generated code. Neither change nor remove.
  111.  
  112. namespace NinjaTrader.NinjaScript.Indicators
  113. {
  114. public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
  115. {
  116. private TSI[] cacheTSI;
  117. public TSI TSI(int fast, int slow)
  118. {
  119. return TSI(Input, fast, slow);
  120. }
  121.  
  122. public TSI TSI(ISeries<double> input, int fast, int slow)
  123. {
  124. if (cacheTSI != null)
  125. for (int idx = 0; idx < cacheTSI.Length; idx++)
  126. if (cacheTSI[idx] != null && cacheTSI[idx].Fast == fast && cacheTSI[idx].Slow == slow && cacheTSI[idx].EqualsInput(input))
  127. return cacheTSI[idx];
  128. return CacheIndicator<TSI>(new TSI(){ Fast = fast, Slow = slow }, input, ref cacheTSI);
  129. }
  130. }
  131. }
  132.  
  133. namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
  134. {
  135. public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
  136. {
  137. public Indicators.TSI TSI(int fast, int slow)
  138. {
  139. return indicator.TSI(Input, fast, slow);
  140. }
  141.  
  142. public Indicators.TSI TSI(ISeries<double> input , int fast, int slow)
  143. {
  144. return indicator.TSI(input, fast, slow);
  145. }
  146. }
  147. }
  148.  
  149. namespace NinjaTrader.NinjaScript.Strategies
  150. {
  151. public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
  152. {
  153. public Indicators.TSI TSI(int fast, int slow)
  154. {
  155. return indicator.TSI(Input, fast, slow);
  156. }
  157.  
  158. public Indicators.TSI TSI(ISeries<double> input , int fast, int slow)
  159. {
  160. return indicator.TSI(input, fast, slow);
  161. }
  162. }
  163. }
  164.  
  165. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement