ccc : ChartStyle

Ninjatreader 8 ccc : ChartStyle
mail@pastecode.io avatar
unknown
csharp
2 years ago
13 kB
2
Indexable
Never
// 
// Copyright (C) 2015, NinjaTrader LLC <www.ninjatrader.com>.
// NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
//
#region Using declarations
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using SharpDX;
using SharpDX.Direct2D1;
using System;

//added
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using NinjaTrader.Gui;

#endregion

namespace NinjaTrader.NinjaScript.ChartStyles
{
	/// <summary>
	/// Sim22 for NT8b9 Mar 2016.
	/// </summary>
	public class ccc : ChartStyle
	{
		private object icon;

		public override int GetBarPaintWidth(int barWidth)
		{
			return 1 + 2 * (barWidth - 1) + 2 * (int) WidthOutline;
		}

		public override object Icon
		{
			get
			{
				if (icon == null)
				{
					icon = NinjaTrader.Gui.Tools.Icons.ChartChartStyle;
				}
				return icon;
			}
		}

		public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars)
		{
			Bars			bars			= chartBars.Bars;
			float			barWidth		= GetBarPaintWidth(BarWidthUI);
			Vector2			point0			= new Vector2();
			Vector2			point1			= new Vector2();
			RectangleF		rect			= new RectangleF();

			SharpDX.Direct2D1.StrokeStyleProperties ssPropsWick = new SharpDX.Direct2D1.StrokeStyleProperties();
			ssPropsWick.DashStyle								= DashStyleWick;
			SharpDX.Direct2D1.StrokeStyle styleWick 			= new SharpDX.Direct2D1.StrokeStyle(RenderTarget.Factory, ssPropsWick);
			SharpDX.Direct2D1.AntialiasMode oldAntialiasMode	= RenderTarget.AntialiasMode;
			RenderTarget.AntialiasMode							= SharpDX.Direct2D1.AntialiasMode.Aliased;
			
			for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++)
			{
				Brush		overriddenBarBrush		= chartControl.GetBarOverrideBrush(chartBars, idx);
				Brush		overriddenOutlineBrush	= chartControl.GetCandleOutlineOverrideBrush(chartBars, idx);
				Brush		upOutBrushDX			= UpOutBrush.ToDxBrush(RenderTarget);
				Brush		dnOutBrushDX			= DnOutBrush.ToDxBrush(RenderTarget);
				Brush		dojiBrushDX				= DojiBrush.ToDxBrush(RenderTarget);
				Brush upOutBrushWickDX = UpOutBrushWick.ToDxBrush(RenderTarget);
				Brush dnOutBrushWickDX = DnOutBrushWick.ToDxBrush(RenderTarget);
				Brush dojiBrushWickDX = DojiBrushWick.ToDxBrush(RenderTarget);
				
				Brush upOutBrushWickDX1 = UpOutBrushWick1.ToDxBrush(RenderTarget);
				Brush dnOutBrushWickDX1 = DnOutBrushWick1.ToDxBrush(RenderTarget);


				double		closeValue				= bars.GetClose(idx);
				float		closeY					= chartScale.GetYByValue(closeValue);
				float		highY					= chartScale.GetYByValue(bars.GetHigh(idx));
				float		lowY					= chartScale.GetYByValue(bars.GetLow(idx));
				double		openValue				= bars.GetOpen(idx);
				float		openY					= chartScale.GetYByValue(openValue);
				float		x						= chartControl.GetXByBarIndex(chartBars, idx);

				
				if (Math.Abs(openY - closeY) < 0.0000001)
				{
					// Doji Line 
					point0.X = x - barWidth * 0.5f;
					point0.Y = closeY;
					point1.X = x + barWidth * 0.5f;
					point1.Y = closeY;

					TransformBrush(overriddenOutlineBrush ?? dojiBrushDX, new RectangleF(point0.X, point0.Y - WidthOutline, barWidth, WidthOutline));
					RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? dojiBrushDX, WidthOutline);
				}
				else
				{
					// Candle
					Brush brush	= closeValue >= openValue ? UpBrushDX : DownBrushDX;
					brush.Opacity	= Opacity * 0.01f;
					rect.X		= x - barWidth * 0.5f + 0.5f;
					rect.Y		= Math.Min(closeY, openY);
					rect.Width	= barWidth - 1;
					rect.Height	= Math.Max(openY, closeY) - Math.Min(closeY, openY);
					TransformBrush(overriddenBarBrush ?? brush, rect);
					TransformBrush(overriddenOutlineBrush ?? (closeValue > openValue ? upOutBrushDX : dnOutBrushDX), rect);
					RenderTarget.FillRectangle(rect, overriddenBarBrush ?? brush);
					RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? (closeValue > openValue ? upOutBrushDX : dnOutBrushDX), WidthOutline);

				}

				// High wick
				if (highY < Math.Min(openY, closeY))
				{
					point0.X = x;
					point0.Y = highY;
					point1.X = x;
					point1.Y = Math.Min(openY, closeY);
					TransformBrush(overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX : dnOutBrushWickDX), new RectangleF(point0.X - WidthWick, point0.Y, WidthWick, point1.Y - point0.Y));
					RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX : dnOutBrushWickDX), WidthWick, styleWick);
				}
				
				// High wick
				if (highY < Math.Min(openY, closeY))
				{
					point0.X = x;
					point0.Y = highY;
					point1.X = x;
					point1.Y = Math.Min(openY, closeY);
					TransformBrush(overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX1 : dnOutBrushWickDX1), new RectangleF(point0.X - WidthWick, point0.Y, WidthWick, point1.Y - point0.Y));
					RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX1 : dnOutBrushWickDX1), WidthWick, styleWick);
				}

				// Low wick
				if (lowY > Math.Max(openY, closeY))
				{
					point0.X = x;
					point0.Y = lowY;
					point1.X = x;
					point1.Y = Math.Max(openY, closeY);
					TransformBrush(overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX : dnOutBrushWickDX), new RectangleF(point1.X - WidthWick, point1.Y, WidthWick, point0.Y - point1.Y));
					RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX : dnOutBrushWickDX), WidthWick, styleWick);
				}

				// Low wick
				if (lowY > Math.Max(openY, closeY))
				{
					point0.X = x;
					point0.Y = lowY;
					point1.X = x;
					point1.Y = Math.Max(openY, closeY);
					TransformBrush(overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX1 : dnOutBrushWickDX1), new RectangleF(point1.X - WidthWick, point1.Y, WidthWick, point0.Y - point1.Y));
					RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? (closeValue == openValue ? dojiBrushWickDX : closeValue > openValue ? upOutBrushWickDX1 : dnOutBrushWickDX1), WidthWick, styleWick);
				}
				
				upOutBrushDX.Dispose();
				dnOutBrushDX.Dispose();
				dojiBrushDX.Dispose();
				upOutBrushWickDX.Dispose();
				dnOutBrushWickDX.Dispose();
				dojiBrushWickDX.Dispose();
				
				upOutBrushWickDX1.Dispose();
				dnOutBrushWickDX1.Dispose();
			}
			styleWick.Dispose();
			RenderTarget.AntialiasMode = oldAntialiasMode;

		}

		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Name				= @"ccc";
				Description			= "Plots customizable outline, wicks and bar opacity Candlesticks";
				ChartStyleType		= (ChartStyleType) 11227;
				this.UpBrush		= System.Windows.Media.Brushes.Pink;
				this.DownBrush 		= System.Windows.Media.Brushes.Yellow;
				this.UpOutBrush 	= System.Windows.Media.Brushes.Pink;
				this.DnOutBrush 	= System.Windows.Media.Brushes.Cyan;
				this.DojiBrush 		= System.Windows.Media.Brushes.Gray;
				this.UpOutBrushWick = System.Windows.Media.Brushes.Lime;
				this.DnOutBrushWick = System.Windows.Media.Brushes.Red;
				this.DojiBrushWick = System.Windows.Media.Brushes.Gray;
				
				this.UpOutBrushWick1 = System.Windows.Media.Brushes.Lime;
				this.DnOutBrushWick1 = System.Windows.Media.Brushes.Red;
				
				WidthOutline		= 1f;
				WidthWick			= 1f;
				Opacity				= 85;
			}
			else if (State == State.Configure)
			{
				SetPropertyName("BarWidth",		Custom.Resource.NinjaScriptChartStyleBarWidth);
				SetPropertyName("DownBrush",	"Colors - Down bar");
				SetPropertyName("UpBrush",		"Colors - Up bar");
				
				Properties.Remove(Properties.Find("Stroke", true));
				Properties.Remove(Properties.Find("Stroke2", true));
				
				UpBrush.Freeze();
				DownBrush.Freeze();
				UpOutBrush.Freeze();
				DnOutBrush.Freeze();
				DojiBrush.Freeze();
				UpOutBrushWick.Freeze();
				DnOutBrushWick.Freeze();
				DojiBrushWick.Freeze();
				
				UpOutBrushWick1.Freeze();
				DnOutBrushWick1.Freeze();
			}
		}
		#region Properties

		[XmlIgnore]
		[Display(Name="Colors - Down bar outline", Description="Outline color of down bar", Order=1, GroupName="Chart Style")]
		public System.Windows.Media.Brush DnOutBrush
		{ get; set; }

		[Browsable(false)]
		public string DnOutBrushSerializable
		{
			get { return Serialize.BrushToString(DnOutBrush); }
			set { DnOutBrush = Serialize.StringToBrush(value); }
		}
		
		[XmlIgnore]
		[Display(Name="Colors - Up bar outline", Description="Outline color of up bar", Order=2, GroupName="Chart Style")]
		public System.Windows.Media.Brush UpOutBrush
		{ get; set; }

		[Browsable(false)]
		public string UpOutBrushSerializable
		{
			get { return Serialize.BrushToString(UpOutBrush); }
			set { UpOutBrush = Serialize.StringToBrush(value); }
		}
	
		[XmlIgnore]
		[Display(Name="Colors - Doji outline", Description="Color of doji bar", Order=3, GroupName="Chart Style")]
		public System.Windows.Media.Brush DojiBrush
		{ get; set; }

		[Browsable(false)]
		public string DojiBrushSerializable
		{
			get { return Serialize.BrushToString(DojiBrush); }
			set { DojiBrush = Serialize.StringToBrush(value); }
		}
		
		[XmlIgnore]
		[Display(Name = "Colors - Down bar wick", Description = "Wick color of down bar", Order = 1, GroupName = "Chart Style")]
		public System.Windows.Media.Brush DnOutBrushWick
		{ get; set; }

		[Browsable(false)]
		public string DnOutBrushWickSerializable
		{
			get { return Serialize.BrushToString(DnOutBrushWick); }
			set { DnOutBrushWick = Serialize.StringToBrush(value); }
		}

		[XmlIgnore]
		[Display(Name = "Colors - Up bar wick", Description = "Wick color of up bar", Order = 2, GroupName = "Chart Style")]
		public System.Windows.Media.Brush UpOutBrushWick
		{ get; set; }

		[Browsable(false)]
		public string UpOutBrushWickSerializable
		{
			get { return Serialize.BrushToString(UpOutBrushWick); }
			set { UpOutBrushWick = Serialize.StringToBrush(value); }
		}

		[XmlIgnore]
		[Display(Name = "Colors - Doji wick", Description = "Wick color of doji bar", Order = 3, GroupName = "Chart Style")]
		public System.Windows.Media.Brush DojiBrushWick
		{ get; set; }

		[Browsable(false)]
		public string DojiBrushWickSerializable
		{
			get { return Serialize.BrushToString(DojiBrushWick); }
			set { DojiBrushWick = Serialize.StringToBrush(value); }
		}
		
		[XmlIgnore]
		[Display(Name = "Colors - Down bar wick", Description = "Wick color of down bar", Order = 1, GroupName = "Chart Style")]
		public System.Windows.Media.Brush DnOutBrushWick1
		{ get; set; }

		[Browsable(false)]
		public string DnOutBrushWick1Serializable
		{
			get { return Serialize.BrushToString(DnOutBrushWick1); }
			set { DnOutBrushWick1 = Serialize.StringToBrush(value); }
		}

		[XmlIgnore]
		[Display(Name = "Colors - Up bar wick", Description = "Wick color of up bar", Order = 2, GroupName = "Chart Style")]
		public System.Windows.Media.Brush UpOutBrushWick1
		{ get; set; }

		[Browsable(false)]
		public string UpOutBrushWick1Serializable
		{
			get { return Serialize.BrushToString(UpOutBrushWick1); }
			set { UpOutBrushWick1 = Serialize.StringToBrush(value); }
		}

		[Range(0, 100),NinjaScriptProperty]
		[Display(Name="Opacity % (0-100)", Description="Opacity of bar.", Order=4, GroupName="Chart Style")]
		public int Opacity
		{ get; set; }
		
		[Range(1.0f, 20.0f),NinjaScriptProperty]
		[Display(Name="Outline width", Description="Outline width.", Order=6, GroupName="Chart Style")]
		public float WidthOutline
		{ get; set; }
		
		[NinjaScriptProperty]
		[Display(Name="Wick dashstyle", Description="Wick dashstyle.", Order=7, GroupName="Chart Style")]
		public SharpDX.Direct2D1.DashStyle DashStyleWick
		{ get; set; }
		
		[Range(1.0f, 20.0f),NinjaScriptProperty]
		[Display(Name="Wick width", Description="Wick width.", Order=8, GroupName="Chart Style")]
		public float WidthWick
		{ get; set; }
		
		#endregion
	}
}