calculate volume based trader risk

Created at 15 Jul 2021, 13:59
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!
IR

IRCtrader

Joined 17.06.2021

calculate volume based trader risk
15 Jul 2021, 13:59


i try to calculate volume based on risk percent . i used pipvalue but it give me volume based on quantity and i need to get lot size.

using cAlgo.API;
using System;
using Microsoft.Win32;
namespace cAlgo
{
    // This sample indicator shows how to add a text box control on your chart
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AWCalcVolume : Indicator
    {

        double risk;
        double pip;
        TextBox textBox3 = new TextBox();



        [Parameter("Horizontal Alignment ", DefaultValue = HorizontalAlignment.Left)]
        public HorizontalAlignment HAlignment { get; set; }

        [Parameter("Vertical Alignment", DefaultValue = VerticalAlignment.Top)]
        public VerticalAlignment VAlignment { get; set; }

        [Parameter("Orientation", DefaultValue = Orientation.Horizontal)]
        public Orientation Orientation1 { get; set; }

        [Parameter("Color", DefaultValue = "Red")]
        public string color { get; set; }


        protected override void Initialize()
        {
            var mainStackPanel = new StackPanel 
            {
                HorizontalAlignment = HAlignment,
                VerticalAlignment = VAlignment,
                Orientation = Orientation1
            };
            var StackPanel1 = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Orientation = Orientation.Vertical
            };
            var StackPanel2 = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Orientation = Orientation.Vertical,
                Margin = 3
            };
            var StackPanel3 = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Orientation = Orientation.Vertical
            };
            var textBlock1 = new TextBlock 
            {
                ForegroundColor = color,
                HorizontalAlignment = HorizontalAlignment.Center,
                Text = "Risk%"
            };

            var textBox1 = new TextBox 
            {
                ForegroundColor = color,
                HorizontalAlignment = HorizontalAlignment.Center,
                Width = 50,
                BorderThickness = 2,
                BorderColor = color,
                TextAlignment = TextAlignment.Center
            };
            var textBlock2 = new TextBlock 
            {
                ForegroundColor = color,
                HorizontalAlignment = HorizontalAlignment.Center,
                Text = "SL(pips)"
            };
            var textBox2 = new TextBox 
            {
                ForegroundColor = color,
                HorizontalAlignment = HorizontalAlignment.Center,
                Width = 50,
                BorderThickness = 2,
                BorderColor = color,
                TextAlignment = TextAlignment.Center
            };


            var textBlock3 = new TextBlock 
            {

                ForegroundColor = color,
                HorizontalAlignment = HorizontalAlignment.Center,
                Text = "Volume"
            };


            textBox3.ForegroundColor = color;
            textBox3.HorizontalAlignment = HorizontalAlignment.Center;
            textBox3.Width = 50;
            textBox3.BorderThickness = 2;
            textBox3.BorderColor = color;
            textBox3.IsEnabled = false;
            textBlock3.TextAlignment = TextAlignment.Center;


            textBox1.TextChanged += TextBox1_TextChanged;
            textBox2.TextChanged += TextBox2_TextChanged;

            StackPanel1.AddChild(textBlock1);
            StackPanel1.AddChild(textBox1);

            StackPanel2.AddChild(textBlock2);
            StackPanel2.AddChild(textBox2);

            StackPanel3.AddChild(textBlock3);
            StackPanel3.AddChild(textBox3);

            mainStackPanel.AddChild(StackPanel1);
            mainStackPanel.AddChild(StackPanel2);
            mainStackPanel.AddChild(StackPanel3);

            Chart.AddControl(mainStackPanel);
        }
        private void TextBox1_TextChanged(TextChangedEventArgs obj)
        {
            risk = Convert.ToDouble(obj.TextBox.Text);
        }
        private void TextBox2_TextChanged(TextChangedEventArgs obj)
        {
            pip = Convert.ToDouble(obj.TextBox.Text);
        }
        public override void Calculate(int index)
        {
            textBox3.Text = CalcVolume(risk, pip);

        }

        public string CalcVolume(double r, double p)
        {
            return Math.Round(Account.Balance * r / 100 / (pip *Symbol.PipValue), 2).ToString();

        }
    }
}

 


@IRCtrader
Replies

PanagiotisCharalampous
16 Jul 2021, 08:21

Hi khoshroomahdi,

You can use VolumeInUnitsToQuantity to do the conversion.

Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous