Hi all,
I have written a simple trend following strategy that works across multiple markets. A Stoploss is placed behind most recent swing high/low and then a position sizing function uses the distance from the entry to the stop to set position size according to maximum permissible risk. However when running this strategy in realtime I have been finding that position sizes are often much larger than the 1% risk allowance. For example I set my account size to 36,000 so a 1% max position size should be capped at 360. This morning the entered position had a risk of 1547 (4.3% of account size). The code for the strategy is pasted below. Does anyone have any ideas as to why the position sizing function is working incorrectly?
Thanks
Roj
// code-parameter
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM FlatAfter = 210000
// window high/low calculation
StartTime = 090000
EndTime = 092000
// Spread
sp = 1
// money management
Capital = 36000
Risk = 0.01
equity = Capital + StrategyProfit
maxrisk = round(equity*Risk)
PositionSizeL = abs(round((maxrisk/StopLossL)/PointValue)*pipsize)
PositionSizeS = abs(round((maxrisk/StopLossS)/PointValue)*pipsize)
// calculate high/low
IF Time >= StartTime AND Time <= EndTime THEN
orh = HIGHEST[20](high)
orl = LOWEST[20](low)
ENDIF
// Count number of trades
TradeCounter = 0
// position management
// Long
IF Not ONMARKET AND close CROSSES OVER orh AND TradeCounter < 5 THEN
// long
IF (Time >= 90000 AND Time <= 150000) THEN
BUY PositionSizeL PERPOINT AT MARKET
StopLossL = abs(close - orl) + sp
set stop loss StopLossL
SET TARGET PROFIT 150 * pipsize
TradeCounter = TradeCounter + 1
ENDIF
ENDIF
// short
IF Not ONMARKET AND close CROSSES UNDER orl AND TradeCounter < 5 THEN
IF (Time >= 90000 AND Time <= 150000) THEN
SELLSHORT PositionSizeS PERPOINT AT MARKET
StopLossS = abs(close - orh) + sp
set stop loss StopLossS
SET TARGET PROFIT 150 * pipsize
TradeCounter = TradeCounter + 1
ENDIF
ENDIF
I figured it out. Just need to remove the
>*pipsize
multiplier at the end – was redundant
>PositionSizeL = abs(round((maxrisk/StopLossL)/PointValue))
works as anticipated