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

Re: AW: Monte Carlo Simulations



PureBytes Links

Trading Reference Links

This is a function (with a signal) for Montecarlo's process.
It should be work, but it's freezes my TS2000...could an EL programmer of
the
list to verify if it's good or not (and modify it if it's necessary)?
Mario


{
  User function: MonteCarlo
  Monte Carlo simulation for position sizing.

  This function records the profits and losses from the trading system that
calls it
  and generates random sequences of the trades. For each sequence, it
  calculates the return and drawdown assuming a fixed percent of the account
is
  risked on each trade. The result is the probability that with a given
  account size and risk percentage you can meet given rate of return and
drawdown
  goals. For example, it might calculate the probability that you can
achieve
  a rate of return of 50% with 30% drawdown by risking 10% on each trade.

  ATTENTION TRADESTATION 4.0 USERS
  Because this function calls the built-in function Random, which is new to
  TradeStation 2000i, this function will not work in TradeStation 4.0.
  I have developed a version of MonteCarlo with additional functionality and
  several input and output options that will work with either TradeStation
2000i
  or 4.0. This version can be purchased on my web site.

  Copyright 2001 Breakout Future
  www.BreakoutFutures.com
  mrb@xxxxxxxxxxxxxxxxxxx
 }

 Input:  ASize   (NumericSimple),   { account size, $ }
         RetGoal (NumericSimple),   { rate of return goal, %}
         DDGoal  (NumericSimple),   { max closed out trade drawdown, % }
         RiskPer (NumericSimple),   { percentage risk per trade }
         TrRisk  (NumericSeries),   { risk for current trade, $ }
         NRand   (NumericSimple);   { number of random sequences }

 Array: Trades[2000](0),     { Trades, $ (win +; loss -) }
        TrRisks[2000](0),    { trade risks, $ (+) }
        RandNums[2000](0),   { array of random numbers }
        RandIndx[2000](-1);  { randomly chosen indices for trade array }

 Var:   NTrades (0),        { Number of trades }
        iSeq    (0),        { sequence number }
        MaxNum  (0),        { max number left in random number array
RandNums }
        iMax    (0),        { index (location) of MaxNum in array RandNums }
        NCon    (0),        { number of contracts }
        Equity  (0),        { account equity }
        NewEquity   (0),    { account equity }
        EqtyHigh    (0),    { highest equity }
        DD      (0),        { closed trade drawdown }
        DDmax   (0),        { worst case closed trade drawdown }
        PReturn (0),        { percent return }
        AveRet  (0),        { average percent return }
        ProbRet (0),        { probability of return goal }
        ProbDD  (0),        { probability of drawdown goal }
        ProbComb(0),        { probability of return & DD goals }
        ii      (0),        { loop counter }
        jj      (0),        { loop counter }
        k       (0);        { loop counter }

 { Collect profit/loss and risk }
 If TotalTrades > NTrades then Begin
    If TotalTrades - NTrades < 2 then Begin
        NTrades = NTrades + 1;
        Trades[NTrades - 1] = PositionProfit(1);
        TrRisks[NTrades - 1] = TrRisk;
    end
    else Begin { case where TotalTrades increments by 2 from one bar to
next }
        NTrades = NTrades + 1;
        Trades[NTrades - 1] = PositionProfit(2);
        TrRisks[NTrades - 1] = TrRisk[1];
        NTrades = NTrades + 1;
        Trades[NTrades - 1] = PositionProfit(1);
        TrRisks[NTrades - 1] = TrRisk;
    end;
 end;

 { Calculate results 1 day from last bar on chart }
  If Date = JulianToDate(DateToJulian(LastCalcDate) - 1) {LastBarOnChart}
then Begin

     for iSeq = 1 to NRand Begin                { loop over # of random
sequences }

        { Generate random sequence of trades }
        For ii = 0 to NTrades - 1 Begin         { generate random numbers }
            RandNums[ii] = IntPortion(NTrades * Random(10));
        end;
        For ii = 0 to NTrades - 1 Begin         { sort index of RandNums
array }
            MaxNum = -1;
            For jj = 0 to NTrades - 1 Begin     { find biggest remaining
number }
                If RandNums[jj] > MaxNum then Begin
                   MaxNum = RandNums[jj];
                   iMax = jj;
                end;
            end;
            RandIndx[ii] = iMax;                { record location of max
number }
            RandNums[iMax] = -1;                { "remove" max number from
array }
        end;

        { Calculate account balance and drawdown for current sequence of
trades }
        Equity = ASize;
        EqtyHigh = Equity;
        DDmax = 0;
        For ii = 0 to NTrades - 1 Begin
            NCon = IntPortion(RiskPer/100 * Equity/TrRisks[RandIndx[ii]]);
            NewEquity = Equity + NCon * Trades[RandIndx[ii]];

            { Calculate closed trade percent drawdown }
            If (NewEquity > EqtyHigh) then
                EqtyHigh = NewEquity
            else Begin
                DD = 100 * (EqtyHigh - NewEquity)/EqtyHigh;
                if (DD > DDmax) then
                    DDmax = DD;
            end;
            Equity = NewEquity;
        end; { for ii }
        PReturn = 100 * (Equity - ASize)/ASize;

        { Accumulate results for probability calculations }
        AveRet = AveRet + PReturn;
        if (PReturn >= RetGoal) then
           ProbRet = ProbRet + 1;
        if (DDmax <= DDGoal) then
           ProbDD = ProbDD + 1;
        if (PReturn >= RetGoal and DDmax <= DDGoal) then
           ProbComb = ProbComb + 1;

     End; { for iSeq }

     { Calculate probabilities by dividing sums by number of sequences }
     AveRet = AveRet/NRand;
     ProbRet = 100 * ProbRet/NRand;
     ProbDD = 100 * ProbDD/NRand;
     ProbComb = 100 * ProbComb/NRand;
     MessageLog("RiskPer=", RiskPer:5:1,"  RetGoal=", RetGoal:5:1,"
DDGoal=", DDGoal:5:1,
        "  ProbRet=",ProbRet:5:1,"  ProbDD=",ProbDD:5:1,"  ProbComb=",
ProbComb:5:1);
     MessageLog("Average Return = ", AveRet:6:2);
 end;

 MonteCarlo = 1;

 {
  EasyEmini
  A simple short-term e-mini breakout system to illustrate the use of
  Monte Carlo simulation for position sizing.

  System: Buy at a fraction of yesterday's range from yesterday's high. Hold
  1 day then look to exit on the first profitable open. Use a fixed money
  management stop (nominally $1000 in the e-mini).

  WARNING: This system looks ahead one day to the next day's open to decide
whether
  to exit the open position. Consequently, the system never executes on the
last bar
  of data on the chart. This means that the entry order for the next day
will not
  appear in the TradeStation Tracking Center.

  ATTENTION TRADESTATION 2000i USERS
  This system includes a call to function MonteCarlo, which performs Monte
Carlo
  simulation for position sizing. MonteCarlo only works in TradeStation
2000i
  because it uses built-in function Random, which is new to 2000i. I have
  commented out the call to MonteCarlo so that this system will work
properly for
  TradeStation 4.0 users. If you are a TradeStation 2000i user, you can
download
  function MonteCarlo for free from my web site (address below). Remove the
  comment braces {..} from the call to the line below that begins "Value1 =
...".
  This will enable the call to MonteCarlo.
    The MonteCarlo code records the profits and losses from the trading
system and
  generates random sequences of the trades. For each sequence, it
  calculates the return and drawdown assuming a fixed percent of the account
is
  risked on each trade. The output is the probability that with a given
  account size and risk percentage you can meet given rate of return and
drawdown
  goals. For example, it might calculate the probability that you can
achieve
  a rate of return of 50% with 30% drawdown by risking 10% on each trade.
    I have developed a version of MonteCarlo with additional functionality
and
  several input and output options that will work with either TradeStation
2000i
  or 4.0. This version can be purchased on my web site.

  Copyright 2001 Breakout Future
  www.BreakoutFutures.com
  mrb@xxxxxxxxxxxxxxxxxxx


 Input: ASize    (30000),   { account size, $ }
        RetGoal  (50),      { rate of return goal, %}
        DDGoal   (30),      { max closed out trade drawdown, % }
        RiskPer  (10),      { percentage risk per trade }
        EntFrac  (0.75),    { multiple of prior day's range for entering }
        MMStop   (1000),    { money management stop, $ }
        NRand    (1000);    { number of random sequences }

 Var:   EntPr    (0),       { Entry target price }

        XitPr    (0),       { mm stop exit price }
        TrRisk   (0);       { trade risk, $ }

 TrRisk = MMStop;

 { Entry conditions }
 EntPr = H + EntFrac * (H - L);
 If C > C[1] then
    Buy next bar at EntPr Stop;

 {
 { Exit conditions }
 Exitlong("MMStop") next bar at EntryPrice - MMStop/BigPointValue stop;
 If BarsSinceEntry >= 1 and open of next bar > EntryPrice then
    ExitLong("ProfOpen") next bar at market;}

 { Perform Monte Carlo analysis: MonteCarlo only works in TradeStation 2000i
 Value1 = MonteCarlo(ASize, RetGoal, DDGoal, RiskPer, TrRisk, NRand); }
}

----- Original Message -----
From: "Clive" <clivesmail@xxxxxxxxxxx>
To: "DH" <catapult@xxxxxxxxxxxxxxxxxx>
Cc: "Omega List" <omega-list@xxxxxxxxxx>
Sent: Saturday, April 20, 2002 2:25 PM
Subject: Re: AW: Monte Carlo Simulations


> Dennis
>
> I am starting to look into this and would appreciate if you could give
some
> more explicit details as how you do the simulation. Is it an add-on to TS
or
> a plugin for excel
>
> Thanks
>
>
> ----- Original Message -----
> From: "DH" <catapult@xxxxxxxxxxxxxxxxxx>
> To: "Omega List" <omega-list@xxxxxxxxxx>
> Sent: 20 April 2002 13:02 PM
> Subject: Re: AW: Monte Carlo Simulations
>
>
> | > What do you think is the right way of doing MC?
> |
> | I can't really comment on the portfolio approach. That gets complicated.
> | I trade index futures and I use the MC individually on each system. But
> | anyway, here's the general idea of how it works.
> |
> | First, you need to generate a list of profits and losses for the
> | backtest. Each backtested trade should risk an equal dollar amount.
> | That's the tricky part because the "risk" is defined differently in
> | different systems. Generally speaking, it's the maximum amount you can
> | lose on the trade. I usually use a large disaster stop based on ATR so
> | it's easy in that case.
> |
> | risk/trade = contracts * disaster stop
> | risk/contract = X * ATR
> | contracts (shares) to trade = Y * account size / ATR
> | solve for Y
> |
> | Once you have your list of trades (each trade adjusted for equal dollar
> | risk) you calculate the max drawdown. Store that number, shuffle the
> | trades into a random order, calculate the MaxDD again, and store that
> | number. Repeat a whole bunch of times - 10,000 is good. After you have
> | the list of 10K drawdowns, sort them in order and play with the numbers
> | statistically.
> |
> | In the end, you should generate a table like the one below. This is for
> | a real system. Every system will be different. Notice how trading just a
> | little bit smaller can dramatically reduce the risk of blowing out the
> | account with this particular system. I consider that valuable
> | information.
> |
> | As an aside, using Vince's methods, I get an Optimal_F of 26% which is
> | consistent with the MC results. The MC says, if I risk 26% on each
> | trade, there is a 99% chance of going broke. Vince fans will say you
> | should find Opt_F and then back off the leverage some until you feel
> | comfortable. The problem is they don't give you any way to calculate
> | "some." The MC helps you decide how much of the account to risk to stay
> | within your own particular comfort level.
> |
> | Probability of ruin (drawdown bigger than account size)
> | Percent of account risked on each trade
> | 0.01% 5.3%
> | 0.1% 6.1%
> | 0.2% 6.7%
> | 0.3% 7.0%
> | 0.4% 7.2%
> | 0.5% 7.4%
> | 0.6% 7.5%
> | 0.7% 7.7%
> | 0.8% 7.8%
> | 0.9% 7.9%
> | 1% 7.9%
> | 2% 8.6%
> | 3% 9.1%
> | 4% 9.5%
> | 5% 9.7%
> | 6% 10.0%
> | 7% 10.2%
> | 8% 10.4%
> | 9% 10.6%
> | 10% 10.8%
> | 11% 11.0%
> | 12% 11.1%
> | 13% 11.2%
> | 14% 11.4%
> | 15% 11.5%
> | 16% 11.7%
> | 17% 11.8%
> | 18% 11.9%
> | 19% 12.1%
> | 20% 12.2%
> | 25% 12.8%
> | 30% 13.3%
> | 35% 13.8%
> | 40% 14.3%
> | 45% 14.8%
> | 50% 15.3%
> | 55% 15.8%
> | 60% 16.3%
> | 65% 16.8%
> | 70% 17.4%
> | 75% 18.0%
> | 80% 18.7%
> | 85% 19.6%
> | 90% 20.6%
> | 95% 22.3%
> | 99% 25.4%
> | 99.9% 28.8%
> | 99.99% 32.5%
> |
> | --
> |   Dennis
> |
> |
>
>