The formulas on the documentation page you provided explain why you are not getting the same values when there is a lag. Note that in the cross covariance formula it is always the mean of y that is being used regardless of the lag value. In your example code the variables gtresiduals (y) and gt3 (the lagged values you created) will (very likely) have different means. As @PaigeMiller said the two procedures are computing different quantities.
... View more