Trend Magic

by cysecsbin.01 in category Trend at 04/07/2019
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.

Download
173 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
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;
            }
        }
    }
}
Comments

alexsanramon - July 04, 2019 @ 10:29

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

5