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

RE: [amibroker] Re: Calculating Position Size Based on a % of Current Equity



PureBytes Links

Trading Reference Links

this is an example from Tomasz awhile back. Dingo put together a collection of documentation on PBT.
 
 
// this statement turns ON custom portfolio backtester procedure
SetOption("UseCustomBacktestProc", True
);

// your formula is run first in actionBacktest

// mode (as in previous versions)

// and after that

// actionPortfolio mode when you have full control

// over backtest process


if( Status("action"
) == actionPortfolio )
{
    
// retrieve the interface to portfolio backtester

    bo =
GetBacktesterObject
();

    
// when using custom backtest routine

    
// we need to begin backtest procedure by

    
// calling PreProcess() method that initializes

    
// internal backtester variables      

    bo.PreProcess();

    
// in our custom backtest loop we must

    
// iterate through all bars and process each bar

    
// signals

    
// an easier way is to use ProcessTradeSignals() method

    
// however there is also an alternative way that

    
// gives even more control.

    
// But for this application ProcessTradeSignals will be just perfect

    
for( bar = 0; bar < BarCount
; bar++ )
    {
         
// this retrieves current value of portfolio-level equity

         CurrentPortfolioEquity = bo.Equity;

         
// the default position size is set by our system code to 25% of

         
// equity, but in this loop we will modify the size based on
         
// current portfolio-level equity


         
// this for loop iterates through all trade signals and adjust pos size

        
for
( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
            {
            
// when our equity grows to $50000 set desired pos size to 20%

             
if( CurrentPortfolioEquity > 50000 ) sig.PosSize = -20;
            
// if above $60K then 18%

             
if( CurrentPortfolioEquity > 60000 ) sig.PosSize = -16
;
            
// if above $80K then 12.5

             
if( CurrentPortfolioEquity > 80000 ) sig.PosSize = -12
;
         }

         
// in simple applications like this

         
// (we only modify some signal parameters but we do not modify the timing)
         
// we will use default processing of the signals

         bo.ProcessTradeSignals( bar );
     }

   
// after finishing trading signal processing loop

     
// we must call PostProcess()
   
// as it calculates necessary statistics and produces trade list and report

    bo.PostProcess();

   
// at this stage backtest is completed

   
// and you can call for example GetPerformanceStats

   
// method to calculate your own metrics from entire backtest

}

f =
Optimize("fast", 12, 1, 30, 1
);
s =
Optimize("slow", 45, 20, 60, 1
);

Buy=Cross(MACD(f,s),Signal
(f,s));
Sell=Cross(Signal(f,s),MACD
(f,s));

SetOption("MaxOpenPositions", 10
);
SetOption("InitialEquity", 40000
);
PositionSize = -25; // default position size of 25%



From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf Of Geoff Mulhall
Sent: Tuesday, 14 March 2006 10:40 AM
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Re: Calculating Position Size Based on a % of Current Equity

Hi again,

Does anyone know how it's done then ??

Is there any documentation anywhere ??

Thanks,

Geoff

--- In amibroker@xxxxxxxxxxxxxxx, "Paul Ho" <paultsho@xxx> wrote:
>
> the example was to illustrate how expectancy is to be calculated
when risk
> is set to 0.01 of equity. it doesnt say how 0.01 is to be achieved,
whether
> it is via position sizing or stoploss or both.

>
>   _____ 
>
> From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx]
On Behalf
> Of Geoff Mulhall
> Sent: Monday, 13 March 2006 3:34 PM
> To: amibroker@xxxxxxxxxxxxxxx
> Subject: [amibroker] Calculating Position Size Based on a % of
Current
> Equity
>
>
> Hi,
>
> In the example provided by Tomasz below I do't see how the risk
> relates to the position size.
>
> When I enter a trade my position size in number of shares is
> calculated as  $ to Risk /(entry price - stop price)
> and I want my $ to risk on the trade to be a % of my currect equity.
>
> I was expecting to see the SetPositionSize used in the example
below
> but it does not appear - so I'm wondering how the Risk value in the
> example below is used to set the position size in the trade.
>
> Any help appreciated.
>
> Geoff
>
> Example follows ->
>
> SetCustomBacktestProc("");
> MaxLossPointStop = 2*ATR(10); // dynamic volatility stoploss, 1st
> modification
>
> function FindEquityAtDateTime( eq, dt, Value )
> {
>    found = -1;
>    for( i = 0; i < BarCount AND found == -1; i++ )
>    {
>       if( dt[ i ] == Value ) found = i;
>    }
>    return IIf( found != -1, eq[ found  - 1 ], Null );
> }
> if( Status("action") == actionPortfolio )
> {
>     bo = GetBacktesterObject();
>     bo.Backtest(1); // run default backtest procedure
>     SumProfitPerRisk = 0;
>     NumTrades = 0;
>     dt = DateTime();
>     eq = Foreign("~~~EQUITY", "C" );
>
>    for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade
> () )
>    {
>       EquityAtEntry = FindEquityAtDateTime( eq, dt,
> trade.EntryDateTime );
>
>       Risk = 0.01 * EquityAtEntry ; //risk is defined as a constant
> 1% of current equity.
>
>       RiskAsPecentOfCurrentEquity = 100 * Risk / EquityAtEntry;
>       RMultiple = trade.GetProfit()/Risk;
>       trade.AddCustomMetric("Initial risk $", Risk  );        
>       trade.AddCustomMetric("Equity at entry", EquityAtEntry );
>       trade.AddCustomMetric("Risk as % of Eq.",
> RiskAsPecentOfCurrentEquity );
>
>       trade.AddCustomMetric("R-Multiple", RMultiple  );
>       SumProfitPerRisk = SumProfitPerRisk + RMultiple;
>       NumTrades++;
>    }
>     Expectancy3 = SumProfitPerRisk / NumTrades;
>     bo.AddCustomMetric( "Expectancy (per risk)", Expectancy3 );
>     bo.ListTrades();
> }
> // your trading system here
>
> ApplyStop( stopTypeLoss, stopModePoint, MaxLossPointStop );
>
>
>
>
>
>
>
> Please note that this group is for discussion between users only.
>
> To get support from AmiBroker please send an e-mail directly to
> SUPPORT {at} amibroker.com
>
> For other support material please check also:
> http://www.amibroker.com/support.html
>
>
>
>
>
>
> SPONSORED LINKS
> Investment
> <http://groups.yahoo.com/gads?
t=ms&k=Investment+management+software&w1=Inves
>
tment+management+software&w2=Real+estate+investment+software&w3=Invest
ment+p
>
roperty+software&w4=Software+support&w5=Real+estate+investment+analysi
s+soft
> ware&w6=Investment+software&c=6&s=200&.sig=_XXUzbE9l5lGlZNcMu4KNQ>
> management software       Real
> <http://groups.yahoo.com/gads?
t=ms&k=Real+estate+investment+software&w1=Inve
>
stment+management+software&w2=Real+estate+investment+software&w3=Inves
tment+
>
property+software&w4=Software+support&w5=Real+estate+investment+analys
is+sof
> tware&w6=Investment+software&c=6&s=200&.sig=5_sgDczz3ArKGMtJ9tFSJA>
estate
> investment software       Investment
> <http://groups.yahoo.com/gads?
t=ms&k=Investment+property+software&w1=Investm
>
ent+management+software&w2=Real+estate+investment+software&w3=Investme
nt+pro
>
perty+software&w4=Software+support&w5=Real+estate+investment+analysis+
softwa
> re&w6=Investment+software&c=6&s=200&.sig=_N6zcwefgp4eg5n6oX5WZw>
property
> software      
> Software
> <http://groups.yahoo.com/gads?
t=ms&k=Software+support&w1=Investment+manageme
>
nt+software&w2=Real+estate+investment+software&w3=Investment+property+
softwa
>
re&w4=Software+support&w5=Real+estate+investment+analysis+software&w6=
Invest
> ment+software&c=6&s=200&.sig=MJ2jP31F3n64RDZkDadU8w> support       Real
> <http://groups.yahoo.com/gads?
t=ms&k=Real+estate+investment+analysis+softwar
>
e&w1=Investment+management+software&w2=Real+estate+investment+software
&w3=In
>
vestment+property+software&w4=Software+support&w5=Real+estate+investme
nt+ana
>
lysis+software&w6=Investment+software&c=6&s=200&.sig=GmF8PlAJASx0wrSaX
5-Zlw>
> estate investment analysis software       Investment
> <http://groups.yahoo.com/gads?
t=ms&k=Investment+software&w1=Investment+manag
>
ement+software&w2=Real+estate+investment+software&w3=Investment+proper
ty+sof
>
tware&w4=Software+support&w5=Real+estate+investment+analysis+software&
w6=Inv
> estment+software&c=6&s=200&.sig=aMgGsKT4w29dMAYUzQUKzg> software
     
>
>   _____ 
>
> YAHOO! GROUPS LINKS
>
>
>      
> *      Visit your group "amibroker
> <http://groups.yahoo.com/group/amibroker> " on the web.
>  
>
> *      To unsubscribe from this group, send an email to:
>  amibroker-unsubscribe@xxxxxxxxxxxxxxx
> <mailto:amibroker-unsubscribe@xxxxxxxxxxxxxxx?subject=Unsubscribe>
>  
>
> *      Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service
> <http://docs.yahoo.com/info/terms/> .
>
>
>   _____
>






Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





SPONSORED LINKS
Investment management software Real estate investment software Investment property software
Software support Real estate investment analysis software Investment software


YAHOO! GROUPS LINKS