Hello,
I'm *slightly* frustrated here, as I am trying to implement correct Percent
Volatility Position Sizing into AB (let alone Percent Risk Position Sizing
which should be even harder to code if implimented correctly & in a
non-specific, general way).
The formula for the example of Volatility Position Sizing in the AB manual is
wrong as this is rather some undefined mixture of a) Percent Volatility
Position Sizing and b) a volatility stop (as Van himself has confirmed).
Volatility PS is completely separate from stops; in fact, one of its best uses
is in systems without any hard stops.
I'm trying to code correct implementations of some basic position sizing
algorithms like Percent Volatility, Percent Risk (= Fixed Fractional), etc with
a nice & easy "Parameter" GUI as I intend to put them into AB's code library
for the benefit of newer users like myself.
Some of these seem to be harder to implement on AB (at least for a novice) than
I initially thought, as there are no "out-of-the-box" variables/object
properties like Position.Risk.Open, Group.Risk.Open, System.Risk.Open (=
Portfolio Heat), Long.Risk.Open, Short.Risk.Open, etc.
As well, I think it's slightly strange & unneccesarily cumbersome that
sig.PosSize doesn't define the position size as *units* (= shares, contracts,
etc), as this is the basic definition of "position size", and gives the user
the greatest flexibility. If one wants percentages, dollar amounts or whatever,
one can calculate this from the units in some separate lines of code.
What adds to the confusion is the fact that the different examples in AB's
manuals switch between PositionSize and SetPositionSize in rather random
fashion (I guess the latter is the newer one).
Don't get me wrong, I really like AB as it has many advantages and appreciate
Tomasz' work, but that position sizing & risk managment thing can be
implemented more logically, more flexible, and thus easier for the user.
I've been using AB just for a couple of months now, so I am no AFL wizz - but
you shouldn't have to be one for some basic position sizing algorithms.
Ok - enough of my little rant - here's my code. I don't know if I did it
completely wrong or just slightly wrong.
As it seems like I cannot use ATRs in the custom backtester, I've used the
AddToComposite function (if anyone has a simpler & faster solution, I am all
for it).
But when I run a backtest, the backtester seems to check the composite array
for signals (?), which looks rather strange, takes rather long, and makes AB
crash after 2 runs.
Any help is appreciated (any improvements to AB in this regard as well), and -
as I said - I'll put it into the code library for the benefit of all when I'm
done.
Thanks in advance!
// Money Manager: Percent Volatility
_SECTION_BEGIN( "Money Manager: Percent Volatility" );
systemMMVolatilityPercent = Param( "Percent", 2, 0.1, 10, 0.1 );
systemMMVolatilityATR = Param( "ATR Period", 20, 1, 250, 1 );
_SECTION_END();
AddToComposite(ATR(systemMMVolatilityATR), "~atr_"+Name(), "1",
atcFlagDefaults|atcFlagEnableInBacktest);
SetCustomBacktestProc("");
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
for ( bar = 0; bar < BarCount; bar++ )
{
CurrentEquity = bo.Equity;
for ( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar
) )
{
if(sig.IsEntry())
{
MMatr = Foreign("~atr_"+sig.Symbol, "1");
units = CurrentEquity * (
systemMMVolatilityPercent / 100 ) / MMatr[bar];
sig.PosSize = sig.Price * units;
}
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}