Basket Trading Using A Directed Acyclic Graph

The AIQ code based on Dave Cline’s article in this issue, “Basket Trading Using A Directed Acyclic Graph,” is provided at, and is also shown here. Acyclic graphs is a means of using a type of network graph called a directed acyclic graph or DAG in choosing a basket of securities for trading a continuously rebalanced portfolio.


!Author: Dave Cline, TASC April 2015

!Coded by: Richard Denning 2/10/2015


!Len is the number of lookback days.

!L1 is the first ticker in the list that you want to compare your list to.

!L2 is the second ticker in the list that you want to compare your list to etc

!You must create a list of 20 or less symbols of interest then type in the same 

!symbols in the same alpha order as your list. If there are less than 20 just use " "

!but do not delete the input as there must be L1 through L20 inputs defined:


Len is 250.

L1 is "ADBE".

L2 is "ADI".

L3 is "ADP".

L4 is "AKAM".

L5 is "ALTR".

L6 is "AMAT".

L7 is "FOX".

L8 is "INTU".

L9 is "LLTC".

L10 is "LMCA".

L11 is "LRCX".

L12 is "LVNTA".

L13 is "NTAP".

L14 is "NVDA".

L15 is "NXPI".

L16 is "PAYX".

L17 is "SIRI".

L18 is "TRIP".

L19 is "TXN".

L20 is "VIAB".


ChgOC is (([close]-[open])/[open]) * 100.

SumY is Sum(ChgOC, Len).

SumYsq is Sum(ChgOC*ChgOC, Len).

SSy is SumYsq - ( (SumY * SumY) / Len ).

ChgOCL1 is TickerUDF(L1, ChgOC).

SumXsq1 is Sum(ChgOCL1*ChgOCL1, Len).

SumX1 is Sum(ChgOCL1, Len).

SumXY1 is Sum(ChgOC*ChgOCL1, Len).

SP1 is SumXY1 - ( (SumX1 * SumY) / Len ).

SSx1 is SumXsq1 - ( (SumX1 * SumX1) / Len ).

PearR1 is SP1/SQRT(SSx1*SSy).

PearRsq1 is ( PearR1 * PearR1 ).

ChgOCL2 is TickerUDF(L2, ChgOC).

SumXsq2 is Sum(ChgOCL2*ChgOCL2, Len).

SumX2 is Sum(ChgOCL2, Len).

SumXY2 is Sum(ChgOC*ChgOCL2, Len).

SP2 is SumXY2 - ( (SumX2 * SumY) / Len ).

SSx2 is SumXsq2 - ( (SumX2 * SumX2) / Len ).

PearR2 is SP2/SQRT(SSx2*SSy).

PearRsq2 is ( PearR2 * PearR2 ).

ChgOCL3 is TickerUDF(L3, ChgOC).

SumXsq3 is Sum(ChgOCL3*ChgOCL3, Len).

SumX3 is Sum(ChgOCL3, Len).

SumXY3 is Sum(ChgOC*ChgOCL3, Len).

SP3 is SumXY3 - ( (SumX3 * SumY) / Len ).

SSx3 is SumXsq3 - ( (SumX3 * SumX3) / Len ).

PearR3 is SP3/SQRT(SSx3*SSy).

PearRsq3 is ( PearR3 * PearR3 ).

ChgOCL4 is TickerUDF(L4, ChgOC).

SumXsq4 is Sum(ChgOCL4*ChgOCL4, Len).

SumX4 is Sum(ChgOCL4, Len).

SumXY4 is Sum(ChgOC*ChgOCL4, Len).

SP4 is SumXY4 - ( (SumX4 * SumY) / Len ).

SSx4 is SumXsq4 - ( (SumX4 * SumX4) / Len ).

PearR4 is SP4/SQRT(SSx4*SSy).

PearRsq4 is ( PearR4 * PearR4 ).

ChgOCL5 is TickerUDF(L5, ChgOC).

SumXsq5 is Sum(ChgOCL5*ChgOCL5, Len).

SumX5 is Sum(ChgOCL5, Len).

SumXY5 is Sum(ChgOC*ChgOCL5, Len).

SP5 is SumXY5 - ( (SumX5 * SumY) / Len ).
—Richard Denning for AIQ Systems


Leave a Reply