custom indicator not displaying
custom indicator not displaying
12 Feb 2025, 09:54
i have created a basic indicator. Buils showed as successfull. when I attach to chart I can see the name of the indi in the panel but no data. Any suggestions.
Here is the code.
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class DTOscillator : Indicator
{
// Input parameters
[Parameter("Length RSI", DefaultValue = 8, MinValue = 8)]
public int LengthRSI { get; set; }
[Parameter("Length Stoch", DefaultValue = 5, MinValue = 5)]
public int LengthStoch { get; set; }
[Parameter("Smooth K", DefaultValue = 3, MinValue = 3)]
public int SmoothK { get; set; }
[Parameter("Smooth D", DefaultValue = 3, MinValue = 3)]
public int SmoothD { get; set; }
// Output buffers
[Output("K", LineColor = "Yellow", PlotType = PlotType.Line, Thickness = 1)]
public IndicatorDataSeries K { get; set; }
[Output("D", LineColor = "Red", PlotType = PlotType.Line, Thickness = 2)]
public IndicatorDataSeries D { get; set; }
private RelativeStrengthIndex rsi;
protected override void Initialize()
{
// Initialize RSI
rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, LengthRSI);
// Initialize output buffers
K = CreateDataSeries();
D = CreateDataSeries();
}
public override void Calculate(int index)
{
if (index < LengthStoch)
return;
// Calculate RSI
double rsiValue = rsi.Result[index];
// Calculate min and max RSI over the LengthStoch period
double minRSI = rsi.Result.Minimum(LengthStoch);
double maxRSI = rsi.Result.Maximum(LengthStoch);
// Avoid division by zero
double stochRSI = (maxRSI - minRSI) != 0 ? (rsiValue - minRSI) / (maxRSI - minRSI) * 100 : 0;
// Calculate K (SMA of Stochastic RSI)
K[index] = CalculateSMA(stochRSI, index, SmoothK);
// Calculate D (SMA of K)
D[index] = CalculateSMA(K, index, SmoothD);
// Ensure valid values
if (double.IsNaN(K[index])) K[index] = 0;
if (double.IsNaN(D[index])) D[index] = 0;
}
private double CalculateSMA(double value, int index, int period)
{
if (index < period - 1)
return double.NaN; // Not enough data to calculate SMA
double sum = 0;
for (int i = 0; i < period; i++)
{
sum += value;
}
return sum / period;
}
private double CalculateSMA(IndicatorDataSeries series, int index, int period)
{
if (index < period - 1)
return double.NaN; // Not enough data to calculate SMA
double sum = 0;
for (int i = 0; i < period; i++)
{
sum += series[index - i];
}
return sum / period;
}
}
}
firemyst
18 Feb 2025, 04:55
You don't need to do this:
// Initialize output buffers
K = CreateDataSeries();
D = CreateDataSeries();
It's taken care for you when you declare those using the [Output … ] directive as you have.
@firemyst