Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using declarations
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using System.Xml.Serialization;
- using NinjaTrader.Cbi;
- using NinjaTrader.Gui;
- using NinjaTrader.Gui.Chart;
- using NinjaTrader.Gui.SuperDom;
- using NinjaTrader.Data;
- using NinjaTrader.NinjaScript;
- using NinjaTrader.Core.FloatingPoint;
- using SharpDX;
- using SharpDX.Direct2D1;
- #endregion
- //This namespace holds Chart styles in this folder and is required. Do not change it.
- namespace NinjaTrader.NinjaScript.ChartStyles
- {
- public class EquiVolume : ChartStyle
- {
- private int smallWidthModifier = -8;
- private int midWidthModifier = 1;
- private int largeWidthModifier = 6;
- protected override void OnStateChange()
- {
- if (State == State.SetDefaults)
- {
- Description = @"EquiVolume Chart Style";
- Name = "EquiVolume";
- ChartStyleType = (ChartStyleType) 8;
- BarWidth = 2;
- }
- else if (State == State.Configure)
- {
- Properties.Remove(Properties.Find("Stroke", false));
- Properties.Remove(Properties.Find("Stroke2", false));
- SetPropertyName("DownBrush", "Down Bar Color");
- SetPropertyName("UpBrush", "Up Bar Color");
- SetPropertyName("BarWidthUI", "Base Width");
- }
- }
- public override int GetBarPaintWidth(int barWidth)
- {
- return 1 + 2 * (barWidth - 1) + 2 * (int)Math.Round(Stroke.Width);
- }
- 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();
- float highVol = 0;
- float smallWidth = barWidth + smallWidthModifier;
- float midWidth = barWidth + midWidthModifier;
- float largeWidth = barWidth + largeWidthModifier;
- float wickOffset = 0;
- float smallWidthTrigger = 0;
- float largeWidthTrigger = 0;
- // Set highVol outside of loop that paints bars
- for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++)
- {
- float curVol = bars.GetVolume(idx);
- if (curVol > highVol)
- highVol = curVol;
- smallWidthTrigger = highVol * (float)0.3333;
- largeWidthTrigger = highVol * (float)0.6666;
- }
- //Draw candles and wicks
- for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++)
- {
- Brush overriddenBarBrush = chartControl.GetBarOverrideBrush(chartBars, idx);
- Brush overriddenOutlineBrush = chartControl.GetCandleOutlineOverrideBrush(chartBars, idx);
- double closeValue = bars.GetClose(idx);
- float close = chartScale.GetYByValue(closeValue);
- float high = chartScale.GetYByValue(bars.GetHigh(idx));
- float low = chartScale.GetYByValue(bars.GetLow(idx));
- double openValue = bars.GetOpen(idx);
- float open = chartScale.GetYByValue(openValue);
- float x = chartControl.GetXByBarIndex(chartBars, idx);
- float curVol = bars.GetVolume(idx);
- if (Math.Abs(open - close) < 0.0000001)
- {
- // Line
- point0.X = x - barWidth * 0.5f;
- point0.Y = close;
- point1.X = x + barWidth * 0.5f;
- point1.Y = close;
- TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, new RectangleF(point0.X, point0.Y - Stroke.Width, barWidth, Stroke.Width));
- RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle);
- }
- else
- {
- // Candle
- Brush brush = closeValue >= openValue ? UpBrushDX : DownBrushDX;
- rect.X = (x - barWidth * 0.5f + 0.5f);
- rect.Y = Math.Min(close, open);
- if (curVol < smallWidthTrigger)
- {
- rect.Width = smallWidth;
- wickOffset = smallWidth;
- }
- else if (curVol > largeWidthTrigger)
- {
- rect.Width = largeWidth;
- wickOffset = largeWidth;
- }
- else
- {
- rect.Width = midWidth;
- wickOffset = midWidth;
- }
- rect.Height = Math.Max(open, close) - Math.Min(close, open);
- TransformBrush(overriddenBarBrush ?? brush, rect);
- TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, rect);
- RenderTarget.FillRectangle(rect, overriddenBarBrush ?? brush);
- RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke.BrushDX, Stroke.Width, Stroke.StrokeStyle);
- }
- // High wick
- if (high < Math.Min(open, close))
- {
- if (Math.Abs(open - close) < 0.0000001)
- {
- point0.X = x;
- point1.X = x;
- }
- else
- {
- point0.X = rect.X + wickOffset / 2;
- point1.X = rect.X + wickOffset / 2;
- }
- point0.Y = high;
- point1.Y = Math.Min(open, close);
- TransformBrush(overriddenOutlineBrush ?? Stroke2.BrushDX, new RectangleF(point0.X - Stroke2.Width, point0.Y, Stroke2.Width, point1.Y - point0.Y));
- RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle);
- }
- // Low wick
- if (low > Math.Max(open, close))
- {
- if (Math.Abs(open - close) < 0.0000001)
- {
- point0.X = x;
- point1.X = x;
- }
- else
- {
- point0.X = rect.X + wickOffset / 2;
- point1.X = rect.X + wickOffset / 2;
- }
- point0.Y = low;
- point1.Y = Math.Max(open, close);
- TransformBrush(overriddenOutlineBrush ?? Stroke2.BrushDX, new RectangleF(point1.X - Stroke2.Width, point1.Y, Stroke2.Width, point0.Y - point1.Y));
- RenderTarget.DrawLine(point0, point1, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle);
- }
- }
- }
- [NinjaScriptProperty]
- [Display(ResourceType = typeof(Custom.Resource), Name = "Low Volume Modifier", GroupName = "NinjaScriptParameters", Order = 0)]
- public int SmallWidthModifier
- {
- get { return smallWidthModifier; }
- set { smallWidthModifier = value; }
- }
- [NinjaScriptProperty]
- [Display(ResourceType = typeof(Custom.Resource), Name = "Medium Volume Modifier", GroupName = "NinjaScriptParameters", Order = 1)]
- public int MidWidthModifier
- {
- get { return midWidthModifier; }
- set { midWidthModifier = value; }
- }
- [NinjaScriptProperty]
- [Display(ResourceType = typeof(Custom.Resource), Name = "High Volume Modifier", GroupName = "NinjaScriptParameters", Order = 2)]
- public int LargeWidthModifier
- {
- get { return largeWidthModifier; }
- set { largeWidthModifier = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement