Calculate index value using component prices looked up during chart bar processing

This commit is contained in:
moshferatu 2024-03-18 06:55:36 -07:00
parent 1f03d10e1f
commit 2b5f974f55

View File

@ -9,9 +9,9 @@ using System.Text.RegularExpressions;
using System.Windows.Media; using System.Windows.Media;
using System.Xml.Serialization; using System.Xml.Serialization;
using NinjaTrader.Cbi; using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui; using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.Chart;
using NinjaTrader.Data;
#endregion #endregion
//This namespace holds Indicators in this folder and is required. Do not change it. //This namespace holds Indicators in this folder and is required. Do not change it.
@ -34,7 +34,6 @@ namespace NinjaTrader.NinjaScript.Indicators
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 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 List<string> Components = new List<string>();
private Dictionary<string, double> ComponentPrices = new Dictionary<string, double>();
private Dictionary<string, double> ComponentWeightings = new Dictionary<string, double>(); private Dictionary<string, double> ComponentWeightings = new Dictionary<string, double>();
protected override void OnStateChange() protected override void OnStateChange()
@ -87,20 +86,16 @@ namespace NinjaTrader.NinjaScript.Indicators
if (BarsInProgress == PrimaryBars && IsRegularTradingHours()) if (BarsInProgress == PrimaryBars && IsRegularTradingHours())
{ {
double indexValue = 0; double indexValue = 0;
foreach (var component in Components) for (int i = ComponentBarsStartIndex; i < BarsArray.Length; i++)
{ {
if (ComponentPrices.ContainsKey(component) && ComponentWeightings.ContainsKey(component)) string component = Components[i - ComponentBarsStartIndex];
indexValue += ComponentPrices[component] * ComponentWeightings[component]; int bar = BarsArray[i].GetBar(Time[0]);
indexValue += BarsArray[i].GetClose(bar) * ComponentWeightings[component];
} }
if (indexValue > 0 && (ComponentPrices.Count == NumComponents)) if (indexValue > 0)
Index[0] = indexValue; Index[0] = indexValue;
} }
else if (BarsInProgress >= ComponentBarsStartIndex)
{
string component = Components[BarsInProgress - ComponentBarsStartIndex];
ComponentPrices[component] = Closes[BarsInProgress][0];
}
} }
private bool IsRegularTradingHours() private bool IsRegularTradingHours()