Advertisement
Guest User

Untitled

a guest
May 4th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.52 KB | None | 0 0
  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.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Xml.Serialization;
  14. using NinjaTrader.Cbi;
  15. using NinjaTrader.Gui;
  16. using NinjaTrader.Gui.Chart;
  17. using NinjaTrader.Gui.SuperDom;
  18. using NinjaTrader.Gui.Tools;
  19. using NinjaTrader.Data;
  20. using NinjaTrader.NinjaScript;
  21. using NinjaTrader.Core.FloatingPoint;
  22. using NinjaTrader.NinjaScript.DrawingTools;
  23. using SharpDX;
  24. using SharpDX.Direct2D1;
  25. using SharpDX.DirectWrite;
  26. #endregion
  27.  
  28. namespace NinjaTrader.NinjaScript.Indicators
  29. {
  30. public class LabeledHorizontalLines : Indicator
  31. {
  32. private System.Windows.Media.Brush textBrush;
  33. private HorizLabelSideType labelSide = HorizLabelSideType.Right;
  34. private HorizLabelAreaType labelArea = HorizLabelAreaType.Above;
  35. private float fontSize = 20.0f ;
  36. private Boolean _init = false;
  37. private string _priceFormat;
  38. private ArrayList lineInfoArray = new ArrayList();
  39. HorizontalLine l1;
  40.  
  41. protected override void OnStateChange()
  42. {
  43. if (State == State.SetDefaults)
  44. {
  45. Description = @"";
  46. Name = "LabeledHorizontalLines";
  47. Calculate = Calculate.OnEachTick;
  48. IsOverlay = true;
  49. DisplayInDataBox = false;
  50. DrawOnPricePanel = true;
  51. // DrawHorizontalGridLines = true; checkear esto
  52. DrawHorizontalGridLines = true;
  53. DrawVerticalGridLines = true;
  54. PaintPriceMarkers = true;
  55. ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
  56. //Disable this property if your indicator requires custom values that cumulate with each new market data event.
  57. //See Help Guide for additional information.
  58. IsSuspendedWhileInactive = true;
  59. TextBrush = System.Windows.Media.Brushes.DodgerBlue;
  60. }
  61. else if (State == State.Configure)
  62. {
  63. }
  64. }
  65.  
  66. protected override void OnBarUpdate()
  67. {
  68. int j ;
  69. String beginMessage = Instrument.FullName + " - Horizontal Line Price Triggered on " ;
  70.  
  71. // Determine the Period Value
  72. switch ( BarsPeriod.BarsPeriodType )
  73. {
  74. case BarsPeriodType.Minute:
  75. beginMessage += BarsPeriod.Value + "M Timeframe @ " ;
  76. break ;
  77.  
  78. case BarsPeriodType.Day:
  79. beginMessage += BarsPeriod.Value + "D Timeframe @ " ;
  80. break ;
  81.  
  82. case BarsPeriodType.Week:
  83. beginMessage += BarsPeriod.Value + "W Timeframe @ " ;
  84. break ;
  85.  
  86. case BarsPeriodType.Month:
  87. beginMessage += BarsPeriod.Value + "MO Timeframe @ " ;
  88. break ;
  89.  
  90. case BarsPeriodType.Year:
  91. beginMessage += BarsPeriod.Value + "Y Timeframe @ " ;
  92. break ;
  93.  
  94. case BarsPeriodType.Tick:
  95. beginMessage += BarsPeriod.Value + "Tick Timeframe @ " ;
  96. break ;
  97.  
  98. }
  99. if (!_init)
  100. {
  101. _init = true;
  102. int Digits = 0;
  103. if (TickSize.ToString().StartsWith("1E-"))
  104. {
  105. Digits=Convert.ToInt32(TickSize.ToString().Substring(3));
  106. }
  107. else if (TickSize.ToString().IndexOf(".")>0)
  108. {
  109. Digits=TickSize.ToString().Substring(TickSize.ToString().IndexOf("."),TickSize.ToString().Length-1).Length-1;
  110. }
  111. _priceFormat = string.Format("F{0}", Digits);
  112.  
  113. // Build a larger font size
  114. //largerFont = new Font( ChartControl.Font.Name, fontSize, FontStyle.Bold ) ;
  115. }
  116.  
  117.  
  118. }
  119.  
  120. protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
  121. {
  122. base.OnRender(chartControl, chartScale);
  123.  
  124. SharpDX.Vector2 startPoint;
  125. SharpDX.Vector2 endPoint;
  126.  
  127. startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
  128. endPoint = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);
  129.  
  130. SharpDX.Vector2 startPoint1 = new System.Windows.Point(ChartPanel.X, ChartPanel.Y + ChartPanel.H).ToVector2();
  131. SharpDX.Vector2 endPoint1 = new System.Windows.Point(ChartPanel.X + ChartPanel.W, ChartPanel.Y).ToVector2();
  132.  
  133. float width = endPoint.X - startPoint.X;
  134. float height = endPoint.Y - startPoint.Y;
  135.  
  136. SharpDX.Vector2 center = (startPoint + endPoint) / 2;
  137.  
  138. SharpDX.Direct2D1.Brush textBrushDx;
  139.  
  140. textBrushDx = textBrush.ToDxBrush(RenderTarget);
  141.  
  142.  
  143. NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);
  144.  
  145. SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();
  146.  
  147. SharpDX.DirectWrite.TextFormat textFormat2 =
  148. new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Century Gothic", SharpDX.DirectWrite.FontWeight.Bold,
  149. SharpDX.DirectWrite.FontStyle.Italic, fontSize);
  150.  
  151. foreach (Gui.NinjaScript.IChartObject thisObject in ChartPanel.ChartObjects)
  152. {
  153.  
  154. if(thisObject is NinjaTrader.NinjaScript.DrawingTools.HorizontalLine)
  155. {
  156.  
  157. l1 = thisObject as NinjaTrader.NinjaScript.DrawingTools.HorizontalLine;
  158.  
  159. SharpDX.DirectWrite.TextLayout textLayout2 =
  160. new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
  161. l1.StartAnchor.Price.ToString(_priceFormat), textFormat2, 400, textFormat1.FontSize);
  162.  
  163. if(LabelSide == HorizLabelSideType.Right)
  164. {
  165. if(labelArea == HorizLabelAreaType.Above)
  166. {
  167. SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
  168. ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y)-(float)textLayout2.Metrics.Height);
  169. RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
  170. }
  171. if(labelArea == HorizLabelAreaType.Below)
  172. {
  173. SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
  174. ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y));
  175. RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
  176. }
  177. }
  178.  
  179. if(LabelSide == HorizLabelSideType.Left)
  180. {
  181. if(labelArea == HorizLabelAreaType.Above)
  182. {
  183. SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(0,
  184. ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y)-(float)textLayout2.Metrics.Height);
  185. RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
  186. }
  187. if(labelArea == HorizLabelAreaType.Below)
  188. {
  189. SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(0,
  190. ChartPanel.Y + ((float)l1.StartAnchor.GetPoint(chartControl, ChartPanel, chartScale).Y));
  191. RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);
  192. }
  193. }
  194.  
  195. }
  196.  
  197.  
  198.  
  199. }
  200.  
  201. }
  202.  
  203. [Description("Set the size of the display font")]
  204. [Range(1, int.MaxValue), NinjaScriptProperty]
  205. [Display(ResourceType = typeof(Custom.Resource), Name = "Font Size", GroupName = "NinjaScriptParameters", Order = 0)]
  206. public float FontSize
  207. {
  208. get { return fontSize; }
  209. set { fontSize = value; }
  210. }
  211.  
  212. [Description("Display on the left side of the line or the right side")]
  213. [NinjaScriptProperty]
  214. [Display(ResourceType = typeof(Custom.Resource), Name = "Label Side", GroupName = "NinjaScriptParameters", Order = 0)]
  215. public HorizLabelSideType LabelSide
  216. {
  217. get { return labelSide; }
  218. set { labelSide = value; }
  219. }
  220.  
  221. [Description("Display the label Above or Below the line")]
  222. [NinjaScriptProperty]
  223. [Display(ResourceType = typeof(Custom.Resource), Name = "Above/Below", GroupName = "NinjaScriptParameters", Order = 0)]
  224. public HorizLabelAreaType LabelArea
  225. {
  226. get { return labelArea; }
  227. set { labelArea = value ; }
  228. }
  229.  
  230. [XmlIgnore]
  231. [Display(ResourceType = typeof(Custom.Resource), Name = "TextColor", GroupName = "NinjaScriptGeneral")]
  232. public System.Windows.Media.Brush TextBrush
  233. {
  234. get { return textBrush; }
  235. set { textBrush = value; }
  236. }
  237.  
  238. [Browsable(false)]
  239. public string TextBrushSerialize
  240. {
  241. get { return Serialize.BrushToString(TextBrush); }
  242. set { TextBrush = Serialize.StringToBrush(value); }
  243. }
  244.  
  245.  
  246. }
  247. }
  248.  
  249. #region NinjaScript generated code. Neither change nor remove.
  250.  
  251. namespace NinjaTrader.NinjaScript.Indicators
  252. {
  253. public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
  254. {
  255. private LabeledHorizontalLines[] cacheLabeledHorizontalLines;
  256. public LabeledHorizontalLines LabeledHorizontalLines(float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  257. {
  258. return LabeledHorizontalLines(Input, fontSize, labelSide, labelArea);
  259. }
  260.  
  261. public LabeledHorizontalLines LabeledHorizontalLines(ISeries<double> input, float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  262. {
  263. if (cacheLabeledHorizontalLines != null)
  264. for (int idx = 0; idx < cacheLabeledHorizontalLines.Length; idx++)
  265. if (cacheLabeledHorizontalLines[idx] != null && cacheLabeledHorizontalLines[idx].FontSize == fontSize && cacheLabeledHorizontalLines[idx].LabelSide == labelSide && cacheLabeledHorizontalLines[idx].LabelArea == labelArea && cacheLabeledHorizontalLines[idx].EqualsInput(input))
  266. return cacheLabeledHorizontalLines[idx];
  267. return CacheIndicator<LabeledHorizontalLines>(new LabeledHorizontalLines(){ FontSize = fontSize, LabelSide = labelSide, LabelArea = labelArea }, input, ref cacheLabeledHorizontalLines);
  268. }
  269. }
  270. }
  271.  
  272. namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
  273. {
  274. public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
  275. {
  276. public Indicators.LabeledHorizontalLines LabeledHorizontalLines(float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  277. {
  278. return indicator.LabeledHorizontalLines(Input, fontSize, labelSide, labelArea);
  279. }
  280.  
  281. public Indicators.LabeledHorizontalLines LabeledHorizontalLines(ISeries<double> input , float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  282. {
  283. return indicator.LabeledHorizontalLines(input, fontSize, labelSide, labelArea);
  284. }
  285. }
  286. }
  287.  
  288. namespace NinjaTrader.NinjaScript.Strategies
  289. {
  290. public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
  291. {
  292. public Indicators.LabeledHorizontalLines LabeledHorizontalLines(float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  293. {
  294. return indicator.LabeledHorizontalLines(Input, fontSize, labelSide, labelArea);
  295. }
  296.  
  297. public Indicators.LabeledHorizontalLines LabeledHorizontalLines(ISeries<double> input , float fontSize, HorizLabelSideType labelSide, HorizLabelAreaType labelArea)
  298. {
  299. return indicator.LabeledHorizontalLines(input, fontSize, labelSide, labelArea);
  300. }
  301. }
  302. }
  303.  
  304. #endregion
  305.  
Tags: ddd
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement