Hello,

I am trying to code for finding optimal f. Since i m not good in writing codes, could anyone provide some guidance to me? Thank you for your help.

Hello,

I am trying to code for finding optimal f. Since i m not good in writing codes, could anyone provide some guidance to me? Thank you for your help.

You can find some guidance here:

http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

No afl but there was an interesting discussion here,

https://groups.yahoo.com/neo/groups/amibroker/conversations/messages/193330

1 Like

Is there any way to access that forum?

I´ve requested permission some time ago and still nothing.

Oops!

You need to be a member to perform this action.

All links from here to there are broken.

Or maybe I am missing something?

```
function _EQ_OptimalF(Data,Watchday)
{
SetBarsRequired(sbrAll,sbrAll);
PR=data-Ref(data,-1);
Intrade= abs(DATA-Ref(DATA,-1))>0;
MAXLOSS=99999;
for (i=1; i<=watchday;i++)
{ VarSet("VAR"+i,ValueWhen(Intrade,PR,i));
MAXLOSS=Min(MAXLOSS,VarGet("VAR"+i));
}
MX=IIf(MAXLOSS>=0,-1,MAXLOSS);
MAXF=0;
maxmean=-99999;
times=-100;
passday=Cum(intrade);
Optimalf=0;
for(f=-100;f<=100;f++)
{
HPR=1;
TWS=1;
for(t=1; t<=watchday; t++)
{
HPR=HPR*( 1+(f/100)*-Nz((VarGet("VAR"+t)/MX)));
}
TWS=HPR^(1/watchday);
for (i=0 ; i<BarCount ; i++)
{ if (MaxMean[i]<TWS[i])
{ MaxMean[i]=TWS[i];
MAXF[i]=F;
}
}
}
for (i=0;i<BarCount;i++)
{ if (passday[i]>watchday)
Optimalf[i]=MaxF[i];
else
Optimalf[i]=null;
}
return Optimalf;
}
```

I seem to remember there was an error in Vince’s first book Portfolio Management Formulas in his example optimal f calc. Anyways, while an interesting exercise, it is based on a stream of mutually chronologically successive trades. While the chronological nature doesn’t matter, the successive nature does. Meaning each trade concludes before the next n+1 trade happens.

If real life, we have more than one trade active concurrently. IMHO you should study portfolio heat as well.

Another thing you can do is treat the portfolio equity (whatever interval) changes as a stream of consecutive trades and calculate the optimal f on that. Though your largest losing day (or other interval) will usually be larger in the future, so you are right back to the initial issue, that is unless you trade long options.

-S

2 Likes