Indicator isn't plotting the right signals
Indicator isn't plotting the right signals
01 Sep 2024, 13:27
Hi, the code is giving signals based on the long ADXR indicator but I want it to give signals for the shorter ADXR indicator. Any help is appreciated!
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Indicators
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CustomEMAADXSignalIndicator : Indicator
{
private ExponentialMovingAverage _emaLong;
private ExponentialMovingAverage _emaShort;
private AverageDirectionalMovementIndexRating _adxrLong;
private AverageDirectionalMovementIndexRating _adxrShort;
// User-defined parameters for EMAs and ADXRs
[Parameter("EMA Long Period", DefaultValue = 200)]
public int EmaLongPeriod { get; set; }
[Parameter("EMA Short Period", DefaultValue = 50)]
public int EmaShortPeriod { get; set; }
[Parameter("ADXR Long Period", DefaultValue = 14)]
public int AdxrLongPeriod { get; set; }
[Parameter("ADXR Short Period", DefaultValue = 7)]
public int AdxrShortPeriod { get; set; }
// Output series for buy and sell signals
[Output("Buy Signal", Color = Colors.LimeGreen, PlotType = PlotType.Points, Thickness = 3)]
public IndicatorDataSeries BuySignal { get; set; }
[Output("Sell Signal", Color = Colors.Red, PlotType = PlotType.Points, Thickness = 3)]
public IndicatorDataSeries SellSignal { get; set; }
private bool _buySignalGiven = false; // To track if a buy entry signal has been given
private bool _sellSignalGiven = false; // To track if a sell entry signal has been given
protected override void Initialize()
{
// Initialize indicators with user-defined parameters
_emaLong = Indicators.ExponentialMovingAverage(MarketSeries.Close, EmaLongPeriod);
_emaShort = Indicators.ExponentialMovingAverage(MarketSeries.Close, EmaShortPeriod);
_adxrLong = Indicators.AverageDirectionalMovementIndexRating(AdxrLongPeriod);
_adxrShort = Indicators.AverageDirectionalMovementIndexRating(AdxrShortPeriod);
}
public override void Calculate(int index)
{
// Clear previous signals
BuySignal[index] = double.NaN;
SellSignal[index] = double.NaN;
// Trend confirmation based on the longer ADXR
bool isUptrend = _adxrLong.DIPlus[index] > _adxrLong.DIMinus[index];
bool isDowntrend = _adxrLong.DIPlus[index] < _adxrLong.DIMinus[index];
// Check for Buy Signal
if (isUptrend && !_buySignalGiven)
{
if (_emaShort.Result[index] > _emaLong.Result[index] &&
_adxrShort.DIPlus.HasCrossedAbove(_adxrShort.DIMinus, index))
{
// Place Buy Signal
BuySignal[index] = MarketSeries.Low[index] - Symbol.TickSize; // Placing signal below the bar
_buySignalGiven = true; // Mark that buy entry signal has been given
ChartObjects.DrawText("BuyEntry" + index, "Trend following Buy Entry", index, MarketSeries.Low[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.LimeGreen);
}
}
// Check for Sell Signal
if (isDowntrend && !_sellSignalGiven)
{
if (_emaShort.Result[index] < _emaLong.Result[index] &&
_adxrShort.DIMinus.HasCrossedAbove(_adxrShort.DIPlus, index))
{
// Place Sell Signal
SellSignal[index] = MarketSeries.High[index] + Symbol.TickSize; // Placing signal above the bar
_sellSignalGiven = true; // Mark that sell entry signal has been given
ChartObjects.DrawText("SellEntry" + index, "Trend following Sell Entry", index, MarketSeries.High[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Red);
}
}
// Exit logic for Buy Signal
if (_buySignalGiven && isDowntrend && _adxrShort.DIPlus.HasCrossedBelow(_adxrShort.DIMinus, index))
{
BuySignal[index] = double.NaN; // Remove Buy Signal
_buySignalGiven = false; // Reset the buy signal flag
ChartObjects.DrawText("BuyExit" + index, "Trend following Buy Exit", index, MarketSeries.Low[index] - Symbol.TickSize, VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Red);
}
// Exit logic for Sell Signal
if (_sellSignalGiven && isUptrend && _adxrShort.DIMinus.HasCrossedBelow(_adxrShort.DIPlus, index))
{
SellSignal[index] = double.NaN; // Remove Sell Signal
_sellSignalGiven = false; // Reset the sell signal flag
ChartObjects.DrawText("SellExit" + index, "Trend following Sell Exit", index, MarketSeries.High[index] + Symbol.TickSize, VerticalAlignment.Top, HorizontalAlignment.Center, Colors.LimeGreen);
}
}
}
}
PanagiotisCharalampous
02 Sep 2024, 05:58
Hi there,
The logic seems to do what you are asking for. Can you provide visual examples of the signals it gives now and what signals it should give instead?
Best regards,
Panagiotis
@PanagiotisCharalampous