Testing Optimal f
Posted: Mon Dec 11, 2006 8:49 pm
Well I was curious to program a system that would calculate the unit size using the "optimal f" calculation.
It appear that it look good when you apply it to your system when you know what was your biggest losing trade. However In trying to apply it in an ongoing trading journey, as per the included script does. This script look at the past history of trade as it is advencing in time, and calculate the current Optimal f, given what we know at that time. As you will see and if you experiment, hopefully you wont encounter, a bigger (biggest losing trade) or you will go bankrupt pretty fast.
Any comment, anybody have traded with this "Optimal f"
The result in the table below are for a sample trading system that follow trend line. The contract is Feeder Cattle.
The First Chart is for a single contract per trade.
The second is volatility adjusted
The third is with Optimal f (and unfortunately the 3rd trade was a bigger loser than the first one)
Also you have the modified Multi money manager that implement the Optimal f
==================================
VARIABLES: adjustedEquity, riskEquity, dollarRisk TYPE: Money
VARIABLES: loopf,Optimalf,p TYPE: FLOATING
VARIABLES: looptrade TYPE: integer
VARIABLES: HPR,TWR,PREVTWR TYPE: FLOATING
VARIABLES: LargestLosing TYPE:FLOATING
IF algorithm = FIXED_FRACTIONAL THEN
' Compute the risk equity.
riskEquity = system.tradingEquity * riskPerTrade
' Compute the dollar risk.
dollarRisk = entryRisk * instrument.bigPointValue
' Adjust the unit to this percentage.
IF dollarRisk = 0 THEN
unitSize = 1
ELSE
unitSize = riskEquity / dollarRisk
ENDIF
ELSE IF algorithm = SINGLE_CONTRACT THEN
' Just use a single contract.
unitSize = 1
ELSE IF algorithm = VOLATILITY_ADJUSTED THEN
' Compute the risk equity.
riskEquity = system.tradingEquity * riskPerTrade
' Compute the dollar risk.
dollarRisk = averageTrueRange * instrument.bigPointValue
' Adjust the unit to this percentage.
IF dollarRisk = 0 THEN
unitSize = 1
ELSE
unitSize = riskEquity / dollarRisk
ENDIF
ELSE IF algorithm = OPTIMAL_F THEN
LargestLosing = 0
for looptrade = 1 to test.totaltrades+1
if test.tradeprofit[looptrade] < LargestLosing THEN
largestLosing = test.tradeprofit[looptrade]/test.tradequantity[looptrade]
endif
next
PRINT "============================================================"
PRINT "Date= " + test.currentdate + " total trade = " + test.totaltrades + " LargestLosing = " + largestlosing
if LargestLosing < 0 and test.totaltrades >= 2 THEN
loopf = 0.00
TWR = 1
PREVTWR = 0
while TWR >= PREVTWR
PREVTWR = TWR
loopf=loopf+0.01
for looptrade = test.totaltrades to 1 step -1
HPR = 1 + (loopf * (-test.tradeprofit[looptrade] / largestlosing))
TWR = TWR * HPR
PRINT "loopf=" + loopf + " HPR=" + HPR + " TWR= " + TWR + " Trade Proft=" + test.tradeprofit[looptrade] + " Largest losing=" + largestlosing
next
endwhile
unitsize = asinteger(system.tradingequity / (abs(largestlosing) / loopf))
ELSE
unitsize = 1
ENDIF
PRINT "UNITSIZE ======> " + unitsize + " Optimal f = " + Loopf
ENDIF
' Set to filter the trade if the unitsize is zero
IF unitSize = 0 THEN unitSize = FILTER
It appear that it look good when you apply it to your system when you know what was your biggest losing trade. However In trying to apply it in an ongoing trading journey, as per the included script does. This script look at the past history of trade as it is advencing in time, and calculate the current Optimal f, given what we know at that time. As you will see and if you experiment, hopefully you wont encounter, a bigger (biggest losing trade) or you will go bankrupt pretty fast.
Any comment, anybody have traded with this "Optimal f"
The result in the table below are for a sample trading system that follow trend line. The contract is Feeder Cattle.
The First Chart is for a single contract per trade.
The second is volatility adjusted
The third is with Optimal f (and unfortunately the 3rd trade was a bigger loser than the first one)
Also you have the modified Multi money manager that implement the Optimal f
==================================
VARIABLES: adjustedEquity, riskEquity, dollarRisk TYPE: Money
VARIABLES: loopf,Optimalf,p TYPE: FLOATING
VARIABLES: looptrade TYPE: integer
VARIABLES: HPR,TWR,PREVTWR TYPE: FLOATING
VARIABLES: LargestLosing TYPE:FLOATING
IF algorithm = FIXED_FRACTIONAL THEN
' Compute the risk equity.
riskEquity = system.tradingEquity * riskPerTrade
' Compute the dollar risk.
dollarRisk = entryRisk * instrument.bigPointValue
' Adjust the unit to this percentage.
IF dollarRisk = 0 THEN
unitSize = 1
ELSE
unitSize = riskEquity / dollarRisk
ENDIF
ELSE IF algorithm = SINGLE_CONTRACT THEN
' Just use a single contract.
unitSize = 1
ELSE IF algorithm = VOLATILITY_ADJUSTED THEN
' Compute the risk equity.
riskEquity = system.tradingEquity * riskPerTrade
' Compute the dollar risk.
dollarRisk = averageTrueRange * instrument.bigPointValue
' Adjust the unit to this percentage.
IF dollarRisk = 0 THEN
unitSize = 1
ELSE
unitSize = riskEquity / dollarRisk
ENDIF
ELSE IF algorithm = OPTIMAL_F THEN
LargestLosing = 0
for looptrade = 1 to test.totaltrades+1
if test.tradeprofit[looptrade] < LargestLosing THEN
largestLosing = test.tradeprofit[looptrade]/test.tradequantity[looptrade]
endif
next
PRINT "============================================================"
PRINT "Date= " + test.currentdate + " total trade = " + test.totaltrades + " LargestLosing = " + largestlosing
if LargestLosing < 0 and test.totaltrades >= 2 THEN
loopf = 0.00
TWR = 1
PREVTWR = 0
while TWR >= PREVTWR
PREVTWR = TWR
loopf=loopf+0.01
for looptrade = test.totaltrades to 1 step -1
HPR = 1 + (loopf * (-test.tradeprofit[looptrade] / largestlosing))
TWR = TWR * HPR
PRINT "loopf=" + loopf + " HPR=" + HPR + " TWR= " + TWR + " Trade Proft=" + test.tradeprofit[looptrade] + " Largest losing=" + largestlosing
next
endwhile
unitsize = asinteger(system.tradingequity / (abs(largestlosing) / loopf))
ELSE
unitsize = 1
ENDIF
PRINT "UNITSIZE ======> " + unitsize + " Optimal f = " + Loopf
ENDIF
' Set to filter the trade if the unitsize is zero
IF unitSize = 0 THEN unitSize = FILTER