2024-03-11 19:44:31 +00:00
|
|
|
#region Using declarations
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2024-03-13 21:21:40 +00:00
|
|
|
using System.Linq;
|
2024-03-11 19:44:31 +00:00
|
|
|
using System.Net.Http;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
|
|
|
using NinjaTrader.Data;
|
|
|
|
#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;
|
2024-03-12 16:35:25 +00:00
|
|
|
private const int RegularTradingHoursBars = 1;
|
|
|
|
private const int ComponentBarsStartIndex = 2;
|
|
|
|
|
|
|
|
private TimeSpan RegularTradingHoursOpen;
|
|
|
|
private TimeSpan RegularTradingHoursClose;
|
|
|
|
|
2024-03-11 19:44:31 +00:00
|
|
|
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>();
|
2024-03-12 21:17:12 +00:00
|
|
|
|
2024-03-11 19:44:31 +00:00
|
|
|
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;
|
2024-03-12 21:15:39 +00:00
|
|
|
IsOverlay = true;
|
2024-03-11 19:44:31 +00:00
|
|
|
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)
|
|
|
|
{
|
2024-03-12 16:35:25 +00:00
|
|
|
// For determining when RTH begins and ends.
|
2024-03-12 21:17:12 +00:00
|
|
|
AddDataSeries(Instrument.FullName,
|
2024-03-12 16:35:25 +00:00
|
|
|
new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 }, "US Equities RTH");
|
2024-03-11 19:44:31 +00:00
|
|
|
|
2024-03-12 16:35:25 +00:00
|
|
|
RetrieveComponentData();
|
2024-03-11 19:44:31 +00:00
|
|
|
foreach (var component in Components)
|
|
|
|
{
|
|
|
|
//Replace "." with "_" in component name to avoid issues with NinjaTrader's naming conventions and "BRK.B".
|
|
|
|
AddDataSeries(component.Replace(".", "_"));
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 16:35:25 +00:00
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
SessionIterator regularTradingHoursSession = new SessionIterator(BarsArray[RegularTradingHoursBars]);
|
|
|
|
RegularTradingHoursOpen = regularTradingHoursSession
|
|
|
|
.GetTradingDayBeginLocal(regularTradingHoursSession.ActualTradingDayExchange).TimeOfDay;
|
|
|
|
RegularTradingHoursClose = regularTradingHoursSession
|
|
|
|
.GetTradingDayEndLocal(regularTradingHoursSession.ActualTradingDayExchange).TimeOfDay;
|
|
|
|
}
|
2024-03-12 21:17:12 +00:00
|
|
|
else if (State == State.Historical)
|
|
|
|
{
|
|
|
|
SetZOrder(-1); // Display behind bars on chart.
|
|
|
|
}
|
2024-03-11 19:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
2024-03-12 16:35:25 +00:00
|
|
|
if (BarsInProgress == PrimaryBars && IsRegularTradingHours())
|
2024-03-11 19:44:31 +00:00
|
|
|
{
|
|
|
|
double indexValue = 0;
|
|
|
|
foreach (var component in Components)
|
|
|
|
{
|
|
|
|
if (ComponentPrices.ContainsKey(component) && ComponentWeightings.ContainsKey(component))
|
|
|
|
indexValue += ComponentPrices[component] * ComponentWeightings[component];
|
|
|
|
}
|
2024-03-12 21:18:00 +00:00
|
|
|
|
|
|
|
if (indexValue > 0 && (ComponentPrices.Count == NumComponents))
|
2024-03-11 19:44:31 +00:00
|
|
|
Index[0] = indexValue;
|
|
|
|
}
|
2024-03-12 16:35:25 +00:00
|
|
|
else if (BarsInProgress >= ComponentBarsStartIndex)
|
2024-03-11 19:44:31 +00:00
|
|
|
{
|
2024-03-12 16:35:25 +00:00
|
|
|
string component = Components[BarsInProgress - ComponentBarsStartIndex];
|
2024-03-11 19:44:31 +00:00
|
|
|
ComponentPrices[component] = Closes[BarsInProgress][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 16:35:25 +00:00
|
|
|
private bool IsRegularTradingHours()
|
|
|
|
{
|
|
|
|
return Time[0].TimeOfDay > RegularTradingHoursOpen && Time[0].TimeOfDay <= RegularTradingHoursClose;
|
|
|
|
}
|
|
|
|
|
2024-03-11 19:44:31 +00:00
|
|
|
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));
|
|
|
|
}
|
2024-03-13 21:21:40 +00:00
|
|
|
|
|
|
|
// Normalize the component weightings to add up to 1.
|
|
|
|
double totalWeighting = ComponentWeightings.Values.Sum();
|
|
|
|
foreach (var component in Components)
|
|
|
|
{
|
|
|
|
ComponentWeightings[component] /= totalWeighting;
|
|
|
|
}
|
2024-03-11 19:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|