I’m currently working on a new indicator and have encountered the errors attached to this message.
Below is the complete script for your reference.
I look forward to your help in resolving this issue.
Thank you in advance!
using NinjaTrader.NinjaScript;
using NinjaTrader.NinjaScript.Strategies;
using NinjaTrader.NinjaScript.DrawingTools;
using NinjaTrader.Gui.Tools;
using System.Windows.Media;
namespace NinjaTrader.NinjaScript.Indicators
{
public class TotoScalping : Indicator
{
private double ph;
private double pl;
private ISeries<double> emaTrend;
private ISeries<double> emaFastTrend;
private double auxHigh = double.NaN;
private double auxLow = double.NaN;
private int auxHighBar = -1;
private int auxLowBar = -1;
private string lastHighLineKey;
private string lastLowLineKey;
private bool sessionActive;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
AddPlot(Brushes.Black, "EMA Trend");
AddPlot(Brushes.Red, "EMA Fast Trend");
}
else if (State == State.DataLoaded)
{
emaTrend = EMA(Close, 100);
emaFastTrend = EMA(Close, 25);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < 16) return;
// Pivotes altos y bajos
ph = High[HighestBar(High, 16)];
pl = Low[LowestBar(Low, 16)];
// Detectar si la sesión está activa
sessionActive = (Time[0].Hour >= 8 && Time[0].Hour < 10);
// Líneas de resistencia (pivote alto)
if (!double.IsNaN(ph) && emaFastTrend[0] > emaTrend[0])
{
if (!double.IsNaN(auxHigh) && ph < auxHigh)
{
if (!string.IsNullOrEmpty(lastHighLineKey))
RemoveDrawObject(lastHighLineKey);
lastHighLineKey = "ResistanceLine" + CurrentBar;
Draw.Line(this, lastHighLineKey, auxHighBar, auxHigh, CurrentBar, ph, Brushes.Black, DashStyleHelper.Dash, 2);
}
auxHigh = ph;
auxHighBar = CurrentBar;
}
// Líneas de soporte (pivote bajo)
if (!double.IsNaN(pl) && emaFastTrend[0] < emaTrend[0])
{
if (!double.IsNaN(auxLow) && pl > auxLow)
{
if (!string.IsNullOrEmpty(lastLowLineKey))
RemoveDrawObject(lastLowLineKey);
lastLowLineKey = "SupportLine" + CurrentBar;
Draw.Line(this, lastLowLineKey, auxLowBar, auxLow, CurrentBar, pl, Brushes.Black, DashStyleHelper.Dash, 2);
}
auxLow = pl;
auxLowBar = CurrentBar;
}
// Borrar las líneas cuando las EMAs se cruzan
if (CrossAbove(emaFastTrend, emaTrend, 1) || CrossBelow(emaFastTrend, emaTrend, 1))
{
auxHigh = double.NaN;
auxLow = double.NaN;
auxHighBar = -1;
auxLowBar = -1;
if (!string.IsNullOrEmpty(lastHighLineKey))
RemoveDrawObject(lastHighLineKey);
if (!string.IsNullOrEmpty(lastLowLineKey))
RemoveDrawObject(lastLowLineKey);
}
// Graficar las EMAs en el gráfico
Values[0][0] = emaTrend[0]; // EMA de 100 periodos
Values[1][0] = emaFastTrend[0]; // EMA de 25 periodos
// Etiquetas en los pivotes altos y bajos
if (emaTrend[0] > emaFastTrend[0])
{
Draw.Text(this, "LowLabel" + CurrentBar, "L", CurrentBar, pl, Brushes.White); // Etiqueta "Low"
}
else
{
Draw.Text(this, "HighLabel" + CurrentBar, "H", CurrentBar, ph, Brushes.White); // Etiqueta "High"
}
// Cambiar el color de fondo durante las horas activas de la sesión
if (sessionActive)
{
BackBrush = new SolidColorBrush(Color.FromArgb(95, 255, 255, 255)); // Fondo blanco transparente
}
}
}
}
region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private TotoScalping[] cacheTotoScalping;
public TotoScalping TotoScalping()
{
return TotoScalping(Input);
}
public TotoScalping TotoScalping(ISeries<double> input)
{
if (cacheTotoScalping != null)
for (int idx = 0; idx < cacheTotoScalping.Length; idx++)
if (cacheTotoScalping[idx] != null && cacheTotoScalping[idx].EqualsInput(input))
return cacheTotoScalping[idx];
return CacheIndicator<TotoScalping>(new TotoScalping(), input, ref cacheTotoScalping);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.TotoScalping TotoScalping()
{
return indicator.TotoScalping(Input);
}
public Indicators.TotoScalping TotoScalping(ISeries<double> input )
{
return indicator.TotoScalping(input);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.TotoScalping TotoScalping()
{
return indicator.TotoScalping(Input);
}
public Indicators.TotoScalping TotoScalping(ISeries<double> input )
{
return indicator.TotoScalping(input);
}
}
}
#endregion

Comment