Skip to content

Instantly share code, notes, and snippets.

@marketcalls
Last active April 11, 2022 11:38
Show Gist options
  • Save marketcalls/85cc21948a8a3537fda3a7e7265f120f to your computer and use it in GitHub Desktop.
Save marketcalls/85cc21948a8a3537fda3a7e7265f120f to your computer and use it in GitHub Desktop.
Cointegration AFL
//Coded by Rajandran R
//Date : 27th Aug 2020
//Requirements
//Used Amipy v0.2.0 (64-bit) - Download from https://forum.amibroker.com/t/amipy-plug-in-python-integration/20337/32
//Amibroker (64 Bit) v6.3 or higher
//Python 3.8 or higher
//ADF Test Return Values
//adffloat - The test statistic.
//pvaluefloat - MacKinnon”s approximate p-value based on MacKinnon (1994, 2010).
//usedlagint - The number of lags used.
//nobsint - The number of observations used for the ADF regression and calculation of the critical values.
//critical valuesdict - Critical values for the test statistic at the 1 %, 5 %, and 10 % levels. Based on MacKinnon (2010).
//icbestfloat - The maximized information criterion if autolag is not None.
_SECTION_BEGIN("Cointegration");
Version(6.3);
SetChartOptions(0,chartShowArrows|chartShowDates);
SetBarsRequired(-2,-2);
//call the python file for execution
PyLoadFromFile("C:\\Python\\Code\\cointegration.py");
_N( Symbol1= ParamStr("Symbol1", "HDFC.NS") );
_N( Symbol2= ParamStr("Symbol2", "HDBK.NS") );
Color1 = ParamColor("Color1",colorGreen);
Color2 = ParamColor("Color2",coloryellow);
//Get the Foreign Price values
SetForeign(symbol1);
symC1 = Close;
_N(sym1 = FullName());
RestorePriceArrays( True );
SetForeign(symbol2);
symC2 = Close;
_N(sym2 = FullName());
RestorePriceArrays( True );
period = Param("Cointegration Lookback",40,1,300,1); //lookback for cointegration
//pass the arrays and numbers to the python functions
adfteststatic = PyEvalFunction("adf",symC1,symC2,period,0);
pvalue = PyEvalFunction("adf",symC1,symC2,period,1);
usedlag = PyEvalFunction("adf",symC1,symC2,period,2);
nobs = PyEvalFunction("adf",symC1,symC2,period,3);
criticalvalues1 = PyEvalFunction("criticalvalues",symC1,symC2,period,4,"1%");
criticalvalues5= PyEvalFunction("criticalvalues",symC1,symC2,period,4,"5%");
criticalvalues10 = PyEvalFunction("criticalvalues",symC1,symC2,period,4,"10%");
icbest = PyEvalFunction("adf",symC1,symC2,period,5);
//pvalue = PyEvalFunction("pvalue",symC1,symC2,period);
//Plot Correlation and CoIntegration in a Dashboard
GfxSetBkMode( 0 );
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetTextColor( colorWhite );
GfxSelectPen( colorGreen, 2 );
GfxSelectSolidBrush( colorgreen );
GfxRectangle( 10, 20, 250, 250 );
GfxTextOut( "Cointegration Stats",23,23);
GfxTextOut( "ADF Test Statistic: " + NumToStr(adfteststatic,1.4,separator=false),23,48);
GfxTextOut( "P- Value : " + NumToStr(pvalue,1.4,separator=false),23,73);
GfxTextOut( "Lags: " + NumToStr(usedlag,1.0,separator=false),23,98);
GfxTextOut( "Observations : " + NumToStr(nobs,1.0,separator=false),23,123);
GfxTextOut( "Critical Values 1% : " + NumToStr(criticalvalues1,1.4,separator=false),23,148);
GfxTextOut( "Critical Values 5% : " + NumToStr(criticalvalues5,1.4,separator=false),23,173);
GfxTextOut( "Critical Values 10% : " + NumToStr(criticalvalues10,1.4,separator=false),23,198);
GfxTextOut( "icbest : " + NumToStr(icbest,1.4,separator=false),23,223);
Plot(symC1,sym1,Color1,styleLine|styleownscale);
Plot(symC2,sym2,Color2,styleLine|styleownscale);
_SECTION_END();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment