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/

    Portfolio Value Vecomes NAN

    General Code/Help
    2
    3
    110
    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.
    • M
      mcandar last edited by

      I am trying to implement a strategy that longs selected n stocks from a universe. In a time interval, I have more than 200 stocks with the format below. However, in this time interval, some of the stocks are listed after the beginning date and also some are unlisted before the end date so there are nan's. (Delisted column shows if that stock is going to be delisted after 20 days.)

                      open      high       low     close  delisted
      2005-01-03  0.211401  0.258859  0.200831  0.215716       0.0
      2005-01-04  0.211401  0.258859  0.200831  0.215716       0.0
      2005-01-05  0.227417  0.278469  0.216046  0.232058       0.0
      2005-01-06  0.224214  0.274547  0.213003  0.228789       0.0
      2005-01-07  0.233823  0.286313  0.222132  0.238595       0.0
      2005-01-10  0.240229  0.294158  0.228217  0.245131       0.0
      .........
      

      I am trying to calculate a metric only for not nan stocks currently and the ones that won't be nan during incoming 20 days.

          def my_metric(self):
              t = self.datas[0].datetime.date(0)
              cons = list(self.p.cons.loc[t].values)
              asset_datas = [d for d in self.datas if d._name in cons]
      
              mom = []
              for d in asset_datas:
                  dlst = d.delisted.get(size=22)
                  dlst = np.asarray(dlst)
                  if np.any(dlst > 0): # if it is going to be unlisted in 20 days
                      self.log(f'Skipping delisted stock {d._name}')
                      continue
      
                  rt = d.close[0]
                  rt_1 = d.close[-self.p.t1*self.p.per]
                  rt_12 = d.close[-self.p.t12*self.p.per]
      
                  short_term = (rt - rt_1)/rt_1
                  long_term = (rt - rt_12)/rt_12
      
                  result = long_term - short_term
                  if np.isnan(result):
                      continue
      
                  mom.append((result,d))
              return sorted(mom,reverse=True) # sort descending
      

      I monitor important values inside next()

          def next(self):
              if len(self) < self.p.t12*self.p.per:
                  return
              
              month = self.datas[0].datetime.date(0).month
      
              if month != self.last_month:
                  self.rebalance()
                  self.last_month = month
      
              self.posdata = [d for d, pos in self.getpositions().items() if pos]
              
              totalwealth = self.broker.getvalue()
              cash = self.broker.getcash()
              invested = totalwealth - cash
      
              self.log("Total Wealth: %.2f, Invested: %.2f, Cash-On-Hand: %.2f" %(totalwealth,
                                                                          invested,
                                                                          cash))
              if np.isnan(totalwealth):
                  exit()
      
              self.log(f"IN HAND ---> {[(d._name,round(d.open[0],2),round(d.high[0],2),round(d.low[0],2),round(d.close[0],2),int(d.delisted[0])) for d in self.posdata]}")
      

      It goes well up to a point at which the portfolio value turns out to be nan.

      2020-05-20, Total Wealth: 1620177.67, Invested: 1533365.52, Cash-On-Hand: 86812.15
      2020-05-20, IN HAND ---> [('KAREL', 15.93, 19.51, 15.14, 16.26, 0), ('GUBRF', 18.46, 22.61, 17.54, 18.84, 0), ('HEKTS', 13.67, 16.74, 12.99, 13.95, 0), ('RYGYO', 4.73, 5.8, 4.5, 4.83, 0), ('GUSGR', 3.24, 3.97, 3.08, 3.31, 0)]
      2020-05-21, Total Wealth: 1639291.91, Invested: 1552479.76, Cash-On-Hand: 86812.15
      2020-05-21, IN HAND ---> [('KAREL', 15.56, 19.06, 14.78, 15.88, 0), ('GUBRF', 19.99, 24.48, 18.99, 20.4, 0), ('HEKTS', 13.58, 16.63, 12.9, 13.86, 0), ('RYGYO', 4.81, 5.89, 4.57, 4.91, 0), ('GUSGR', 3.19, 3.91, 3.04, 3.26, 0)]
      2020-05-22, Total Wealth: 1657420.90, Invested: 1570608.75, Cash-On-Hand: 86812.15
      2020-05-22, IN HAND ---> [('KAREL', 15.67, 19.19, 14.89, 15.99, 0), ('GUBRF', 19.46, 23.83, 18.49, 19.86, 0), ('HEKTS', 14.7, 18.0, 13.96, 15.0, 0), ('RYGYO', 4.81, 5.89, 4.57, 4.91, 0), ('GUSGR', 3.21, 3.94, 3.05, 3.28, 0)]
      2020-05-27, Total Wealth: 1686695.71, Invested: 1599883.56, Cash-On-Hand: 86812.15
      2020-05-27, IN HAND ---> [('KAREL', 16.32, 19.98, 15.5, 16.65, 0), ('GUBRF', 19.57, 23.96, 18.59, 19.97, 0), ('HEKTS', 14.9, 18.24, 14.15, 15.2, 0), ('RYGYO', 4.9, 6.0, 4.66, 5.0, 0), ('GUSGR', 3.27, 4.01, 3.11, 3.34, 0)]
      2020-05-28, Total Wealth: 1669631.77, Invested: 1582819.62, Cash-On-Hand: 86812.15
      2020-05-28, IN HAND ---> [('KAREL', 15.11, 18.5, 14.36, 15.42, 0), ('GUBRF', 20.99, 25.7, 19.94, 21.42, 0), ('HEKTS', 14.51, 17.77, 13.79, 14.81, 0), ('RYGYO', 4.76, 5.83, 4.52, 4.86, 0), ('GUSGR', 3.22, 3.95, 3.06, 3.29, 0)]
      2020-05-29, Total Wealth: 1692349.23, Invested: 1605537.08, Cash-On-Hand: 86812.15
      2020-05-29, IN HAND ---> [('KAREL', 14.95, 18.31, 14.21, 15.26, 0), ('GUBRF', 22.13, 27.1, 21.02, 22.58, 0), ('HEKTS', 14.5, 17.76, 13.78, 14.8, 0), ('RYGYO', 4.72, 5.78, 4.49, 4.82, 0), ('GUSGR', 3.29, 4.03, 3.13, 3.36, 0)]
      2020-06-01, Leave KAREL
      2020-06-01, Rebal GUBRF
      2020-06-01, Rebal HEKTS
      2020-06-01, Rebal RYGYO
      2020-06-01, Rebal GUSGR
      2020-06-01, Enter DEVA
      2020-06-01, Total Wealth: nan, Invested: nan, Cash-On-Hand: 86812.15
      
      

      I rebalance once in a month and check values everyday. In the logs I don't see any nan value for the stocks I have in hand, but the portfolio value is nan. What is that I am missing here?

      M 1 Reply Last reply Reply Quote 0
      • M
        mcandar @mcandar last edited by

        @mcandar Any clues how to avoid this? What is the reason that causes NaN values?

        1 Reply Last reply Reply Quote 0
        • A
          ab_trader last edited by

          I would recommend the follwoing tools -

          • pdb — The Python Debugger
          • Python Debugging With Pdb

          or another debugging tools (if available) in your IDE. Can be quicker, than waiting for an answer here.

          • If my answer helped, hit reputation up arrow at lower right corner of the post.
          • Python Debugging With Pdb
          • New to python and bt - check this out
          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors