#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Linq;
#endregion
namespace NinjaTrader.Indicator
{
[Description("David Cline : 'Candlesticks, Condensed' - TASC February 2015")]
public class CondensedCandlesticks : Indicator
{
#region Variables
private int segmentCount = 6;
private int period = 10;
private int rankLevels = 10;
private float textSize = 8;
private double segmentDivisor = 0;
private int rightSideMarginPrevious = 0;
private bool buttonsloaded = false;
private CondensedCandlesticksSortOrder sortOrder = CondensedCandlesticksSortOrder.Descending;
private Dictionary<string, Dictionary<string, double>> candlePatterns = new Dictionary<string, Dictionary<string, double>>();
private Dictionary<string, Dictionary<string, float>> candleMetrics = new Dictionary<string, Dictionary<string, float>>();
private Dictionary<string, float> topRankSort = new Dictionary<string, float>();
private DataSeries rangeSeries;
private DataSeries hoSeries;
private DataSeries hcSeries;
private DataSeries olSeries;
private StringSeries patternSeries;
private string[] rankedPatternList = null;
private string topRankedPattern = "";
private System.Windows.Forms.Control[] controls = null;
private System.Windows.Forms.ToolStrip strip = null;
private System.Windows.Forms.ToolStripButton showHideRank = null;
private System.Windows.Forms.ToolStripButton updateRank = null;
private System.Windows.Forms.ToolStripSeparator space1 = null;
private System.Windows.Forms.ToolStripSeparator space2 = null;
private Font boldFont = new Font("Arial", 8, FontStyle.Bold);
private Font regularFont = new Font("Arial", 8);
#endregion
protected override void Initialize()
{
Overlay = true;
rangeSeries = new DataSeries(this);
hoSeries = new DataSeries(this);
hcSeries = new DataSeries(this);
olSeries = new DataSeries(this);
patternSeries = new StringSeries(this);
segmentDivisor = 100.0 / SegmentCount;
}
protected override void OnStartUp()
{
if (ChartControl != null)
{
controls = ChartControl.Controls.Find("tsrTool", false);
rightSideMarginPrevious = ChartControl.BarMarginRight;
}
if (controls != null && controls.Length > 0)
{
strip = (System.Windows.Forms.ToolStrip)controls[0];
showHideRank = new System.Windows.Forms.ToolStripButton("showHide");
showHideRank.Font = regularFont;
showHideRank.ForeColor = Color.White;
showHideRank.BackColor = Color.Green;
showHideRank.Text = "Show Rank";
updateRank = new System.Windows.Forms.ToolStripButton("update");
updateRank.Font = regularFont;
updateRank.ForeColor = Color.White;
updateRank.BackColor = Color.Green;
updateRank.Text = "Update Rank";
showHideRank.Click += showHideRank_Click;
updateRank.Click += updateRank_Click;
space1 = new System.Windows.Forms.ToolStripSeparator();
space2 = new System.Windows.Forms.ToolStripSeparator();
strip.Items.Add(space1);
strip.Items.Add(showHideRank);
buttonsloaded = true;
}
}
protected override void OnBarUpdate()
{
int span = Math.Min(CurrentBar, period);
rangeSeries.Set(High[0] - Low[0]);
double averageRange = SMA(rangeSeries, span)[0];
double rangePercent = (rangeSeries[0] / 100);
double rangeMultiplier = (rangeSeries[0] / averageRange);
if (rangeMultiplier > 1)
rangeMultiplier = 1;
hoSeries.Set(Math.Round(((((High[0] - Open[0]) / rangePercent) * rangeMultiplier) / segmentDivisor), 0));
hcSeries.Set(Math.Round(((((High[0] - Close[0]) / rangePercent) * rangeMultiplier) / segmentDivisor), 0));
olSeries.Set(Math.Round(((((High[0] - Low[0]) / rangePercent) * rangeMultiplier) / segmentDivisor), 0));
string candleSignature = hoSeries[0].ToString() + "-" + hcSeries[0].ToString() + "-" + olSeries[0].ToString();
PatternSeries[0] = candleSignature;
DrawText("calc" + CurrentBar, true, candleSignature, 0, Low[0] - 6 * TickSize, 0, Color.Blue, new Font("Arial", TextSize), StringAlignment.Center, Color.Empty, Color.White, 9);
double candleReturn = Close[0]-Close[span];
if (!candlePatterns.ContainsKey(candleSignature))
candlePatterns.Add(candleSignature, new Dictionary<string, double> () { {"Ups", 0}, {"Downs", 0}, {"Totals", 0}, {"Count", 0} });
candlePatterns[candleSignature]["Count"]++;
candlePatterns[candleSignature]["Totals"] += candleReturn;
Comment