MACD CANDLE --- Can anyone Please Help Me Fix the code?

Created at 05 Feb 2023, 16:41
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
Nkosinathi-Muravha's avatar

Nkosinathi-Muravha

Joined 12.08.2021

MACD CANDLE --- Can anyone Please Help Me Fix the code?
05 Feb 2023, 16:41


using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AutoRescale = false, ScalePrecision = 4, AccessRights = AccessRights.None)]
    public class MACDCandle : Indicator
    {
        private ExponentialMovingAverage fastEMA;
        private ExponentialMovingAverage slowEMA;
        private MovingAverageConvergenceDivergence macd;

        [Parameter("Fast EMA Period", DefaultValue = 12, MinValue = 2, MaxValue = 100, Step = 1)]
        public int FastPeriod { get; set; }

        [Parameter("Slow EMA Period", DefaultValue = 26, MinValue = 2, MaxValue = 100, Step = 1)]
        public int SlowPeriod { get; set; }

        [Parameter("Signal Period", DefaultValue = 9, MinValue = 1, MaxValue = 100, Step = 1)]
        public int SignalPeriod { get; set; }

        [Output("MACD", Color = Colors.Red, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries MACD { get; set; }

        [Output("Signal", Color = Colors.Blue, LineStyle = LineStyle.Lines)]
        public IndicatorDataSeries Signal { get; set; }

        [Output("Histogram", Color = Colors.Green, LineStyle = LineStyle.Histogram)]
        public IndicatorDataSeries Histogram { get; set; }

        protected override void Initialize()
        {
            fastEMA = Indicators.ExponentialMovingAverage(MarketData.GetPrice(PriceType.Close), FastPeriod);
            slowEMA = Indicators.ExponentialMovingAverage(MarketData.GetPrice(PriceType.Close), SlowPeriod);
            macd = Indicators.MovingAverageConvergenceDivergence(fastEMA.Result, slowEMA.Result, SignalPeriod);
        }

        public override void Calculate(int index)
        {
            MACD[index] = macd.Macd[index];
            Signal[index] = macd.Signal[index];
            Histogram[index] = macd.Histogram[index];

            // Optional: plot MACD as a candle
            if (MACD[index] > Signal[index])
            {
                ChartObjects.DrawText("macd_candle" + index, "↑", index, MACD[index], VerticalAlignment.Center, HorizontalAlignment.Center, Colors.Green);
                ChartObjects.DrawLine("macd_candle_line" + index, index, MACD[index], index + 1, MACD[index], Colors.Green, 2, LineStyle.Solid);
            }
            else if (MACD[index] < Signal[index])
            {
                ChartObjects.DrawText("macd_candle" + index, "↓", index, MACD[index], VerticalAlignment.Center, HorizontalAlignment.Center, Colors.Red);
                ChartObjects.DrawLine("macd_candle_line" + index, index, MACD[index], index

 


@Nkosinathi-Muravha
Replies

PanagiotisChar
06 Feb 2023, 10:45

Hi there,

The code in not even complete. Please provide the complete indicator code and explain what the exact problem is.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar