Advertisement
rdusnr

Untitled

Sep 28th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.01 KB | None | 0 0
  1. #region Using declarations
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.ComponentModel.DataAnnotations;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Input;
  11.  
  12. using System.Xml.Serialization;
  13. using NinjaTrader.Cbi;
  14. using NinjaTrader.Gui;
  15. using NinjaTrader.Gui.Chart;
  16. using NinjaTrader.Gui.SuperDom;
  17. using NinjaTrader.Data;
  18. using NinjaTrader.NinjaScript;
  19. using NinjaTrader.Core.FloatingPoint;
  20.  
  21. using SharpDX;
  22. using SharpDX.Direct2D1;
  23.  
  24.  
  25. #endregion
  26.  
  27. //This namespace holds Chart styles in this folder and is required. Do not change it.
  28. namespace NinjaTrader.NinjaScript.ChartStyles
  29. {
  30. public class EquiVolume : ChartStyle
  31. {
  32.  
  33. private int smallWidthModifier = -8;
  34. private int midWidthModifier = 1;
  35. private int largeWidthModifier = 6;
  36.  
  37. protected override void OnStateChange()
  38. {
  39. if (State == State.SetDefaults)
  40. {
  41. Description = @"EquiVolume Chart Style";
  42. Name = "EquiVolume";
  43. ChartStyleType = (ChartStyleType) 8;
  44. BarWidth = 2;
  45. }
  46. else if (State == State.Configure)
  47. {
  48. Properties.Remove(Properties.Find("Stroke", false));
  49. Properties.Remove(Properties.Find("Stroke2", false));
  50.  
  51. SetPropertyName("DownBrush", "Down Bar Color");
  52. SetPropertyName("UpBrush", "Up Bar Color");
  53. SetPropertyName("BarWidthUI", "Base Width");
  54. }
  55. }
  56.  
  57. public override int GetBarPaintWidth(int barWidth)
  58. {
  59. return 1 + 2 * (barWidth - 1) + 2 * (int)Math.Round(Stroke.Width);
  60. }
  61.  
  62. public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars)
  63. {
  64. Bars bars = chartBars.Bars;
  65. float barWidth = GetBarPaintWidth(BarWidthUI);
  66. Vector2 point0 = new Vector2();
  67. Vector2 point1 = new Vector2();
  68. RectangleF rect = new RectangleF();
  69.  
  70. float highVol = 0;
  71. float smallWidth = barWidth + smallWidthModifier;
  72. float midWidth = barWidth + midWidthModifier;
  73. float largeWidth = barWidth + largeWidthModifier;
  74. float wickOffset = 0;
  75.  
  76. float smallWidthTrigger = 0;
  77. float largeWidthTrigger = 0;
  78.  
  79. // Set highVol outside of loop that paints bars
  80. for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++)
  81. {
  82. float curVol = bars.GetVolume(idx);
  83.  
  84. if (curVol > highVol)
  85. highVol = curVol;
  86.  
  87. smallWidthTrigger = highVol * (float)0.3333;
  88. largeWidthTrigger = highVol * (float)0.6666;
  89. }
  90.  
  91. //Draw candles and wicks
  92. for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++)
  93. {
  94. Brush overriddenBarBrush = chartControl.GetBarOverrideBrush(chartBars, idx);
  95. Brush overriddenOutlineBrush = chartControl.GetCandleOutlineOverrideBrush(chartBars, idx);
  96. double closeValue = bars.GetClose(idx);
  97. float close = chartScale.GetYByValue(closeValue);
  98. float high = chartScale.GetYByValue(bars.GetHigh(idx));
  99. float low = chartScale.GetYByValue(bars.GetLow(idx));
  100. double openValue = bars.GetOpen(idx);
  101. float open = chartScale.GetYByValue(openValue);
  102. float x = chartControl.GetXByBarIndex(chartBars, idx);
  103. float curVol = bars.GetVolume(idx);
  104.  
  105. if (Math.Abs(open - close) < 0.0000001)
  106. {
  107. // Line
  108. point0.X = x - barWidth * 0.5f;
  109. point0.Y = close;
  110. point1.X = x + barWidth * 0.5f;
  111. point1.Y = close;
  112. TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width));
  113. RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle);
  114. }
  115. else
  116. {
  117. // Candle
  118. Brush brush = closeValue >= openValue ? UpBrushDX : DownBrushDX;
  119. rect.X = (x - barWidth * 0.5f + 0.5f);
  120. rect.Y = Math.Min(close, open);
  121.  
  122. if (curVol < smallWidthTrigger)
  123. {
  124. rect.Width = smallWidth;
  125. wickOffset = smallWidth;
  126. }
  127. else if (curVol > largeWidthTrigger)
  128. {
  129. rect.Width = largeWidth;
  130. wickOffset = largeWidth;
  131. }
  132. else
  133. {
  134. rect.Width = midWidth;
  135. wickOffset = midWidth;
  136. }
  137.  
  138. rect.Height = Math.Max(open, close) - Math.Min(close, open);
  139. TransformBrush(overriddenBarBrush ?? brush, rect);
  140. TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, rect);
  141. RenderTarget.FillRectangle(rect, overriddenBarBrush ?? brush);
  142. RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle);
  143. }
  144.  
  145. // High wick
  146. if (high < Math.Min(open, close))
  147. {
  148. if (Math.Abs(open - close) < 0.0000001)
  149. {
  150. point0.X = x;
  151. point1.X = x;
  152. }
  153. else
  154. {
  155. point0.X = rect.X + wickOffset / 2;
  156. point1.X = rect.X + wickOffset / 2;
  157. }
  158. point0.Y = high;
  159. point1.Y = Math.Min(open, close);
  160. TransformBrush(overriddenOutlineBrush ?? Stroke2.BrushDX, new RectangleF(point0.X - Stroke2.Width, point0.Y, Stroke2.Width, point1.Y - point0.Y));
  161. RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle);
  162. }
  163.  
  164. // Low wick
  165. if (low > Math.Max(open, close))
  166. {
  167. if (Math.Abs(open - close) < 0.0000001)
  168. {
  169. point0.X = x;
  170. point1.X = x;
  171. }
  172. else
  173. {
  174. point0.X = rect.X + wickOffset / 2;
  175. point1.X = rect.X + wickOffset / 2;
  176. }
  177. point0.Y = low;
  178. point1.Y = Math.Max(open, close);
  179. TransformBrush(overriddenOutlineBrush ?? Stroke2.BrushDX, new RectangleF(point1.X - Stroke2.Width, point1.Y, Stroke2.Width, point0.Y - point1.Y));
  180. RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle);
  181. }
  182. }
  183. }
  184. [NinjaScriptProperty]
  185. [Display(ResourceType = typeof(Custom.Resource), Name = "Low Volume Modifier", GroupName = "NinjaScriptParameters", Order = 0)]
  186. public int SmallWidthModifier
  187. {
  188. get { return smallWidthModifier; }
  189. set { smallWidthModifier = value; }
  190. }
  191.  
  192. [NinjaScriptProperty]
  193. [Display(ResourceType = typeof(Custom.Resource), Name = "Medium Volume Modifier", GroupName = "NinjaScriptParameters", Order = 1)]
  194. public int MidWidthModifier
  195. {
  196. get { return midWidthModifier; }
  197. set { midWidthModifier = value; }
  198. }
  199.  
  200. [NinjaScriptProperty]
  201. [Display(ResourceType = typeof(Custom.Resource), Name = "High Volume Modifier", GroupName = "NinjaScriptParameters", Order = 2)]
  202. public int LargeWidthModifier
  203. {
  204. get { return largeWidthModifier; }
  205. set { largeWidthModifier = value; }
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement