Position Size function based on risk amount and stop size
Posted: Jul 17 2022
Hi all,
I've been using a poorly coded position size function for a few years now and I've decided to refactor it so it can be used across all markets and brokers with no issues. The function takes a stop loss size and the dollars risked and outputs a position size. I'm surprised multicharts doesn't come with this function already. Anyways I would guess that many other users already have this function coded.
The code below is what I've done so far. This works fairly well, except when I try it on crypto on Oanda or LMAX. I would like to include commissions into the calculation too. So if I risk 100 usd that's the total risk including the commission of the trade. Any help would be much appreciated
I've been using a poorly coded position size function for a few years now and I've decided to refactor it so it can be used across all markets and brokers with no issues. The function takes a stop loss size and the dollars risked and outputs a position size. I'm surprised multicharts doesn't come with this function already. Anyways I would guess that many other users already have this function coded.
The code below is what I've done so far. This works fairly well, except when I try it on crypto on Oanda or LMAX. I would like to include commissions into the calculation too. So if I risk 100 usd that's the total risk including the commission of the trade. Any help would be much appreciated
![Very Happy :D](./images/smilies/icon_biggrin.gif)
Code: Select all
Input:
riskAmt(numericSimple),
StopLoss_size(numericSimple);
var:
base_currency( USD ),
riskInCounterCurrency( 0 ),
PositionSize( 0 );
//convert to counter currency
riskInCounterCurrency = convert_currency( DateTime, base_currency, SymbolCurrencyCode, riskAmt);
PositionSize = riskInCounterCurrency/ (StopLoss_size * 10 * PointValue);
if PositionSize > 100000 then PositionSize = Floor(PositionSize/1000) * 1000; // Oanda Position size
Position_size_$risk = PositionSize;