I think your issue is related to the line
Margin = 1 * (Capital + ProfitClosed);

This line actually has the result of putting up your entire account balance as margin! That is, you are getting margin called whenever price moves against you.

Try setting that line to

Margin = 0.1 * (Capital + ProfitClosed) for putting up 10% of your account as margin.