lots-calculate function

Created at 08 Apr 2020, 11:34
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
M1

m17qarock

Joined 08.04.2020

lots-calculate function
08 Apr 2020, 11:34


1.Please add a lots-calculate function:

Trader decide risk X % and stoploss X pips, and system calculate how many lots we should trade.

(It's really useful for day trading)

2.Add 2 hotkey:

Active symbol Panel: show/not show 

Trade watch : show/not show 


cTrader
@m17qarock
Replies

genappsforex
08 Apr 2020, 14:07

automation

lots calculate also in automate


@genappsforex

Shares4UsDevelopment
13 Apr 2020, 10:41

RE: automation

genappsforex said:

lots calculate also in automate

agree


@Shares4UsDevelopment

... Deleted by UFO ...

Reneemar862
12 Jun 2020, 07:03

RE: automation

mywegmansconnect said:

lots calculate also in automate 

Agree me too


@Reneemar862

ranjicgnr
28 Apr 2021, 14:54

Risk Calculation Funcrion

You can create your own. The below is mine. You can modify if you want.

 

public class RiskManagementService : IRiskManagementService
    {

        private readonly IAccount _account;
        private readonly Symbol _symbol;

        public RiskManagementService(IAccount account, Symbol symbol)
        {
            _account = account;
            _symbol = symbol;
        }

        public double CalculateBuyQty(double riskPercent, double stopLoss, double askPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk)
        {
            isLimitReached = false;
            currentRisk = riskPercent;
            double balance = _account.Balance;
            double riskAmount = balance * riskPercent * 0.01;
            double longRiskPoints = askPrice - stopLoss;
            if(stopValueType == StopValueType.Pips)
            {
                longRiskPoints = _symbol.PipSize * stopLoss;
            }
            double volumeinLots = Math.Floor(((riskAmount / longRiskPoints) / _symbol.LotSize) * 100) / 100;
            if(volumeinLots < minimumQty)
            {
                volumeinLots = minimumQty;
                isLimitReached = true;
                currentRisk = Math.Ceiling(((longRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
            }
            if(volumeinLots > maximumQty)
            {
                volumeinLots = maximumQty;
                currentRisk = Math.Ceiling(((longRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
                isLimitReached = true;
            }

            return volumeinLots;
        }

        public double CalculateSellQty(double riskPercent, double stopLoss, double bidPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk)
        {
            isLimitReached = false;
            currentRisk = riskPercent;
            double balance = _account.Balance;
            double riskAmount = balance * riskPercent * 0.01;
            double shortRiskPoints = stopLoss - bidPrice;
            if (stopValueType == StopValueType.Pips)
            {
                shortRiskPoints = _symbol.PipSize * stopLoss;
            }
            double volumeinLots = Math.Floor(((riskAmount / shortRiskPoints) / _symbol.LotSize) * 100) / 100;
            if (volumeinLots < minimumQty)
            {
                volumeinLots = minimumQty;
                isLimitReached = true;
                currentRisk = Math.Ceiling(((shortRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
            }
            if (volumeinLots > maximumQty)
            {
                volumeinLots = maximumQty;
                currentRisk = Math.Ceiling(((shortRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
                isLimitReached = true;
            }
            return volumeinLots;
        }
    }

    public interface IRiskManagementService
    {
        double CalculateBuyQty(double riskPercent, double stopLossPrice, double askPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk);
        double CalculateSellQty(double riskPercent, double stopLossPrice, double bidPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk);
    }

    public enum StopValueType
    {
        Absolute,
        Pips
    }
}


@ranjicgnr

ezequieltp97
07 May 2021, 17:17

RE: Risk Calculation Funcrion

ranjicgnr said:

You can create your own. The below is mine. You can modify if you want.

 

public class RiskManagementService : IRiskManagementService
    {

        private readonly IAccount _account;
        private readonly Symbol _symbol;

        public RiskManagementService(IAccount account, Symbol symbol)
        {
            _account = account;
            _symbol = symbol;
        }

        public double CalculateBuyQty(double riskPercent, double stopLoss, double askPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk)
        {
            isLimitReached = false;
            currentRisk = riskPercent;
            double balance = _account.Balance;
            double riskAmount = balance * riskPercent * 0.01;
            double longRiskPoints = askPrice - stopLoss;
            if(stopValueType == StopValueType.Pips)
            {
                longRiskPoints = _symbol.PipSize * stopLoss;
            }
            double volumeinLots = Math.Floor(((riskAmount / longRiskPoints) / _symbol.LotSize) * 100) / 100;
            if(volumeinLots < minimumQty)
            {
                volumeinLots = minimumQty;
                isLimitReached = true;
                currentRisk = Math.Ceiling(((longRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
            }
            if(volumeinLots > maximumQty)
            {
                volumeinLots = maximumQty;
                currentRisk = Math.Ceiling(((longRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
                isLimitReached = true;
            }

            return volumeinLots;
        }

        public double CalculateSellQty(double riskPercent, double stopLoss, double bidPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk)
        {
            isLimitReached = false;
            currentRisk = riskPercent;
            double balance = _account.Balance;
            double riskAmount = balance * riskPercent * 0.01;
            double shortRiskPoints = stopLoss - bidPrice;
            if (stopValueType == StopValueType.Pips)
            {
                shortRiskPoints = _symbol.PipSize * stopLoss;
            }
            double volumeinLots = Math.Floor(((riskAmount / shortRiskPoints) / _symbol.LotSize) * 100) / 100;
            if (volumeinLots < minimumQty)
            {
                volumeinLots = minimumQty;
                isLimitReached = true;
                currentRisk = Math.Ceiling(((shortRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
            }
            if (volumeinLots > maximumQty)
            {
                volumeinLots = maximumQty;
                currentRisk = Math.Ceiling(((shortRiskPoints * _symbol.QuantityToVolumeInUnits(volumeinLots) * 10000) / balance) * 0.01);
                isLimitReached = true;
            }
            return volumeinLots;
        }
    }

    public interface IRiskManagementService
    {
        double CalculateBuyQty(double riskPercent, double stopLossPrice, double askPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk);
        double CalculateSellQty(double riskPercent, double stopLossPrice, double bidPrice, double minimumQty, double maximumQty, 
            StopValueType stopValueType, out bool isLimitReached, out double currentRisk);
    }

    public enum StopValueType
    {
        Absolute,
        Pips
    }
}

Hi Sir! Excuse me, could you explain to me how to correctly place the code in CTrader? I copied and pasted it but when compiling it gave me an error (No algo source file was found in "New cBot.csproj")  Is it possible that you can send a screenshot or something? Thank you!


@ezequieltp97