Hello, Generally speaking you should be using low-level backtest for that. Guessing from your rather short descriptions, I think that you are trying to override normal spsPercentOfEquity position size processing and use your own.
The example codes for using low-level interface are included at the end of this article: http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/ ProcessTradeSignals not only processes signals but also does what UpdateStats() is doing therefore you should not mix both since exposure will be counted twice. Using low-level mode avoids that. Best regards, Tomasz Janeczko amibroker.com On 2010-07-16 18:56, rise_t575 wrote: > > One last question for clarification: > > 1) On one hand, I have to call bo.ProcessTradeSignals() for updating equity. > 2) On the other hand, I have to calculate position sizes *before* calling > bo.ProcessTradeSignals(). > 3) But for calculating the correct position sizes, my position sizing code > has to have access to the updated equity. > > So using UpdateStats() in this case is *mandatory*, correct? > > --- In [email protected], Tomasz Janeczko<gro...@...> wrote: >> Hello, >> >> You can call UpdateStats( bar, 2 ) but please call it once per bar. When >> TimeInsideBar = 2 >> two things in addition to updating equity happen: >> a) interest earnings are added (from free cash) >> b) position exposure and portfolio stats are calculated >> >> You can get incorrect exposure figures if you call UpdateStats( bar, 2 ) >> more than once per bar. >> >> One thing you should think of: be aware that if you are using current bar >> closing equity for >> position sizing, you are generating possible future data leak. >> How is that so? It is easy - assume that your position sizing depends on >> current equity, >> you can calculate bar's open equity and open position equal to the >> difference between >> open equity and close equity of the same bar. And you generate single bar >> trade. >> This way you have the system that has 100% winning trades, because it is >> using future leak in >> position sizing. >> That is the reason AmiBroker DOES NOT use close equity of current bar for >> position sizing. >> It either uses previous bar close equity or equity value at the open. >> >> Best regards, >> Tomasz Janeczko >> amibroker.com >> >> On 2010-07-16 15:29, rise_t575 wrote: >>> I see - thanks. >>> >>> Actually I would like to use equity calculated from closing prices of the >>> current bar (by searching the web, I've found that normally, AB calculates >>> equity for position sizing with opening prices of the current bar - and not >>> being aware of this has caused me some major headaches the last couple of >>> days. Is this mentioned in the position sizing related sections of the >>> manual? If not, this information should be added). Reading the description >>> of UpdateStats(), it seems like I could achieve this by setting the second >>> (TimInsideBar) parameter to "2". When I put "bo.UpdateStats( bar, 2 );" >>> into my code, at least the position sizes are calculated exactly the way I >>> want them to. >>> But there are several warnings about doing this in the method's >>> description. Can I unintentionally mess something up by doing this? >>> >>> Thanks. >>> >>> --- In [email protected], Tomasz Janeczko<groups@> wrote: >>>> Hello, >>>> >>>> Sorry, the method name is actually UpdateStats() >>>> >>>> Best regards, >>>> Tomasz Janeczko >>>> amibroker.com >>>> >>>> On 2010-07-16 14:06, rise_t575 wrote: >>>>> Probably I'm blind, but I cannot find any information about the mentioned >>>>> UpdateEquity() function, any searches in the online/offline manual result >>>>> nothing. Could someone provide me with a link? >>>>> >>>>> Thanks. >>>>> >>>>> >>>>> --- In [email protected], Tomasz Janeczko<groups@> wrote: >>>>>> Hello, >>>>>> >>>>>> The value of bo.Equity is correct. >>>>>> >>>>>> And your findings are incorrect. >>>>>> "Use previous bar equity" works as described in the manual. >>>>>> >>>>>> You are making mistake in your thinking/debugging. >>>>>> Your formula is checking bo.Equity BEFORE calling ProcessTradeSignals(). >>>>>> And it is giving you last known >>>>>> equity value. That is the reason of your incorrect findings. >>>>>> >>>>>> If you are using custom backtester interface, there are clearly defined >>>>>> points where *you* are in charge >>>>>> and when AB is in charge (and can update anything). It will NOT do any >>>>>> "magical steps" out of blue right after >>>>>> for( bar = 0; bar< BarCount; bar++ ) loop. If you want to update >>>>>> equity to the current bar you either >>>>>> must call ProcessTradeSignals() (it will update equity and process >>>>>> signals) OR... if you want to have >>>>>> current bar equity WITHOUT calling ProcessTradeSignals, you have to call >>>>>> UpdateEquity( bar, 0 ) function. >>>>>> >>>>>> The difference that "Use previous bar equity" makes in case of CBT is >>>>>> that ***INSIDE*** ProcessTradeSignals() >>>>>> it will use either previous bar or current bar equity, depending on >>>>>> setting, and it will affect the size >>>>>> of position open (if you are using spsPercentOfEquity or otherwise >>>>>> depend on available equity) >>>>>> >>>>>> Recommended reading: >>>>>> http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/ >>>>>> >>>>>> >>>>>> Best regards, >>>>>> Tomasz Janeczko >>>>>> amibroker.com >>>>>> >>>>>> On 2010-07-15 20:15, rise_t575 wrote: >>>>>>> Hi, >>>>>>> >>>>>>> I've just found out what is happening, but I have not the slightest >>>>>>> idea why. >>>>>>> >>>>>>> The backtester is *always* using previous bar equity for positions >>>>>>> sizing, although this setting is *not* ticked, and I haven't included >>>>>>> the corresponding SetOption function. >>>>>>> >>>>>>> In fact, the backtests and the debugging logs are exactly identical >>>>>>> when UsePrevBarEquityForPosSizing is True and when it is False. >>>>>>> >>>>>>> Is this some bug or isn't that setting being used when using CBT for >>>>>>> position sizing? >>>>>>> >>>>>>> Thanks in advance! >>>>>>> >>>>>>> --- In [email protected], "rise_t575"<rise_t@> wrote: >>>>>>>> Hello, >>>>>>>> >>>>>>>> The following are the (debug) outputs of a tested position sizing >>>>>>>> algorithm. >>>>>>>> >>>>>>>> I've attached a) part of the code , b) the output of the _TRACE() >>>>>>>> function from within the CBT code (the TRACE function had been placed >>>>>>>> within the most inner "if{}" code block), and c) the output of the AA >>>>>>>> Results window (Detailed Log). >>>>>>>> >>>>>>>> Note that on the _TRACE() output, the equity on day 2 (21.01.2000) is >>>>>>>> still at its intial value (100000), so it hasn't been adjusted for >>>>>>>> price changes of the position taken at the close of day 1 (which did >>>>>>>> exist). >>>>>>>> >>>>>>>> Since this equity value is used for calculation of the position size >>>>>>>> of subsequent signals, this is a problem. >>>>>>>> >>>>>>>> On the other hand, the output of the AA Results windows on day 2 is >>>>>>>> correct (99823.2) >>>>>>>> >>>>>>>> And - no - "Use previous bar equity for position sizing" is not >>>>>>>> activated. >>>>>>>> >>>>>>>> Does anyone have an idea why bo.equity delivers incorrect values in >>>>>>>> this case? >>>>>>>> >>>>>>>> Thanks in advance! >>>>>>>> >>>>>>>> Part of CBT code in question: >>>>>>>> >>>>>>>> for ( sig = bo.GetFirstSignal( bar ); sig; sig = >>>>>>>> bo.GetNextSignal( bar ) ) >>>>>>>> { >>>>>>>> if ( sig.IsEntry() ) >>>>>>>> { >>>>>>>> currEquity = bo.Equity; >>>>>>>> pointVal = sig.PointValue; >>>>>>>> cbtAtr = StaticVarGet( "statAtr" + >>>>>>>> sig.Symbol ); >>>>>>>> psUnits = int( currEquity * ( pctVolaRisk >>>>>>>> / 100 ) / ( cbtAtr[bar] * pointVal ) ); >>>>>>>> [_TRACE] >>>>>>>> sig.PosSize = ( 2000 + psUnits ) * -1; >>>>>>>> } >>>>>>>> } >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> DebugView Output: >>>>>>>> [2856] Date: 20.01.2000 Symbol: FDRY Equity: 100000 Cash: 100000 ATR: >>>>>>>> 14.383258 Units: 69 >>>>>>>> [2856] Date: 21.01.2000 Symbol: PCLN Equity: 100000 Cash: 89818.2 ATR: >>>>>>>> 27.671930 Units: 36 >>>>>>>> [2856] Date: 21.01.2000 Symbol: TYC Equity: 100000 Cash: 89818.2 ATR: >>>>>>>> 14.318740 Units: 69 >>>>>>>> [2856] Date: 25.01.2000 Symbol: AKAM Equity: 98382.8 Cash: 59163.1 >>>>>>>> ATR: 27.247997 Units: 36 >>>>>>>> [2856] Date: 03.02.2000 Symbol: IBM Equity: 97308.7 Cash: 49107.9 ATR: >>>>>>>> 4.628230 Units: 210 >>>>>>>> [2856] Date: 04.02.2000 Symbol: FDRY Equity: 97846.4 Cash: 42716.2 >>>>>>>> ATR: 13.667122 Units: 71 >>>>>>>> >>>>>>>> >>>>>>>> AA Results Output: >>>>>>>> 20.01.2000 >>>>>>>> Entry signals(score):FDRY=Buy(1), >>>>>>>> Exit signals: >>>>>>>> Enter Long, FDRY, Price: 147.563, Shares: 69, Commission: 0, >>>>>>>> Rank: 1, Equity 100000, Margin Loan: 0, Fx rate: 1 >>>>>>>> 1 Open Positions: , FDRY (+69), Equity: 100000, Cash: 89818.2 >>>>>>>> >>>>>>>> 21.01.2000 >>>>>>>> Entry signals(score):PCLN=Buy(1), TYC=Buy(1), >>>>>>>> Exit signals: >>>>>>>> Enter Long, PCLN, Price: 381, Shares: 36, Commission: 0, Rank: >>>>>>>> 1, Equity 100237, Margin Loan: 0, Fx rate: 1 >>>>>>>> Enter Long, TYC, Price: 245.493, Shares: 69, Commission: 0, >>>>>>>> Rank: 1, Equity 100237, Margin Loan: 0, Fx rate: 1 >>>>>>>> 3 Open Positions: , FDRY (+69), , PCLN (+36), , TYC (+69), >>>>>>>> Equity: 99823.2, Cash: 59163.1 >>>>>>>> >>>>>>>> 24.01.2000 >>>>>>>> Entry signals(score): >>>>>>>> Exit signals: >>>>>>>> 3 Open Positions: , FDRY (+69), , PCLN (+36), , TYC (+69), >>>>>>>> Equity: 98382.8, Cash: 59163.1 >>>>>>>> >>>>>>>> 25.01.2000 >>>>>>>> Entry signals(score):AKAM=Buy(1), >>>>>>>> Exit signals: >>>>>>>> Enter Long, AKAM, Price: 279.313, Shares: 36, Commission: 0, >>>>>>>> Rank: 1, Equity 98825.1, Margin Loan: 0, Fx rate: 1 >>>>>>>> 4 Open Positions: , FDRY (+69), , PCLN (+36), , TYC (+69), , >>>>>>>> AKAM (+36), Equity: 98806.8, Cash: 49107.9 >>>>>>>> >>>>>>> ------------------------------------ >>>>>>> >>>>>>> **** IMPORTANT PLEASE READ **** >>>>>>> This group is for the discussion between users only. >>>>>>> This is *NOT* technical support channel. >>>>>>> >>>>>>> TO GET TECHNICAL SUPPORT send an e-mail directly to >>>>>>> SUPPORT {at} amibroker.com >>>>>>> >>>>>>> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at >>>>>>> http://www.amibroker.com/feedback/ >>>>>>> (submissions sent via other channels won't be considered) >>>>>>> >>>>>>> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: >>>>>>> http://www.amibroker.com/devlog/ >>>>>>> >>>>>>> Yahoo! Groups Links >>>>>>> >>>>>>> >>>>>>> >>>>>>> >>>>> >>>>> ------------------------------------ >>>>> >>>>> **** IMPORTANT PLEASE READ **** >>>>> This group is for the discussion between users only. >>>>> This is *NOT* technical support channel. >>>>> >>>>> TO GET TECHNICAL SUPPORT send an e-mail directly to >>>>> SUPPORT {at} amibroker.com >>>>> >>>>> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at >>>>> http://www.amibroker.com/feedback/ >>>>> (submissions sent via other channels won't be considered) >>>>> >>>>> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: >>>>> http://www.amibroker.com/devlog/ >>>>> >>>>> Yahoo! Groups Links >>>>> >>>>> >>>>> >>>>> >>> >>> >>> ------------------------------------ >>> >>> **** IMPORTANT PLEASE READ **** >>> This group is for the discussion between users only. >>> This is *NOT* technical support channel. >>> >>> TO GET TECHNICAL SUPPORT send an e-mail directly to >>> SUPPORT {at} amibroker.com >>> >>> TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at >>> http://www.amibroker.com/feedback/ >>> (submissions sent via other channels won't be considered) >>> >>> For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: >>> http://www.amibroker.com/devlog/ >>> >>> Yahoo! Groups Links >>> >>> >>> >>> > > > > ------------------------------------ > > **** IMPORTANT PLEASE READ **** > This group is for the discussion between users only. > This is *NOT* technical support channel. > > TO GET TECHNICAL SUPPORT send an e-mail directly to > SUPPORT {at} amibroker.com > > TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at > http://www.amibroker.com/feedback/ > (submissions sent via other channels won't be considered) > > For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG: > http://www.amibroker.com/devlog/ > > Yahoo! Groups Links > > > >
