Order Flow Trading Strategy (Amibroker Code)

The original article can be found here.

Trading rules in plain English:

  • If PEP opens 0.4% LOWER than XLP, we buy the open and sell at the close
  • If PEP opens 0.4% HIGHER than XLP, we short the open and cover at the close

Amibroker code:

Fremmed=Foreign("xlp","close",fixup=1);
Fremmed1=Foreign("xlp","open",fixup=1);
apdifffremmed=(fremmed1-Ref(fremmed,-1))/Ref(fremmed,-1) ;

gapdiff= (O-Ref(C,-1))/Ref(C,-1)   ;

Plot(gapdifffremmed,"xlp", colorRed,styleLine);
Plot(gapdiff,"pep", colorBlue,styleLine);

Buy= (gapdifffremmed -.0034 ) > gapdiff ;
buyprice= Open;
Sell= C>0;  //forces exit at the same trading day
sellPrice= Close;

Short= (gapdifffremmed +.004 ) < gapdiff;
ShortPrice= Open;
Cover= C>0 ; //forces exit at the same trading day
CoverPrice=Close;