ninjatrader/indicators/IndexTopN.cs

218 lines
8.0 KiB
C#
Raw Normal View History

#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
#endregion
//This namespace holds Indicators in this folder and is required. Do not change it.
namespace NinjaTrader.NinjaScript.Indicators
{
[CategoryOrder("Index Top N", 1)]
[CategoryOrder("Plots", 2)]
public class IndexTopN : Indicator
{
private const int PrimaryBars = 0;
private const int RegularTradingHoursBars = 1;
private const int ComponentBarsStartIndex = 2;
private TimeSpan RegularTradingHoursOpen;
private TimeSpan RegularTradingHoursClose;
private const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
// Regular expression to match the Slickcharts table row containing the components' symbols and index weightings.
private const string ComponentRegex = @"<tr>\s*<td>\d+</td>\s*<td><a href=""/symbol/[A-Z.]+?"">(.+?)</a></td>\s*<td><a href=""/symbol/[A-Z.]+?"">([A-Z.]+?)</a></td>\s*<td>(.+?)%</td>";
private List<string> Components = new List<string>();
private Dictionary<string, double> ComponentPrices = new Dictionary<string, double>();
private Dictionary<string, double> ComponentWeightings = new Dictionary<string, double>();
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Plots the top N components (weighted by market cap) of the specified index.";
Name = "Index Top N";
Calculate = Calculate.OnPriceChange;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
ScaleJustification = ScaleJustification.Overlay;
IsSuspendedWhileInactive = false;
NumComponents = 10;
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "Top N Index");
}
else if (State == State.Configure)
{
// For determining when RTH begins and ends.
AddDataSeries(Instrument.FullName,
new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, "US Equities RTH");
RetrieveComponentData();
foreach (var component in Components)
{
//Replace "." with "_" in component name to avoid issues with NinjaTrader's naming conventions and "BRK.B".
AddDataSeries(component.Replace(".", "_"));
}
}
else if (State == State.DataLoaded)
{
SessionIterator regularTradingHoursSession = new SessionIterator(BarsArray[RegularTradingHoursBars]);
RegularTradingHoursOpen = regularTradingHoursSession
.GetTradingDayBeginLocal(regularTradingHoursSession.ActualTradingDayExchange).TimeOfDay;
RegularTradingHoursClose = regularTradingHoursSession
.GetTradingDayEndLocal(regularTradingHoursSession.ActualTradingDayExchange).TimeOfDay;
}
else if (State == State.Historical)
{
SetZOrder(-1); // Display behind bars on chart.
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress == PrimaryBars && IsRegularTradingHours())
{
double indexValue = 0;
foreach (var component in Components)
{
if (ComponentPrices.ContainsKey(component) && ComponentWeightings.ContainsKey(component))
indexValue += ComponentPrices[component] * ComponentWeightings[component];
}
if (indexValue > 0)
Index[0] = indexValue;
}
else if (BarsInProgress >= ComponentBarsStartIndex)
{
string component = Components[BarsInProgress - ComponentBarsStartIndex];
ComponentPrices[component] = Closes[BarsInProgress][0];
}
}
private bool IsRegularTradingHours()
{
return Time[0].TimeOfDay > RegularTradingHoursOpen && Time[0].TimeOfDay <= RegularTradingHoursClose;
}
private void RetrieveComponentData()
{
const string url = "https://www.slickcharts.com/sp500";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);
var htmlContent = httpClient.GetStringAsync(url).Result;
int count = 0;
var matches = Regex.Matches(htmlContent, ComponentRegex);
foreach (Match match in matches)
{
if (count++ >= NumComponents) break;
var symbol = match.Groups[2].Value;
var percentage = match.Groups[3].Value;
Components.Add(symbol);
ComponentWeightings.Add(symbol, double.Parse(percentage));
}
}
public override string DisplayName
{
get { return "Index Top " + NumComponents; }
}
#region Plots
[Browsable(false)]
[XmlIgnore]
public Series<double> Index
{
get { return Values[0]; }
}
#endregion
#region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Number of Components", GroupName = "Index Top N", Order = 1)]
public int NumComponents { get; set; }
#endregion
}
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private IndexTopN[] cacheIndexTopN;
public IndexTopN IndexTopN(int numComponents)
{
return IndexTopN(Input, numComponents);
}
public IndexTopN IndexTopN(ISeries<double> input, int numComponents)
{
if (cacheIndexTopN != null)
for (int idx = 0; idx < cacheIndexTopN.Length; idx++)
if (cacheIndexTopN[idx] != null && cacheIndexTopN[idx].NumComponents == numComponents && cacheIndexTopN[idx].EqualsInput(input))
return cacheIndexTopN[idx];
return CacheIndicator<IndexTopN>(new IndexTopN(){ NumComponents = numComponents }, input, ref cacheIndexTopN);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.IndexTopN IndexTopN(int numComponents)
{
return indicator.IndexTopN(Input, numComponents);
}
public Indicators.IndexTopN IndexTopN(ISeries<double> input , int numComponents)
{
return indicator.IndexTopN(input, numComponents);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.IndexTopN IndexTopN(int numComponents)
{
return indicator.IndexTopN(Input, numComponents);
}
public Indicators.IndexTopN IndexTopN(ISeries<double> input , int numComponents)
{
return indicator.IndexTopN(input, numComponents);
}
}
}
#endregion