Amibroker AFL code for Pair Trading:
You can use this AFL for Implementing Option Strategies in Amibroker.
Script:
_SECTION_BEGIN(“Pair Trading”);
SetChartOptions(0,0,chartGrid30|chartGrid70);
_N( Symbol1= ParamStr(“Symbol1”, “$NIFTY”) );
SetForeign( Symbol1 );
C1 = C;
H1 = H;
L1 = L;
O1 = O;
V1 = V;
RestorePriceArrays();
_N( Symbol2= ParamStr(“Symbol2”, “$BANKNIFTY”) );
SetForeign( Symbol2 );
C2 = C;
H2 = H;
L2 = L;
O2 = O;
V2 = V;
RestorePriceArrays();
Color = ParamColor( “Color”, colorDefault );
Style = ParamStyle( “Style”, styleLine, maskPrice );
PlotOHLC( O2/O1, H2/H1, L2/L1, C2/C1, “(” + Symbol2+” / “+Symbol1 + “) spread”, Color, style );
AddToComposite(C2/C1, “~Pair”, “X” );
SetForeign( “~Pair” );
C3 = C;
H3 = H;
L3 = L;
O3 = O;
V3 = V;
RestorePriceArrays();
EntrySignal = Cross(EMA(C3,2),C3);
ExitSignal = Cross(C3,EMA(C3,2));
if( Name() == “$NIFTY” )
{
Buy = EntrySignal;
Sell = ExitSignal;
Short= Sell;
Cover = Buy;
SetPositionSize( 100, spsShares );
}
if( Name() == “$BANKNIFTY” )
{
Short = EntrySignal;
Cover = ExitSignal;
Buy = Cover;
Sell = Short;
SetPositionSize( 50, spsShares );
}
_SECTION_END();