Category Other  Published on 19/07/2020

Trade size calculator

Description

Indicator is to calculate trade size based on percentage of amount trader want to risk in one trade. This works on the assumption that trader is using ATR for stop losses.

Input parameters include:

Risk Parameters:

  • Risk Percentage: Percentage of total balance trader want to risk on one trade.
  • ATR Stoploss multiplier: Multiplyer to be used in conjunction with ATR for calculating stop loss pips.

ATR Parameters: Parameters required for calculating ATR

  • Periods
  • Moving Average Type

Indicator outputs following:

  • Trade Size(Lots)
  • Trade Size(Units)
  • Desired Risk Amount: This is generally Percentage of Account balance.
  • Calculated Risk Amount: Risk amount after normalizing trade units.
  • Stop Loss Pips: Pips to be used for stop loss.


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

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TradeSize : Indicator
    {
        [Parameter("Risk Percentage", Group = "Risk", DefaultValue = 1, MinValue = 0.5, MaxValue = 5, Step = 0.5)]
        public double RiskPercentage { get; set; }

        [Parameter("ATR Stoploss multiplyer", Group = "Risk", DefaultValue = 2, MinValue = 1, MaxValue = 20, Step = 1)]
        public int ATRMultiplyer { get; set; }

        [Parameter("Periods", Group = "ATR Params", DefaultValue = 14, MinValue = 1)]
        public int Periods { get; set; }

        [Parameter("Moving Average Type", Group = "ATR Params", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType maType { get; set; }

        public IndicatorDataSeries Result;

        private AverageTrueRange atr;

        protected override void Initialize()
        {
            atr = Indicators.AverageTrueRange(Periods, maType);
            Result = CreateDataSeries();
        }

        public override void Calculate(int index)
        {

            double RiskAmount = (Account.Balance * RiskPercentage) / 100;

            double RiskPerUnit = ATRMultiplyer * getATRPips() * Symbol.PipValue;

            double RiskUnits = RiskAmount / RiskPerUnit;

            Result[index] = Symbol.NormalizeVolumeInUnits(RiskUnits);
            string Text = "Trade Size(Lots) : " + Result[index] / Symbol.LotSize + "\n";
            Text += "Trade Size(Units) : " + Result[index] + "\n";
            Text += "Desired Risk Amount : " + RiskAmount + "\n";
            Text += "Calculated Risk Amount : " + (Symbol.NormalizeVolumeInUnits(RiskUnits) * RiskPerUnit + "\n");
            Text += "Stop Loss Pips : " + ATRMultiplyer * getATRPips();
            Chart.DrawStaticText("Symbols", Text, VerticalAlignment.Top, HorizontalAlignment.Left, Color.White);
        }

        private double getATRPips()
        {
            return atr.Result.LastValue / Symbol.PipSize;
        }
    }
}


VO
voldemort

Joined on 10.07.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: TradeSize.algo
  • Rating: 0
  • Installs: 2500
Comments
Log in to add a comment.
FO
fotosbellville · 3 months ago

lo he instalado y no me ha servido, ni idea como usar esto… alguien puede explicarme ó dejar el enlace de algun videotutorial..??

BI
BigManDave · 2 years ago

Thanks for uploading this. Was looking for how to calculate risk; this is very useful and straightforward