How to calculate position size programatically

Created at 18 Apr 2015, 09:00
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!
MA

markstewie

Joined 18.04.2015

How to calculate position size programatically
18 Apr 2015, 09:00


Hello,

I'm trying to make a cBot that enters positions at with a certain percentage of the account balance at risk.

For example, if I wanted to risk 2.5% of account balance per trade and I've worked out I need a stop loss of 60 pips for the current trade, how would I calculate the position size taking account base currency, the symbol being traded and the pip value etc?

I have all the variables I need to place a limit order except volume... this is what I'm trying to calculate.

// This is where I've got to but it's clearly not right.
var volume = (Account.Balance * RiskPercentage / 100) / (stopLossPips * Symbol.PipValue);

TradeResult result = PlaceLimitOrder( TradeType.Buy, Symbol, volume, entryPrice, label, stopLossPips, takeProfitPips);

Any help would be greatly appreciated.

 


@markstewie
Replies

WhiteSage
19 Apr 2015, 12:01

Not exactly the answer but relevant:

Volume must be rounded to a suitable amount for trades. Check out Symbol.NormalizeVolume in the reference.


@WhiteSage

9718853
19 Apr 2015, 22:40

RE:

This will give you a calculation to manipulate volume with whole numbers...

var smartVolume = Volume * (int)(Account.Equity / openingBalance)

 

markstewie said:

Hello,

I'm trying to make a cBot that enters positions at with a certain percentage of the account balance at risk.

For example, if I wanted to risk 2.5% of account balance per trade and I've worked out I need a stop loss of 60 pips for the current trade, how would I calculate the position size taking account base currency, the symbol being traded and the pip value etc?

I have all the variables I need to place a limit order except volume... this is what I'm trying to calculate.

// This is where I've got to but it's clearly not right.
var volume = (Account.Balance * RiskPercentage / 100) / (stopLossPips * Symbol.PipValue);

TradeResult result = PlaceLimitOrder( TradeType.Buy, Symbol, volume, entryPrice, label, stopLossPips, takeProfitPips);

Any help would be greatly appreciated.

 

 


@9718853