An Optimal F money management position sizing strategy by Ralph Vince 0
Posted on 17, June 2013
in Category SID
Description for OptimalF SID 512
An Optimal F money management/position sizing strategy. Optimal F Money Management was developed by Ralph Vince as an aggressive way to grow your account balance. Optimal F is simply the optimal amount of capital that should be invested in each trade.
Description
An Optimal F money management/position sizing strategy. Optimal F Money Management was developed by Ralph Vince as an aggressive way to grow your account balance. Optimal F is simply the optimal amount of capital that should be invested in each trade.
Optimal F has it’s critics – with one of the major criticisms being that it is too risky. The money management strategy presented here focuses on the practical issues on how to implement Optimal F for a real trading system, and as such commentary about the wisdom of using Optimal-F is beyond the scope of this money management strategy. There are plenty of resources on the internet that discus Optimal F in great detail.
As the Optimal-F formula uses the trading history of the system in it’s calculation the first issue that we first have to consider is what to do before there are enough trades for the results of Optimal-F to be statistically meaningful. The simplest solution – the one adopted here – is to wait for X trades to occur, and then use the real Optimal-F value. We then recalculate the value of Optimal-F after each trade.
This Optimal-F strategy is controlled by two variables: $mintrades and $defaultf. The value of $mintrades contains the number of trades that have to occur before the real value of Optimal-F is used. If the number of trades is less than $mintrades, the value of $defaultf is used to calculate the size of the position. These values can be changed in the PositionSize function.
All orders are supported with this money management, but for the case of stop and limit orders this money management expects to be passed the stop and limit prices. The actual position size that is calculated is suitable for stocks but with a little tweaking it can be used for futures or forex.
The calculation of Optimal-F that is used within this money management is extremely inefficient.
The logic for this Position Sizing Strategy:
PositionSize:
########################### # Optimal F Money management - read notes for a description # Play around with the vars below to change the settings of this MM # my $mintrades = 20; #Minimum number of trades before optimal-f is used my $defaultf = 0.30; #The value of optimal-f if we havn't got enough trades # # ############################ sub OptimalF { #This function uses a brute force approach to calculate the optimal f #and can be made *much* more efficient. #define our variables my ($BestTWR,$OptimalF,$f,$TWR,$HPR,$WSC); $WSC=LargestLosingTrade; #Loop round for all the values of f - in this case #all values between 1 and 100 foreach my $per (1..100) { $f=$per/100; #Set F to be a percent $TWR=1; #reset the TWR #Loop round all trades for this system foreach my $trade (TradeHistory) { #Work out the HPR and TWR $HPR=1+($f*($trade->NetProfit / $WSC)); $TWR*=$HPR; } #Output "f $f is TWR $TWR"; #Is this the best f so far? if ($BestTWR<$TWR) { $BestTWR=$TWR; $OptimalF=$f; } } #Output "best $BestTWR Optimal F $OptimalF"; #Return the optimal f return $OptimalF; } #Set the optimal-f to the default value my $optimalf=$defaultf; if (TotalTrades> $mintrades) { #we have enough trades - calculate the optimal-f $optimalf=OptimalF; } #Calculate the number of units we should hold for this position my $units=int(($optimalf * LiquidatingValue)/ClosePrice); return $units;
BuyMarket event logic:
if (ShortPosition) { #If we have a short position, close it. PlaceBuyOpen('OptimalF'); } else { #we have no position, or we are long. my $num=PositionSize(); PlaceBuyOpen('OptimalF',$num) if $num; }
BuyStop event logic:
my $stop=shift; #Stop trading if we do not have a stop price die 'No stop price for BuyStop' unless $stop; if (ShortPosition) { #If we have a short position, close it. PlaceBuyStop('OptimalFClosingShort',0,$stop); } else { #we have no position, or we are long. my $num=PositionSize(); PlaceBuyStop('OptimalFGoingLong',$num,$stop) if $num; }
BuyLimit event logic:
my $limit=shift; #Stop trading if we do not have a limit price die 'No limit price for BuyLimit' unless $limit; if (ShortPosition) { #If we have a short position, close it. PlaceBuyLimit('OptimalFClosingShort',0,$limit); } else { #we have no position, or we are long. my $num=PositionSize(); PlaceBuyLimit('OptimalFGoingLong',$num,$limit) if $num; }
BuyStopLimit event logic:
my ($stop,$limit)=@_; #Stop trading if we do not have a stop and limit price die 'No limit price for BuyStopLimit' unless $limit; die 'No stop price for BuyStopLimit' unless $stop; if (ShortPosition) { #If we have a short position, close it. PlaceBuyStopLimit('OptimalFClosingShort',0,$stop,$limit); } else { #we have no position, or we are long. my $num=PositionSize(); PlaceBuyStopLimit('OptimalFGoingLong',$num,$stop,$limit) if $num; }
SellMarket event logic:
if (LongPosition) { #If we have a long position, close it. PlaceSellOpen('OptimalFClosingLong'); } else { #If we have no position or we are short, place the order my $num=PositionSize(); PlaceSellOpen('OptimalFGoingShort',$num) if $num; }
SellStop event logic:
my $stop=shift; #Stop trading if we do not have a stop price die 'No stop price for BuyStop' unless $stop; if (LongPosition) { #If we have a long position, close it. PlaceSellStop('OptimalFClosingLong',0,$stop); } else { #If we have no position or we are short, place the order my $num=PositionSize(); PlaceSellStop('OptimalFGoingShort',$num,$stop) if $num; }
SellLimit event logic:
my $limit=shift; #Stop trading if we do not have a limit price die 'No limit price for BuyStop' unless $limit; if (LongPosition) { #If we have a long position, close it. PlaceSellLimit('OptimalFClosingLong',0,$limit); } else { #If we have no position or we are short, place the order my $num=PositionSize(); PlaceSellLimit('OptimalFGoingShort',$num,$limit) if $num; }
SellStopLimit event logic:
my ($stop,$limit)=@_; #Stop trading if we do not have a stop and limit price die 'No limit price for BuyStopLimit' unless $limit; die 'No stop price for BuyStopLimit' unless $stop; if (LongPosition) { #If we have a long position, close it. PlaceSellStopLimit('OptimalFClosingLong',0,$stop,$limit); } else { #If we have no position or we are short, place the order my $num=PositionSize(); PlaceSellStopLimit('OptimalFGoingShort',$num,$stop,$limit) if $num; }
0 Comments