Add the ability to toggle between the S&P 500 and NASDAQ indices

This commit is contained in:
moshferatu 2024-03-13 14:22:43 -07:00
parent 65c9072155
commit 1f03d10e1f

View File

@ -50,6 +50,7 @@ namespace NinjaTrader.NinjaScript.Indicators
ScaleJustification = ScaleJustification.Overlay;
IsSuspendedWhileInactive = false;
TrackingIndex = MarketIndex.SP500;
NumComponents = 10;
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "Top N Index");
@ -109,7 +110,10 @@ namespace NinjaTrader.NinjaScript.Indicators
private void RetrieveComponentData()
{
const string url = "https://www.slickcharts.com/sp500";
string url = "https://www.slickcharts.com/sp500";
if (TrackingIndex == MarketIndex.NASDAQ)
url = "https://www.slickcharts.com/nasdaq100";
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent);
var htmlContent = httpClient.GetStringAsync(url).Result;
@ -150,13 +154,24 @@ namespace NinjaTrader.NinjaScript.Indicators
#endregion
#region Properties
[NinjaScriptProperty]
[Display(Name = "Tracking Index", GroupName = "Index Top N", Order = 1)]
public MarketIndex TrackingIndex
{ get; set; }
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(Name = "Number of Components", GroupName = "Index Top N", Order = 1)]
[Display(Name = "Number of Components", GroupName = "Index Top N", Order = 2)]
public int NumComponents { get; set; }
#endregion
}
}
public enum MarketIndex
{
NASDAQ,
SP500
}
#region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
@ -164,18 +179,18 @@ namespace NinjaTrader.NinjaScript.Indicators
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private IndexTopN[] cacheIndexTopN;
public IndexTopN IndexTopN(int numComponents)
public IndexTopN IndexTopN(MarketIndex trackingIndex, int numComponents)
{
return IndexTopN(Input, numComponents);
return IndexTopN(Input, trackingIndex, numComponents);
}
public IndexTopN IndexTopN(ISeries<double> input, int numComponents)
public IndexTopN IndexTopN(ISeries<double> input, MarketIndex trackingIndex, 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))
if (cacheIndexTopN[idx] != null && cacheIndexTopN[idx].TrackingIndex == trackingIndex && cacheIndexTopN[idx].NumComponents == numComponents && cacheIndexTopN[idx].EqualsInput(input))
return cacheIndexTopN[idx];
return CacheIndicator<IndexTopN>(new IndexTopN(){ NumComponents = numComponents }, input, ref cacheIndexTopN);
return CacheIndicator<IndexTopN>(new IndexTopN(){ TrackingIndex = trackingIndex, NumComponents = numComponents }, input, ref cacheIndexTopN);
}
}
}
@ -184,14 +199,14 @@ namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.IndexTopN IndexTopN(int numComponents)
public Indicators.IndexTopN IndexTopN(MarketIndex trackingIndex, int numComponents)
{
return indicator.IndexTopN(Input, numComponents);
return indicator.IndexTopN(Input, trackingIndex, numComponents);
}
public Indicators.IndexTopN IndexTopN(ISeries<double> input , int numComponents)
public Indicators.IndexTopN IndexTopN(ISeries<double> input , MarketIndex trackingIndex, int numComponents)
{
return indicator.IndexTopN(input, numComponents);
return indicator.IndexTopN(input, trackingIndex, numComponents);
}
}
}
@ -200,14 +215,14 @@ namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.IndexTopN IndexTopN(int numComponents)
public Indicators.IndexTopN IndexTopN(MarketIndex trackingIndex, int numComponents)
{
return indicator.IndexTopN(Input, numComponents);
return indicator.IndexTopN(Input, trackingIndex, numComponents);
}
public Indicators.IndexTopN IndexTopN(ISeries<double> input , int numComponents)
public Indicators.IndexTopN IndexTopN(ISeries<double> input , MarketIndex trackingIndex, int numComponents)
{
return indicator.IndexTopN(input, numComponents);
return indicator.IndexTopN(input, trackingIndex, numComponents);
}
}
}