How do I write the code of this objective function?

The way OSR works in Dynare, it does not allow for user-defined welfare functions. The function needs to be representable in the form of second moments only. If your variables are not at the target on average, you will have linear terms in your objective. In that case, you need to run an optimizer over the parameter given the welfare objective. See e.g. Loop over parameters to find maximized welfare