macd and stoch in one windows

khoshroomahdi's avatar

khoshroomahdi since: 17 Jun 2021;

  22 Jul 2021, 08:27
macd and stoch in one windows

i try to have stoch and macd in same windows but down'st show both together.

i want to show both and macd zero level fix on stoch 50 level.

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

namespace cAlgo
{

    [Levels(0)]
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Macdstoch : Indicator
    {


        public MacdHistogram _Macd3;


        [Parameter("source")]
        public DataSeries Source { get; set; }

        private StochasticOscillator _stochastic;




        [Parameter("Long Cycle 3", DefaultValue = 26, Group = "Macd 3")]
        public int LongCycle3 { get; set; }

        [Parameter("Short Cycle 3", DefaultValue = 12, Group = "Macd 3")]
        public int ShortCycle3 { get; set; }

        [Parameter("Signal 3", DefaultValue = 9, Group = "Macd 3")]
        public int inpSingal3 { get; set; }


        [Parameter("K Periods", DefaultValue = 5)]
        public int kPeriods { get; set; }

        [Parameter("K Slowing", DefaultValue = 3)]
        public int kSlowing { get; set; }

        [Parameter("D Periods", DefaultValue = 3)]
        public int dPeriods { get; set; }







        [Output("Macd 3 ", LineColor = "Purple", LineStyle = LineStyle.Solid, Thickness = 2, PlotType = PlotType.Histogram)]
        public IndicatorDataSeries Macd3 { get; set; }
        [Output("Signal 3 ", LineColor = "Black", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries Singal3 { get; set; }


        [Output("K%  ", LineColor = "Black", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries k { get; set; }
        [Output("D% ", LineColor = "Black", LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries d { get; set; }

        protected override void Initialize()
        {

            _stochastic = Indicators.StochasticOscillator(kPeriods, kSlowing, dPeriods, MovingAverageType.Simple);

            _Macd3 = Indicators.MacdHistogram(Source, LongCycle3, ShortCycle3, inpSingal3);


        }

        public override void Calculate(int index)
        {



            Macd3[index] = _Macd3.Histogram[index];
            Singal3[index] = _Macd3.Signal[index];

            k[index] = _stochastic.PercentK[index];
            d[index] = _stochastic.PercentD[index];


        }
    }
}
amusleh's avatar

amusleh since: 01 Mar 2021;

  22 Jul 2021, 10:17

Hi,

There is a scaling issue with MACD and Stochastic indicators, MACD usually fluctuates between -1 and 1, Stochastic fluctuates between 0-100.

If you place both indicators on the same chart with the same Y-axis then you will not be able to display the one with a lower scale properly, to solve this issue you have to change the scale of both to the same scale by using a scaling method like normalization or min/max scaling.

Community Developer | Spotware | Telegram: @algodeveloper
khoshroomahdi's avatar

khoshroomahdi since: 17 Jun 2021;

  22 Jul 2021, 12:17
RE:

amusleh said:

Hi,

There is a scaling issue with MACD and Stochastic indicators, MACD usually fluctuates between -1 and 1, Stochastic fluctuates between 0-100.

If you place both indicators on the same chart with the same Y-axis then you will not be able to display the one with a lower scale properly, to solve this issue you have to change the scale of both to the same scale by using a scaling method like normalization or min/max scaling.

could you give me sample in my code.

and how can i fix macd zero level on stoch 50 percent level?

and i couldn't find scaling method in references

amusleh's avatar

amusleh since: 01 Mar 2021;

  22 Jul 2021, 13:11

Hi,

Please Google about normalizing a set of numbers or min/max scaling, it's not part of cTrader automate API.

You have to do it by yourself, there are plenty of examples on the web.

Community Developer | Spotware | Telegram: @algodeveloper