y and x are the price changes of two correlated stocks.

data$$spread <- y - hedge_ratio*x threshold <- sd(data$$spread, na.rm = TRUE)
# Generate sell and buy signals
buys <- ifelse(data_out$$spread > threshold, 1, 0) sells <- ifelse(data_out$$spread < -threshold, -1, 0)

I don't understand why do we buy the spread if spread>threshold... In my opinion, if spread>threshold means it is valuable, so we should sell it but in the example, the spread is bought.

Thanks!