Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Gonzalo Kobastre
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Groups 0

    Gonzalo Kobastre

    @Gonzalo Kobastre

    0
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Gonzalo Kobastre Unfollow Follow

    Latest posts made by Gonzalo Kobastre

    • position closed by margin call

      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.

      posted in General Discussion
      Gonzalo Kobastre
      Gonzalo Kobastre