Category Trend  Published on 18/12/2023

supertrend money management.

Description

Calculate the lots to use at the supertrend level.


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

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class CombinedIndicator : Indicator
    {
        [Parameter("Supertrend Period", DefaultValue = 10)]
        public int SupertrendPeriod { get; set; }

        [Parameter("Supertrend Multiplier", DefaultValue = 3)]
        public double SupertrendMultiplier { get; set; }

        [Parameter("Risk Percentage", DefaultValue = 1, MinValue = 0.1, MaxValue = 100, Group = "Risk Params")]
        public double RiskPercentage { get; set; }

        [Parameter("Vertical Position", DefaultValue = VerticalAlignment.Bottom, Group = "Risk Params")]
        public VerticalAlignment Valign { get; set; }

        [Parameter("Horizontal Position", DefaultValue = HorizontalAlignment.Left, Group = "Risk Params")]
        public HorizontalAlignment Halign { get; set; }

        public enum _MyBalance
        {
            Balance,
            Equity
        }

        [Parameter("Capital", DefaultValue = _MyBalance.Equity, Group = "Risk Params")]
        public _MyBalance CapitalRisk { get; set; }

        [Output("UpTrend", Color = Colors.LimeGreen,  PlotType = PlotType.DiscontinuousLine, Thickness = 1)]
        public IndicatorDataSeries UpTrend { get; set; }

        [Output("DownTrend", Color = Colors.Crimson, PlotType = PlotType.DiscontinuousLine, Thickness = 1)]
        public IndicatorDataSeries DownTrend { get; set; }

        private IndicatorDataSeries _upBuffer;
        private IndicatorDataSeries _downBuffer;
        private AverageTrueRange _averageTrueRange;
        private int[] _trend;
        private bool _changeofTrend;

        protected override void Initialize()
        {
            _trend = new int[Bars.Count];
            _upBuffer = CreateDataSeries();
            _downBuffer = CreateDataSeries();
            _averageTrueRange = Indicators.AverageTrueRange(SupertrendPeriod, MovingAverageType.Simple);

            Print("Combined Indicator: 1.0.0");
        }

        public override void Calculate(int index)
        {
            UpTrend[index] = double.NaN;
            DownTrend[index] = double.NaN;

            double median = (MarketSeries.High[index] + MarketSeries.Low[index]) / 2;
            double atr = _averageTrueRange.Result[index];

            _upBuffer[index] = median + SupertrendMultiplier * atr;
            _downBuffer[index] = median - SupertrendMultiplier * atr;

            if (index < 1)
            {
                _trend[index] = 1;
                return;
            }

            if (MarketSeries.Close[index] > _upBuffer[index - 1])
            {
                _trend[index] = 1;
                if (_trend[index - 1] == -1)
                    _changeofTrend = true;
            }
            else if (MarketSeries.Close[index] < _downBuffer[index - 1])
            {
                _trend[index] = -1;
                if (_trend[index - 1] == 1)
                    _changeofTrend = true;
            }
            else
            {
                _trend[index] = _trend[index - 1];
                _changeofTrend = false;
            }

            UpdateBuffers(index, median, atr);
            UpdateTrendLines(index);

            DisplaySupertrendValue(index);

            if (IsLastBar)
            {
                string info = String.Format("{0}", SymbolName, CapitalRisk.ToString());

                double myCapitalRisk = _getMyCapital(CapitalRisk);
                double supertrendValue = _trend[index] == 1 ? UpTrend[index] : DownTrend[index];
                double differenzaInPip = Math.Abs(supertrendValue - MarketSeries.Close[index]) / Symbol.PipSize;

                info += string.Format("\nDifferenza in Pip: {0:F2} pip", differenzaInPip);
                info += string.Format("\nRisk Percentage: {0}%", RiskPercentage);
                info += string.Format("\nLotti da utilizzare: {0:0.00}", _getLotSize(myCapitalRisk, RiskPercentage, differenzaInPip));

                Chart.DrawStaticText("RiskCalculator", info, Valign, Halign, Color.Black);
            }
        }

        private void UpdateBuffers(int index, double median, double atr)
        {
            if (_trend[index] < 0 && _trend[index - 1] > 0)
                _upBuffer[index] = median + (SupertrendMultiplier * atr);
            else if (_trend[index] > 0 && _trend[index - 1] < 0)
                _downBuffer[index] = median - (SupertrendMultiplier * atr);

            if (_trend[index] < 0 && _upBuffer[index] > _upBuffer[index - 1])
                _upBuffer[index] = _upBuffer[index - 1];

            if (_trend[index] > 0 && _downBuffer[index] < _downBuffer[index - 1])
                _downBuffer[index] = _downBuffer[index - 1];
        }

        private void UpdateTrendLines(int index)
        {
            if (_trend[index] == 1)
            {
                UpTrend[index] = _downBuffer[index];
                if (_changeofTrend)
                    UpTrend[index - 1] = DownTrend[index - 1];
            }
            else if (_trend[index] == -1)
            {
                DownTrend[index] = _upBuffer[index];
                if (_changeofTrend)
                    DownTrend[index - 1] = UpTrend[index - 1];
            }
        }

        private void DisplaySupertrendValue(int index)
        {
            // Puoi fare qualcosa con il valore del Supertrend se necessario
        }

        private double _getLotSize(double capital, double riskPercentage, double differenceInPips)
        {
            double pipValue = Symbol.PipValue * Symbol.LotSize;
            double moneyRisk = (capital / 100) * riskPercentage;
            double stopLossInPips = differenceInPips;

            double lotSize = Math.Round(moneyRisk / (stopLossInPips * pipValue), 2);
            double Minim = 0.01;

            return lotSize < Minim ? Minim : lotSize;
        }

        private double _getMyCapital(_MyBalance x)
        {
            switch (x)
            {
                case _MyBalance.Equity:
                    return Account.Equity;
                default:
                    return Account.Balance;
            }
        }
    }
}

DE
DELETED_USER

Joined on 28.06.2022 Blocked

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: SUPERTRENMONEYM.algo
  • Rating: 0
  • Installs: 296
  • Modified: 18/12/2023 17:13
Comments
Log in to add a comment.
DE
DELETED_USER · 10 months ago

grazie Andrea

AndreaPereira's avatar
AndreaPereira · 10 months ago

devi caricare l'indicatore su un sito a parte e collegare il link su questa pagina sotto ogni articolo.

DE
DELETED_USER · 10 months ago

I don't understand where they pay me?

DE
DELETED_USER · 10 months ago

I did it but I don't know why it gives two, the old one and the new one

JI
jim.tollan · 10 months ago

fair enough - so why not do the reader a courtesy and put some text into the description to alert to that fact…

as i said, if we don't object to stuff like this, the crowds will just keep on walking by! I hope you understand my point.

DE
DELETED_USER · 10 months ago

it was just a test, I still need to understand how it works

 

JI
jim.tollan · 10 months ago

vince -please check your uploads before committing them - this is a waste of time as there is no info, no upload, no clue as to what it does. if this demonstrates your level of attention to detail, i have some misgivings about what the PAID content quality must be like.

sorry if this sounds like an attack but if we don't self moderate rubbish, then we all suffer.