Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    position closed by margin call

    General Discussion
    1
    1
    37
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Gonzalo Kobastre
      Gonzalo Kobastre last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors