Risk on all open positions

Created at 23 Oct 2013, 01:46
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!
kricka's avatar

kricka

Joined 13.03.2013

Risk on all open positions
23 Oct 2013, 01:46


Hi,

would like to know the code for printing the following to the log.

The total risk in currency on all open positions. I assume you use total volume, total stop loss and balance.

Thanks


@kricka
Replies

Spotware
23 Oct 2013, 15:36

In the next version we will add a new property to the Symbol class: PipValue - the amount of profit that you gain or lose when the price goes up or down one pip so this task will be much easier. Right now, you will need to convert your possible loss from the quote currency of the symbol to your deposit currency.


@Spotware

kricka
22 Jan 2014, 17:11

Hi,

I'm just after to print the amount at risk based on all opened positions.  So counting total volume, stop loss and pip value on all opened positions might do the trick.

Any suggestion for a code to achieve this?

Thanks..


@kricka

fzlogic
22 Jan 2014, 17:39

RE:

Isn't that the sum of all positions net profit?

kricka said:

Hi,

I'm just after to print the amount at risk based on all opened positions.  So counting total volume, stop loss and pip value on all opened positions might do the trick.

Any suggestion for a code to achieve this?

Thanks..

 


@fzlogic

kricka
22 Jan 2014, 21:13

What I'm after is the risk based on the balance. For example if the account balance is 10.000 and and there is lets say 5 positions opened with a 20 pips stop loss each and the total volume on all the positions are 250.000. The risk would then be 5% or in amount 500 which you are at risk to lose if the stop loss are hit on all the 5 positions.

Hope it clarifies somewhat what I would like to print to the log.


@kricka

atrader
24 Jan 2014, 12:25

protected override void OnStart()
{
    double risk = 0;
    foreach (var position in Positions)
    {
        Symbol symbol = MarketData.GetSymbol(position.SymbolCode);
        if (position.StopLoss != null)
        {
            var stopLoss = Math.Abs(position.EntryPrice - (double)position.StopLoss) / symbol.PipSize;
            risk += stopLoss * symbol.PipValue * position.Volume;
        }

    }

    Print(risk);
    Stop();
}

I think that should work.


@atrader

kricka
27 Jan 2014, 16:58

Atrader, it sure did work and thanks once again for you excellent coding. If I am not mistaken the info printed to the log is without commission. Maybe you have an idea how to add code to include commission both ways. It will then give a more accurate sum of the risk.


@kricka

atrader
28 Jan 2014, 11:16

Add position.Commissions * 2 to the risk

risk += stopLoss * symbol.PipValue * position.Volume + position.Commissions*2;

 


@atrader

atrader
28 Jan 2014, 11:18

Sorry that would be subtract because commissions are negative so:

risk += stopLoss * symbol.PipValue * position.Volume - position.Commissions*2;

 


@atrader

kricka
07 Feb 2014, 23:37

Perfect, the comm is included now and it prints the correct info to the log.

Thanks..


@kricka