# Pair trading with Amibroker



## Gringotts Bank (4 April 2016)

Using this, found here:  http://www.marketcalls.in/amibroker/pair-trading-amibroker-afl-code.html

_SECTION_BEGIN("Pair Trading");
SetChartOptions(0,0,chartGrid30|chartGrid70);

_N( Symbol1= ParamStr("Symbol1", "ANZ") );


SetForeign( Symbol1 );
C1 = C;
H1 = H;
L1 = L;
O1 = O;
V1 = V;
RestorePriceArrays();

_N( Symbol2= ParamStr("Symbol2", "CBA") );


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() == "ANZ" )
{
  Buy = EntrySignal;
  Sell = ExitSignal;
  Short= Sell;
  Cover = Buy;

   SetPositionSize( 10000, spsShares );
}

if( Name() == "CBA" )
{
  Short = EntrySignal;
  Cover = ExitSignal;
  Buy = Cover;
  Sell = Short;

   SetPositionSize( 10000, spsShares );
}

_SECTION_END();



EQ curve, LOL.  Need some tips please.


----------



## CanOz (4 April 2016)

Buy low, sell high....don't eat yellow snow


Reserve the signals, should be stellar


----------



## edman79 (6 April 2016)

Gringotts Bank said:


> EQ curve, LOL.  Need some tips please.




Tips about the code or the method?
You might want to try bollinger bands for the entry signals (and anything else you think might work).
I seem to recall the banks dont pair backtest too well. They stay closely correlated and then diverge more than expected.
The offset dividend dates also plays havoc with any pair system. I do think there might be something in pairing the pre-dividend bank long against one that is not expecting a dividend short... but you would need to backtest that scenario.


----------



## Gringotts Bank (6 April 2016)

edman79 said:


> Tips about the code or the method?
> You might want to try bollinger bands for the entry signals (and anything else you think might work).
> I seem to recall the banks dont pair backtest too well. They stay closely correlated and then diverge more than expected.
> The offset dividend dates also plays havoc with any pair system. I do think there might be something in pairing the pre-dividend bank long against one that is not expecting a dividend short... but you would need to backtest that scenario.




Thanks for the tips ed.  I've decided to give it away for the moment.  I've been reading about pair trading on another thread on ASF and a lot of it is foreign.  Plenty of other systems to continue work on.


----------

