Bruce's code is correct and enters desired # shares.
Below is even more simplified example:
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject( );
bo.PreProcess( );
for ( bar = 0; bar < BarCount; bar++ )
{
for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
{
shares = 5;
sig.PosSize = -2000 - shares;
sig.RoundLotSize = shares;
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess( );
}
Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
From: Markus Witzler
To: [email protected]
Sent: Friday, July 17, 2009 8:33 PM
Subject: Re: [amibroker] Re: Using CBI the first time
Hello Bruce,
the code you provided does not what I intend to do - unfortunately!
"sig.possize" only can hold dollar value (positive numbers) and percent of
equity (negative numbers)
Percent of equity (or cash for that matter) can also mean differing values
for shares/contracts being bought due to rounding if fractional share sizes
occur!?
In the example I provide further below, I backtest an S&P contract.
The system carries positions in $1 / handle lots and rounds to the nearest
250 lots, representing one S&P 500 contract.
Incidentally, the "risk per lot" used below is actually an exponential lage
of the Average true range multilied by a constant (say 5).
Lot_size_not_rounded=(heat*bo.cash)/(Risk_per_lot)
Lot_size_rounded=Round(Lot_size_not_rounded/250)
Unit_size=Lot_size_rounded*250
With numbers:
Lot_size_not_rounded=(0.1*1000,000 USD)/3.115*5)=6420.545746
Lot_size_rounded=Round(6420.545746/250)=Round(25.682....)=26
Unit_size=26*250=6500 units to be bought.
I´m afraid using percentages of cash or position value [expressed in USD]
could result (due to rounding) in slightly different shares to be bought/sold
thus influencing the whole process. I ned to reproduce the exact share/contract
numbers!
I figure going like this is the only chance I have:
sharesize=round(((Heat*bo.cash)/(Expon._lag_of_ATR*ATRmultiplier))/250)*250;
// i.e. # of shares/lots like above
sig.possize=sharesize*sig.price; // i.e. position value - I must use THAT
since it´s needed in EnterTrade that I must use in low-level format. And I need
low-level format
since I intend to include proprietary stops (not ASpplyStop) in a later stage
of development!!
bo.EnterTrade(i, sig.Symbol, True, sig.Price, sig.PosSize);
I know "sig.possize means" Dollar value here. "sharesize" means unit size or
# of shares. I just hope that AB doens´t round the value of sharesize somewhere
in the backtesting process thereby lossing the exact number of shares/units I
intend to buy!!!!!!
But this may be an issue to discuss with TJ if no one has an idea how to do
this??!!
In case I didn´t explain myself properly, please let me know and I´ll clarify.
Thanks!
Markus
----- Original Message -----
From: bruce1r
To: [email protected]
Sent: Thursday, July 16, 2009 3:46 PM
Subject: [amibroker] Re: Using CBI the first time
Markus -
OK, here's an example of what I think that you were after. It is a
non-practical demo, but should show some points about encoding of signal types.
In your case, it shows calculating number of shares directly from % of cash
and putting it in the signal object for the backtester to process. See the
comments for details and the AA results for the effect. BTW, I hope you'll
find some other nuggets in this example also, and find it useful as a framework
for exploring some aspects of the CBT. Some aspects of the CBT just need to be
"played" with to see what is possible. This will teach me to get involved. :-)
-- BruceR
// Simple example to do the following -
// 1. Buy on the first trading day of the month and sell 5 days later
// 2. Setup default to buy $5000
// 3. Then, in the CBT, modify the trades on even months to buy s
// shares equal to 20% of cash.
//
// NOTE - the encodings for PosSize as detailed in the SetPositionSize()
help are -
// values below -2000 encode share count,
// values between -2000 AND -1000 encode % of current position
(scaling)
// values between -1000 AND 0 encode % of portfolio Equity
// values above 0 encode dollar value
Buy = IIf( Month( ) != Ref( Month( ), -1 ), 1, 0 );
Sell = Ref( Buy, -5 );
Short = Cover = 0;
SetPositionSize( 5000, spsValue );
SetOption( "InitialEquity", 100000 );
RoundLotSize = 1;
// MID-LEVEL CBT MODEL
SetOption( "UseCustomBacktestProc", True );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject( );
bo.PreProcess( );
mon = Month( );
dt = DateTime( );
for ( bar = 0; bar < BarCount; bar++ )
{
for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar
) )
{
// On even months, arbitrarily buy shares = 20% of cash
// On odd months, $5000 will be used from SetPositionSize above
if ( sig.IsEntry )
{
if ( Mon[ bar ] % 2 == 0 AND sig.IsEntry )
{
// Note - int() function effectively rounds down
shares = int( bo.cash * 0.20 / sig.Price );
sig.PosSize = -2000 - shares;
}
_TRACE( NumToStr( dt[ bar ], formatDateTime ) +
" , Cash = " + bo.Cash +
" , Price = " + sig.Price +
" , PositionSize = " + sig.possize );
}
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess( );
}
--- In [email protected], "bruce1r" <bru...@...> wrote:
>
> Markus -
>
> I don't think that you, and maybe Mike, are seeing what I'm trying to
point out. I referred you to SetPositionSize() help because it details the
encoding of shares in PosSize in the signal object in the CBT. Specically that
is (-2000-shares).
>
> When you traverse the signal objects in the CBT, you can do your own
calculation of shares from cash or whatever, and replace the sig.PosSize value
before calling the mid or low level routines to process trades.
>
> Stated simply, you can set the number of shares yourself directly in the
CBT from your own calculation.
>
> If that is what you're after, give me a short time and I'll whip up an
example if you don't see this.
>
> -- Bruce
>
>
> --- In [email protected], Markus Witzler funnybiz@ wrote:
> >
> > Hello Bruce,
> >
> > the reason I must use CBT is that I can´t use Setpositionsize
(...spsshares), since I need to compute adequate number of shares by referring
to actual cash position.
> >
> > "spsShares" doesn´t allow - according to cust. support- for calling
current cash or equity position to compute "spsshares". Thus the need for using
CBT.
> >
> > Normally, everyone seems to compute sig.possize (and implied number of
shares is a result thereof). I want to compute shares and the position size (in
money terms) should be the implied result - thus the other way around.
> >
> > The reason for this is that i want to follow along a coding excercise I
was given. And I´m not sure that -if I only specify sig.possize and not
sharesize (i.e. trade.shares)- that AB may screw up (i.e. round up or down in
some cases) when I don´t realize that.
> >
> > Take for instance into account that AB ALWAYS rounds DOWN if fraction
of shares occur.
> >
> > I, myself, want to have control if rounding occurs or not and HOw it
occurs (up or down). That could sometimes mean I´m rounding up when AB would
round down.
> >
> > This is not because I´m particularly picky since this issue influences
the outcome of the backtest only slightly. It´s only that I want to accurately
reproduce the results of the excercise I was given.
> >
> > In actual trading, there shouldn´t be any problem determining
sig.possize in signal list and leave it up to AB to (maybe) rounding up or down
potential fractional shares.
> >
> > If you have any clue that might help, please fell free to step in.
> >
> > Thanks for your contribution
> >
> > Markus
> > ________________________________________________________________
> > Neu: WEB.DE Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
> > für nur 19,99 Euro/mtl.!* http://produkte.web.de/go/02/
> >
>