Position Size function based on risk amount and stop size

Questions about MultiCharts and user contributed studies.
DanielFXS
Posts: 29
Joined: 08 Oct 2019
Has thanked: 10 times
Been thanked: 4 times

Position Size function based on risk amount and stop size

Postby DanielFXS » 17 Jul 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 :D

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;

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: Position Size function based on risk amount and stop size

Postby rrams » 04 Aug 2022

Hello DanielFXS,
If you wanted to just use currency conversion when needed; you could try: (Some Oanda pairs do not support currency conversion)

Code: Select all

if StrategyCurrencyCode<>SymbolCurrencyCode and StrategyCurrencyCode>0 then riskInCounterCurrency=Convert_Currency(DateTime, StrategyCurrencyCode, SymbolCurrencyCode, riskAmt) else riskInCounterCurrency=riskAmt;
Hopefully, someone might help you with the rest if you are much more specific about the details. Is the StopLoss_Size input a dollar amount or points? Where did you get that risk normalization formula? Why does the denominator have a 10 in it? Are you using spread only pricing model or core pricing plus commission?

As far as I know, Oanda and LMAX crypto is catagorized as a FOREX symbol, but structured as a Contract For Difference. So I can't help much. Being a US citizen, I am not allowed to trade these products or have a tradeable demo account to test with.

I would think any amount from commission would be negligible compared to the possible huge bid ask spread in the crypto contract price. Which is maybe why their website says that 78% of people trading CFDs lose money.


Return to “MultiCharts”