I am still working on the paper “Optimal monetary and fiscal policy in a currency union” by Gali and Monacelli (2008).

The model is running but I get non-zero residuals for my market clearing equations. The residuals are not even close to zero but there rather seems to be an error in my coded equations.

Can anybody explain me why these residuals are non-zero and how to solve this problem?

My guess is because you are mixing linearized and level equations. For example, your equation (4) is equation (25), but it should most probably be (26)