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/

    Problem on cash/portfolio with leverage for FOREX

    General Discussion
    1
    2
    284
    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.
    • L
      lvieira last edited by

      Hello guys,

      Im backtesting on FOREX EURUSD with daily data, with leverage=100. When I go long, the amount of cash is decreased buy price*quantity/100(my leverage), but when I go short the cash = price*quantity (without dividing by the leverage). All the orders are Market orders. This is making the cash plotting inconsistent, since it grows 100 times more when shorting than when going long.
      Im not sure if its a misconfiguration or it's a bug. Could anyone help me?

      26-EURUSDDaily.png

      The code used to print on the strategy:

      def notify_order(self, order):
              if(order.status in [order.Completed]):
                  self.log('order', order.getstatusname(),'positionSize', self.broker.getposition(data).size, 'cash', self.broker.get_cash(), 'portfolio', self.broker.get_value())
                  return
      
              if(order.status in [order.Canceled, order.Margin, order.Rejected]):
                  self.log(order)
                  self.log(self.broker.getposition(data))
                  return
              
              self.log(order.ordtypename(), order.size, order.getstatusname())
      

      Sizer code that also logs:

      class MySizer(bt.Sizer):
          def log(self, *args):
              self.strategy.log(args)
      
          def _getsizing(self, comminfo, cash, data, isbuy):
              sizeNecessaryToRevert = abs(self.broker.getposition(data).size)
              tradingSize = self.getSizeOnStandardDeviation(self.strategy.StandardDeviation[0])
      
              if(isbuy):
                  self.log('getting size to buy', tradingSize + sizeNecessaryToRevert)
              else:
                  self.log('getting size to sell', (tradingSize + sizeNecessaryToRevert))
                  
              return tradingSize + sizeNecessaryToRevert
              
          def getSizeOnStandardDeviation(self, stddev):
              valueToTrade = RISK_TO_TRADE * self.broker.get_value()
              return math.floor(valueToTrade/stddev)
      

      My commissions configuration (no commission for now, just leverage)

      cerebro.broker.setcash(10000.0)
      cerebro.broker.setcommission(commission=0, margin=False, mult=1, leverage=100) 
      

      log

      2016-06-30, ('getting size to buy', 9226)
      2016-07-01, Buy, 9226, Submitted
      2016-07-01, Buy, 9226, Accepted
      2016-07-01, order, Completed, positionSize, 9226, cash, 9897.5784836, portfolio, 10032.014220000001
      2016-07-04, ('getting size to sell', 18788)
      2016-07-05, Sell, -18788, Submitted
      2016-07-05, Sell, -18788, Accepted
      2016-07-05, order, Completed, positionSize, -9562, cash, 20711.729000000003, portfolio, 10121.814000000004
      2016-07-08, ('getting size to buy', 19477)
      2016-07-11, Buy, 19477, Submitted
      2016-07-11, Buy, 19477, Accepted
      2016-07-11, order, Completed, positionSize, 9915, cash, 10041.182048000004, portfolio, 10162.589240000005
      2016-07-12, ('getting size to sell', 19742)
      2016-07-13, Sell, -19742, Submitted
      2016-07-13, Sell, -19742, Accepted
      2016-07-13, order, Completed, positionSize, -9827, cash, 21033.436940000007, portfolio, 10136.964530000007
      2016-07-15, ('getting size to buy', 20234)
      2016-07-18, Buy, 20234, Submitted
      2016-07-18, Buy, 20234, Accepted
      2016-07-18, order, Completed, positionSize, 10407, cash, 10059.902522100007, portfolio, 10201.018320000006
      2016-07-18, ('getting size to sell', 21876)
      2016-07-19, Sell, -21876, Submitted
      2016-07-19, Sell, -21876, Accepted
      

      As u can see, there is a first buy of 9226 units and when completed the cash was 9897.57 and the portfolio value 10032.01. The next order was a sell of 18788 units that left my cash at 20711.72 and portfolio value at 10121.81

      Thanks in advance,
      Lucas Vieira.

      1 Reply Last reply Reply Quote 0
      • L
        lvieira last edited by

        PS: Sorry, I just realized after posting it that the proper Forum Section would be General Code/Help, I cant seen to find where to change the section now

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