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

[amibroker] Re: OT - cointegration - pair trade



PureBytes Links

Trading Reference Links

dxd,

At this stage I'm not going to discuss too many details, but there 
has been some correspondence on possibly creating a DLL/plug-in link 
to R, either directly, or via a third program. In general, advanced 
and efficient matrix functionality would not only help in your case, 
but (IMMO) is absolutely crucial in portfolio (risk) management.

My personal motivation, and it would be great if Tomasz receives more 
of these signals, is that AB still lacks some functionality in the 
areas of, for example:
- matrix calculations (e.g. I've been unable so far to code an 
inverted matrix in AFL. Also, you can do cross-sectional analyses, 
but they are fairly slow if > say 500 stocks are involved);
- advanced statistics/mathematics (e.g. regressions), available 
externally in programs like Matlab, Maple, MathCad, SPSS, or R. Each 
has its (dis)advantages, and none provide everything I need.

Although I am currently using another program for statistics, I 
recently reviewed SPSS which I used some time ago. They have 
massively improved their functionality by providing a Developer's 
Kit, with a plug-in to R:

http://www.spss.com/devcentral/index.cfm?pg=plugins

So, if you or anybody else is interested in such a link, please let 
TJ know.

PS
--- In amibroker@xxxxxxxxxxxxxxx, "john_dxd_smith" 
<john_dxd_smith@xxx> wrote:
>
> 
> I googled to death and managed to find an unprotected Excel VBA code
> for Augmented Dickey Fuller test 
> (
> http://www.quantcode.com/modules/docmanager/view_file.php?
curent_file=79&curent_dir=1
> ).
> To check the accuracy of the code, I needed to teach myself R (
> http://www.r-project.org/ ).
> So far I have not been able to match the results...
> 
> Even if I successfully verify the accuracy, it would be a Gargantuan
> task for me to convert to afl.
> 
> Probably R can produce cointegration matrices ( my final goal )
> directly to rank the possible trading pairs 
> but the learning curve seems very steep.
> 
> I must admit I don't have enough domain knowledge nor coding 
ability but..
> 
> Is it TECHNICALLY possible to use R functions ( adf, Phillips-
Perron,
> KPSS, johansen, etc ) and use AmiBroker to read the result and loop
> through tickers to create matrices ?
> 
> regards,
> dxd
> 
> --- In amibroker@xxxxxxxxxxxxxxx, "john_dxd_smith"
> <john_dxd_smith@> wrote:
> >
> > 
> > Thank you for the pointers.
> > 
> > ADF was exactly what I was looking for as learning exercise.
> > I'm aware that most of the pair-trade alpha is quickly arbitarged 
away
> > and goes in a full circle in which bad performances drive away the
> > less sofisticacted and increase the opportunity which in turn 
attracts
> > more participants which again decrease the profit potential.
> > 
> > With your pointers I will start my long journey of learning the
> > anotomy and theory of internal combustion engine before driving a 
car.
> > 
> > best regards,
> > dxd
> > 
> > --- In amibroker@xxxxxxxxxxxxxxx, "vlanschot" <vlanschot@> wrote:
> > >
> > > It's been a while since I created that, but yes, this can quite 
> > > easily be achieved in AB. However, since there's nothing more 
> > > important than to learn along the way, and find you own 
solutions, 
> > > I'll give you only a few pointers:
> > > 
> > > 1) You need to create a few OLS-regression functions. One 
simple 
> > > example is:
> > > 
> > > function OLSXYb2(x,y,Period)
> > > // y = dependent var
> > > {
> > > 	global RSq;
> > > 	
> > > 	SumX=Sum(x,Period);
> > > 	SumY=Sum(y,Period);
> > > 	CombSum=Sum(x*y,Period);
> > > 	SumSqX=Sum(x^2,period);
> > > 	SumXSq=Sum(x,period)^2;
> > > 	
> > > 	
> > > 	b2=(period*CombSum-SumX*SumY)/(period*SumSqX-SumXSq);
> > > 	//b2=OLS-beta
> > > 	Rsq=Correlation(x,y,Period)^2;
> > > 	
> > > 	return b2;
> > > }
> > > 
> > > Unfortunately, this only applies to 1 independent variable, and 
> > > you'll need to find out yourself how to expand it to 2 
independent 
> > > variables, i.e. to regress y over x and z.
> > > 
> > > 2) You then need to calculate and further manipulate the OLS 
error 
> > > terms. One approach is to use the Augmented Dickey Fuller (ADF) 
stat. 
> > > 
> > > Again, a hint:
> > > 
> > > ADFResErr = OLSXY(P1, P2, LBPer);
> > > 			n = 0;
> > > 			ADFOLSErr[0]=0;
> > > 			while (n<LBPer)
> > > 			{
> > > 			ADFOLSErr = Ref(P2,-n)-SelectedValue(OLSb1) - 
> > > SelectedValue(OLSb2)*Ref(P1,-n);
> > > 			n = n+1;
> > > 			}
> > > 			etmin1 = Ref(ADFOLSErr,-1);
> > > 			etmin2 = Ref(ADFOLSErr,-2);
> > > 			ResChange = ADFOLSErr - etmin1;
> > > 			Lag1ResCh = Ref(ResChange,-1);
> > > 			Lag2ResCh = Ref(ResChange,-2);
> > > 			Cointgr = OLSXYZ(ResChange, 
> > > etmin1,Lag1ResCh,LBPer);
> > > 			tau = tvalueb3;
> > > . . . .
> > > 
> > > FWIW, too many investors are now looking at pair-trading via 
coint, 
> > > i.e. it has largely been arbitraged away. Most succesful hedge 
funds 
> > > use way more advanced strategies.
> > > 
> > > PS
> > > 
> > > --- In amibroker@xxxxxxxxxxxxxxx, "john_dxd_smith" 
> > > <john_dxd_smith@> wrote:
> > > >
> > > > 
> > > > Hi,
> > > > 
> > > > I understand cointegration, not correlation, is used to rank 
stocks 
> > > to
> > > > find "pair trade" candidates.
> > > > I read a lot of papers I found googling but they are all too 
> > > technical
> > > > and way over my head.
> > > > 
> > > > I'm only interested in a very basic/primitive textbook-case 
approach
> > > > among many variants actually used by hedge funds.
> > > > 
> > > > How do I go about creating cointegration matrix ?
> > > > Is it doable with AmiBroker ?
> > > > If not, are there any statistical/math softwares for non-
quants to
> > > > create the matrix ?
> > > > 
> > > > Thank you in advance.
> > > > 
> > > > best regards,
> > > > dxd
> > > >
> > >
> >
>




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 NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

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/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> 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/