I have been implementing tests with the library and the need arose for me to have a function/method that closes the positions by "margin call" as in real brokers. So i added the following just previous to the native "next" method.
def check_margin_call(self,leverage,margin,entryprice,next_price,market):
perc_var_leveraged = ((next_price-entryprice)/entryprice) * leverage
if market=='bull' and \
(perc_var_leveraged < 0) and \
abs(perc_var_leveraged) >= margin:
return True
elif market=='bear' and \
(perc_var_leveraged > 0) and \
abs(perc_var_leveraged) >= margin:
return True
else:
return False
def next(self):
...
if self.position:
if self.check_margin_call(leverage, margin, entryprice, next_price=close):
self.log('POSITION CLOSED BY MARGIN CALL !!')
self.close()
I leave this code snippet here in case it helps someone.