Your code looks very complicated, and you have double variable names and generate price series in a bar function where they do not belong. HA uses the normal bar period so a bar function is not needed anyway. Use the run function for calculating the HA candles. Or use the normal HA functions and simply smooth them with EMA.

HAOpenSmooth = EMA(HAOpen(),10);