Topics
22 Apr 2021, 23:48
 0
 1178
 2
Replies

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

ranjicgnr
27 Apr 2021, 13:56 ( Updated at: 21 Dec 2023, 09:22 )

 

I am coding one for risk based qty. You can suggest features. I will try to incorporate


@ranjicgnr

ranjicgnr
22 Apr 2021, 23:36

RE:

ctid3066979 said:

Not sure if my heading explain it enough but i will try go into detail. so basically would like feature that is available on mt4/5 position size calculator that we can have a fixed risk percentage and our lot size changes based on our entry and SL input. 

thanks

graham

 

Hai

You can use the below code for automating lot sizing on cAlgo. Also you can modify the way you wanted

 

                                     double longStopLoss = Math.Min(Bars.Last(1).Low, Bars.Last(2).Low) - (Symbol.PipValue * 0.5);
                                    double balance = Account.Balance;
                                    double riskAmount = balance * RiskPercent * 0.01;
                                    douuble longRiskPoints = Ask - longStopLoss;
                                    double volumeinLots = Math.Floor(((riskAmount / longRiskPoints) / Symbol.LotSize) * 100) / 100;
                                    double volumeInUnits = Symbol.QuantityToVolumeInUnits(volumeinLots);
                                    ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, volumeInUnits, label);

 

I have used for EuroUSD. I have put last two bars low as stoploss.


@ranjicgnr