[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GEN: computing optimal f via spreadsheet



PureBytes Links

Trading Reference Links

Below is a post I submitted to another list a while back, describing how to
calculate Optimal_F in TradeStation. Perhaps you will find it useful.
Attached is an ELA file.

Bob Fulks

-------------


On Dec 18,  9:17am, Gary Fritz wrote:

>> Does anybody have a nice piece of code to calculate opt-f and Risk of
>> Ruin on a system?  If not, I'll have to see if I can toss one
>> together.


There is an easy way to calculate Optimal_f with TradeStation. This post
will illustrate it with an example.

What Ralph Vince's Optimal_f calculation does is to compute what fraction
of your trading account balance you should risk in each trade such that
your account balance is the maximum possible value after taking all the
trades specified by your trading system. He calls this the "TWR" for
"Terminal Wealth Relative"

He defines "Optimal_f" as a fraction of the biggest losing trade of the
historical series of trades. Obviously, this fraction will be less than 1.0
since a bet equal to your biggest losing trade will assure that you lose
all your money on that losing trade.

You can use the Optimization function of TradeStation to search for this
optimum quite easily.

However, it is a little tricky since you need to create an input to the
trading system that you can use to run through a sequence of values with
the Optimization procedure. But since you do not know the size of the
largest losing trade before you run the test, you cannot search for
Optimal_f directly.

So I created a variable which I call "Leverage". This is defined as the
dollar value of the trade as a multiple of the value of your trading
account. It is usually greater than one when trading stocks and can be less
than one when trading futures.

Using a "Leverage" of 1.0 means you invest your total trading account on
each trade. This is what you would do if you used the total balance of your
account to buy shares of a stock, leaving no cash in the trading account.

Using a leverage of 2.0 means that you borrow an amount equal to your
trading account and invest two times the value of your account on each
trade. In the example below, the Optimal_f occurs with a leverage of about
5.4 which means you would be borrowing 4.4 times the value of your trading
account to trade.

To demonstrate this I used a simple trading system that buys the daily SPX
cash index on Monday at the Open and exits on Friday at the Close. The
dollar value of each Monday's trade is equal to the size of the trading
account at the end of the previous week multiplied by the "Leverage" input.

We can then optimize this system for the value of "Leverage" that results
in the highest "Total Net Profit". Then we can calculate the value of
Optimal_f from the resulting system performance parameters. The attached
chart shows the graph from the optimization report resulting from
optimizing on "leverage" over the range of 1 to 9. The curve is not
perfectly smooth because TradeStation rounds the number of shares traded to
an integer number. (The code for this system is appended below and attached
as an ELA file.)

Two "modes" of operation are provided based upon an input parameter:

  > Mode = 0

    The "Unequalized" mode assumes the following relationship:

        Trading_account / Shares = a constant for all trades
                                    = Biggest_Loss / Optimal_F

    This is the case most often mentioned in the books. In this example,
    the value of about 5.3 for "Leverage" resulted in the maximum
    net profit of about $23,600. This corresponds to an Optimal_f of
    about 0.31 (See the print log). It gives the same results as all
    three of the methods described in Vince's, "Portfolio Management
    Formulas", Chapter 4.

  > Mode = 1

    The "Equalized" mode assumes the following relationship:

        (Trading_account / Shares) * (Initial_share_price / Share_Price)
                            = a constant for all trades
                            = Biggest_Loss / Optimal_F

    This case is described in Ralph Vince's book, "The Mathematics of
    Money Management" page 83. (I understand from private correspondences
    with Ralph that it is also covered in more detail in his latest book.)

In this example, the value of about 6.4 for "Leverage" resulted in the
maximum net profit of about $28,500. This corresponds to an Optimal_f of
about 0.29 (See the print log)

The usual problem with trading at Optimal_f is the drawdowns. It
mathematically will result in the highest return IF THE FUTURE STATISTICS
OF THE MARKET YOU ARE TRADING ARE EQUAL TO THOSE OF THE PAST. This is a big
assumption for most people.

This example illustrates this fact in a crude way. This trading system is
sort of like a buy/hold system for an S&P index fund (except for weekends).
To trade at Optimal_f, for an account size of $20,000 you would need to
borrow about $100,000 and buy about $120,000 worth of an S&P index fund.
Would anybody you know want to buy/hold the S&P for the past year with that
kind of leverage?

The system/market has a Sharpe Ratio of about 0.6 (bad) with an annualized
return of 130% (great) but with a standard deviation of returns of 220%
(terrible). So the peaks and valleys of the equity curve would be enormous.

Hope this has been useful.

Bob Fulks

---------

{ *******************************************************************

   System:       _Optimal_f_Demo

   Last Edit:    12/27/98

   Coded By:     Bob Fulks

  Description: This system buys on the open on Mondays and exits
     on the close of Fridays. The size of each trade is determined
     by the size of the trading account times the input called
     "Leverage". No correction was made for Monday/Friday holidays.

  Inputs:

     Leverage - This is the input used to iterate through the
       sequence of values using the TradeStation Optimization
       operation. Optimize for Total Net Profit. This results in
       the conditions for Optimal_f.

       Use the value = 0 (zero) to force the system to trade one
       share for all trades. This can be used to create the series
       of trades requires to calculate Optimal_f by the methods
       in Vince's books.

     PrntMode - Set to zero for no detail, Set > zero for detail.

     Mode - Set to zero to optimize by the "Unequalized" method. This
       sets the parameters based upon the price at the first trade
       and lets the actual leverage vary as required. The results
       duplicate the methods described by Vince in his books. It
       trades one share of stock for each:

            Biggest_loss_per_share / Optimal_f

       we have in our account. (This value is a constant for all
       trades.) The actual leverage used will vary with each trade.

       Set to non-zero to use the "Equalized" method. This dynamically
       adjusts the shares traded on each bar while holding the
       leverage constant over all bars. This method is described by
       Vince in is 1992 book, page 83. The leverage used will be a
       constant for all trades.

********************************************************************}

Input:Leverage(1),  {Leverage used}
     PrntMode(0),   {PrintMode: No detail = 0, Detail > 0}
     Mode(0);       {Unequalized = 0, Equalized method <> 0}

Vars:Value(20000),  {Beginning value of trading account}
     NetValue(0),   {Net value of trading account every bar}
     Invest(0),     {Amount invested on each trade}
     TShares(0),    {Number of shares/contracts bought each trade}
     PrPerShare(0), {Profit per share}
     OptF(0),       {Optimal_f calculated}
     SClose(0),     {Close on first Friday}
     BLoss(0),      {Biggest loss per share}
     ActLever(0),   {Actual leverage used on each trade}
     First(TRUE);   {True on first Friday only}

if CurrentBar = 1 and Leverage = 0 then Value = Close * BigPointValue;

NetValue = Value + NetProfit + OpenPositionProfit;

if DayOfWeek(Date) = 5 then begin

  {Calculate SClose on first trade}
  if Mode = 0 then begin
     if First then begin
        SClose = Close;
        First = FALSE;
     end;
  end else SClose = Close;

  ExitLong at Close;

  {Calculate biggest loss per share}
  if TShares <> 0 then PrPerShare = PositionProfit(0) / TShares;
  BLoss   = iff(PrPerShare < BLoss, PrPerShare, BLoss);

  {Calculate number of shares to buy}
  Invest = iff(NetValue >= 0, Leverage * NetValue, 0);
  TShares = iff(Leverage = 0, 1, Round(Invest/SClose,0));
  Buy TShares shares next bar at market;

  if PrntMode > 0 then begin
     if NetValue <> 0 then ActLever = TShares * Close / NetValue;
     Print("  ", Date:6:0, " B", Close:5:2, NetValue:8:0, Invest:8:0,
     TShares:5:0, PrPerShare:6:2, BLoss:4:2, ActLever:4:2, "  ");
  end;
end;

{Calculate & print Optimal_f from performance data}
if LastBarOnChart then begin
  OptF = -Leverage * BLoss / SClose;
  Print(Leverage:4:3, NetProfit:8:0, BLoss:4:2, Close:5:2, OptF:3:4);
end;

Attachment converted: Macintosh HD:Leverage.gif (GIFf/JVWR) (0006FB3D)
Attachment Converted: "c:\eudora\attach\Optf4.ela"