The costs and taxes of each transaction go into its value; loan interest is excluded by art. 35.1.b).
An indicative calculation under the state IRPF rules. It does not replace professional advice and does not take your full situation into account.
Spanish capital gains tax by Calculadora Capital