PureBytes Links
Trading Reference Links
|
Hi Andrew,
Are you trying to do this in RT trading? If so, I don't trade RT, so I don't
know much about AB's RT capabilities - I don't know if it gives you any RT
access to your account equity as it changes through the day...
Maybe you could modify this code from the help file to fit your needs?
Position sizing
This is a new feature in version 3.9. Position sizing in backtester is
implemented by means of new reserved variable
PositionSize = <size array>
Now you can control dollar amount or percentage of portfolio that is
invested into the trade
a.. positive number define (dollar) amount that is invested into the trade
for example:
PositionSize = 1000; // invest $1000 in every trade
b.. negative numbers -100..-1 define percentage:
-100 gives 100% of current portfolio size,
-33 gives 33% of available equity for example:
PositionSize = -50; /* always invest only half of the current equity */
c.. dynamic sizing example:
PositionSize = - 100 + RSI();
as RSI varies from 0..100 this will result in position depending on RSI
values -> low values of RSI will result in higher percentage invested
If less than 100% of available cash is invested then the remaining amount
earns interest rate as defined in the settings.
There is also a new checkbox in the AA settings window: "Allow position size
shrinking" - this controls how backtester handles the situation when
requested position size (via PositionSize variable) exceeds available cash:
when this flag is checked the position is entered with size shinked to
available cash if it is unchecked the position is not entered.
To see actual position sizes please use a new report mode in AA settings
window: "Trade list with prices and pos. size"
For the end, here is an example of Tharp's ATR-based position sizing
technique coded in AFL:
Buy = <your buy formula here>
Sell = 0; // selling only by stop
TrailStopAmount = 2 * ATR( 20 );
Capital = 100000; /* IMPORTANT: Set it also in the Settings: Initial Equity
*/
Risk = 0.01*Capital;
PositionSize = (Risk/TrailStopAmount)*BuyPrice;
ApplyStop( 2, 2, TrailStopAmount, 1 );
The technique could be summarized as follows:
The total equity per symbol is $100,000, we set the risk level at 1% of
total equity. Risk level is defined as follows: if a trailing stop on a $50
stock is at, say, $45 (the value of two ATR's against the position), the $5
loss is divided into the $1000 risk to give 200 shares to buy. So, the loss
risk is $1000 but the allocation risk is 200 shares x $50/share or $10,000.
So, we are
allocating 10% of the equity to the purchase but only risking $1000. (Edited
excerpt from the AmiBroker mailing list)
----- Original Message -----
From: "Andrew Z" <wizard@xxxxxxxxxxxxxxx>
To: <amibroker@xxxxxxxxxxxxxxx>
Sent: Saturday, November 12, 2005 6:31 AM
Subject: Re: [amibroker] Re: Value of Equity ??
> Thanks Steve.
>
> Basically, what I am trying to do is get a value of my equity at a given
> point. Then, using this equity figure to determine my maximum position
> size allowable according to my MM rule of not risking more than 2% of my
> capital per trade.
>
> I've managed to get it to work (in a round about way), but only after
> placing my position sizing equations AFTER the buy/sells. Interestingly,
> it seems that unless I have the same 2 lines of code before the
> buy/sells then the equations do not function correctly. To illustrate, I
> have the following ( the same lines of code are marked with <<<<<< ):
>
> ********************************************************
> SetBarsRequired(500,5000);
> SetTradeDelays( 1, 1, 1, 1 );
> SetOption( "initialequity", 5000 );
> SetOption( "PriceBoundChecking", 1 );
> SetOption("FuturesMode", 1 );
> SetOption("MaxOpenPositions", 1);
>
> size=0;
>
> PointValue = 10000;
> MarginDeposit = 1;
>
> size = Equity() * (2/100); <<<<<<<<<<<<
> PositionSize = size / 40; <<<<<<<<<<<<
>
> spread=0.0002;
>
> stop_amount = Optimize("Default stop",0.0040,0.0010,0.0100,0.0005);
> ApplyStop(stopTypeLoss,stopModePoint,stop_amount, exitatstop=1, volatile
> = False, ReEntryDelay = 0 ) ;
>
> profit_amount = Optimize("Default profit",0.0210,0.0010,0.0500,0.0005);
> ApplyStop(stopTypeProfit,stopModePoint,profit_amount, exitatstop=1,
> volatile = False, ReEntryDelay = 0 );
>
> Buy = Buyconditions....
> Short = Shortconditions...
> Sell = Sellconditions...
> Cover = Coverconditions...
>
> BuyPrice = O + spread;
> ShortPrice = O - spread;
>
> size = Equity() * (2/100); <<<<<<<<<<<<<<<<<
> PositionSize = size / 40; <<<<<<<<<<<<<<<<<
> *********************************************************
>
> The code appears to work ok, given me varying "size" and PositionSize
> variables according to the Equity at the given bars, but I do not
> understand why this code needs to appear twice.
>
> Any ideas?
>
> Andrew.
>
>> Not sure what you are trying to do with it, but I think the main reason
>> the
>> Equity() function was introduced is to return an equity line as an aid to
>> system development (allows you to visually see drawdowns, etc, when you
>> are
>> backtesting your systems and compare equity performance with price,
>> indicators, etc ). TJ describes it as a "backtester in a box". You need
>> to
>> define your buy/sell rules before calling it because it uses these rules
>> internally to process your trades and return the corresponding equity
>> line.
>
>
>
>
>
> 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
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
------------------------ Yahoo! Groups Sponsor --------------------~-->
Try Online Currency Trading with GFT. Free 50K Demo. Trade
24 Hours. Commission-Free.
http://us.click.yahoo.com/RvFikB/9M2KAA/U1CZAA/GHeqlB/TM
--------------------------------------------------------------------~->
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
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
amibroker-unsubscribe@xxxxxxxxxxxxxxx
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|