Here is the code for a 2 to 1 ratio spread. I always use the dollar value of a contract as opposed to closing price when calculating a spread.
inputs: AvgLength(20), NumDev(2);
vars: spread(0), avg(0), stdev(0),
DataSeries1( Close of data1 ), DataSeries2( Close of data2 ) ...