Category Trend  Published on 04/07/2019

Trend Magic

Description

Follow my cTrader Telegram group at https://t.me/cTraderCommunity; it's a new community but it will grow fast, plus everyone can talk about cTrader indicators and algorithm without restrictions, though it is not allowed to spam commercial indicators to sell them. There's also a Discord Server now @ https://discord.gg/5GAPMtp

This is a conversion for the Trend Magic indicator.

I removed a bit of code for the coloring that made it change colors too often.

Enjoy!

For any bug report or suggestion, contace me by joinig the group linked above or by commenting below.


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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendMagic : Indicator
    {
        [Parameter("CCI Period", DefaultValue = 20)]
        public int cciPer { get; set; }
        [Parameter("ATR Period", DefaultValue = 5)]
        public int atrPer { get; set; }
        [Parameter("ATR Multiplier", DefaultValue = 1)]
        public int atrMul { get; set; }

        [Output("Trend Magic", LineColor = "Red")]
        public IndicatorDataSeries TM { get; set; }
        [Output("Trend Magic Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Lime", Thickness = 2)]
        public IndicatorDataSeries TMUp { get; set; }
        [Output("Trend Magic Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red", Thickness = 2)]
        public IndicatorDataSeries TMDown { get; set; }

        private CommodityChannelIndex cci;
        private AverageTrueRange atr;
        private IndicatorDataSeries bufferUp, bufferDown, x, swap;


        protected override void Initialize()
        {
            cci = Indicators.CommodityChannelIndex(cciPer);
            atr = Indicators.AverageTrueRange(atrPer, MovingAverageType.Simple);
            bufferDown = CreateDataSeries();
            bufferUp = CreateDataSeries();
            x = CreateDataSeries();
            swap = CreateDataSeries();
        }
        public override void Calculate(int index)
        {
            bufferDown[index] = MarketSeries.High[index] + atrMul * atr.Result[index];
            bufferUp[index] = MarketSeries.Low[index] - atrMul * atr.Result[index];

            if (cci.Result[index] >= 0 && cci.Result[index - 1] < 0)
            {
                bufferUp[index] = bufferDown[index - 1];
            }
            if (cci.Result[index] <= 0 && cci.Result[index - 1] > 0)
            {
                bufferDown[index] = bufferUp[index - 1];
            }
            if (cci.Result[index] >= 0 && bufferUp[index] < bufferUp[index - 1])
                bufferUp[index] = bufferUp[index - 1];
            else if (cci.Result[index] <= 0 && bufferDown[index] > bufferDown[index - 1])
                bufferDown[index] = bufferDown[index - 1];

            x[index] = cci.Result[index] >= 0 ? bufferUp[index] : cci.Result[index] < 0 ? bufferDown[index] : x[index - 1];
            swap[index] = x[index] > x[index - 1] ? 1 : x[index] < x[index - 1] ? -1 : swap[index - 1];

            TM[index] = x[index];
            if (swap[index] == 1)
            {
                TMUp[index] = x[index];
                TMDown[index] = double.NaN;
            }
            if (swap[index] == -1)
            {
                TMDown[index] = x[index];
                TMUp[index] = double.NaN;
            }
        }
    }
}


CY
cysecsbin.01

Joined on 10.11.2018 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Trend Magic.algo
  • Rating: 5
  • Installs: 3415
Comments
Log in to add a comment.
SU
suchocki82 · 3 years ago

Hi

I like this indicator.

Can you add bot function which will buy on green and sell on red?

Rgds.

JE
jedimaster · 4 years ago

In the first place, I was very impressed by the implementation of the Belkhayate in the signal indicator you provided. The GUI is giving a very helpful assistance during the stressfull trading hours. I am following him for several years and I believe you have done something quite unique. As I am mainly trading very volatile commodities, trend magic based on CCI was something I definitely want to look into details. I think it can replace both Supertrend and/or ATR trailer as it shares the methodology but it's instead based on CCI rather than the data series itself. I just added minor options to help me with super volatile commodities and also in a way to bridge Trend Magic with those two previous indicators. I am using the normalized version of the ATR.but aside that modification the default values won't change the original version behavior.

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendMagicVL : Indicator
    {
        [Parameter("CCI Period", DefaultValue = 20)]
        public int cciPer { get; set; }
        [Parameter("MA Method", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }
        [Parameter("ATR Period", DefaultValue = 5)]
        public int atrPer { get; set; }
        [Parameter("ATR Factor", DefaultValue = 1, MinValue = 0.1, MaxValue = 4.0)]
        public double Factor { get; set; }
        [Parameter("ATR Multiplier", DefaultValue = 1.0)]
        public double atrMul { get; set; }
        [Output("Trend Magic", LineColor = "Yellow")]
        public IndicatorDataSeries TM { get; set; }
        [Output("Trend Magic Up", PlotType = PlotType.DiscontinuousLine, LineColor = "Lime", Thickness = 2)]
        public IndicatorDataSeries TMUp { get; set; }
        [Output("Trend Magic Down", PlotType = PlotType.DiscontinuousLine, LineColor = "Red", Thickness = 2)]
        public IndicatorDataSeries TMDown { get; set; }

        private CommodityChannelIndex cci;
        private AverageTrueRange atr;
        private IndicatorDataSeries bufferUp, bufferDown, x, swap;


        protected override void Initialize()
        {
            cci = Indicators.CommodityChannelIndex(cciPer);
            atr = Indicators.AverageTrueRange(atrPer, MaType);
            bufferDown = CreateDataSeries();
            bufferUp = CreateDataSeries();
            x = CreateDataSeries();
            swap = CreateDataSeries();
        }
        public override void Calculate(int index)
        {
            bufferDown[index] = MarketSeries.High[index] + Factor * (atrMul * (100 * atr.Result[index] / MarketSeries.Close[index]));
            bufferUp[index] = MarketSeries.Low[index] - Factor * (atrMul * (100 * atr.Result[index] / MarketSeries.Close[index]));

            if (cci.Result[index] >= 0 && cci.Result[index - 1] < 0)
            {
                bufferUp[index] = bufferDown[index - 1];
            }
            if (cci.Result[index] <= 0 && cci.Result[index - 1] > 0)
            {
                bufferDown[index] = bufferUp[index - 1];
            }
            if (cci.Result[index] >= 0 && bufferUp[index] < bufferUp[index - 1])
                bufferUp[index] = bufferUp[index - 1];
            else if (cci.Result[index] <= 0 && bufferDown[index] > bufferDown[index - 1])
                bufferDown[index] = bufferDown[index - 1];

            x[index] = cci.Result[index] >= 0 ? bufferUp[index] : cci.Result[index] < 0 ? bufferDown[index] : x[index - 1];
            swap[index] = x[index] > x[index - 1] ? 1 : x[index] < x[index - 1] ? -1 : swap[index - 1];

            TM[index] = x[index];
            if (swap[index] == 1)
            {
                TMUp[index] = x[index];
                TMDown[index] = double.NaN;
            }
            if (swap[index] == -1)
            {
                TMDown[index] = x[index];
                TMUp[index] = double.NaN;
            }
        }
    }
}

 

AL
alexsanramon · 5 years ago

Join the group and request the indicator you are dreaming about.