code: def hold_time(self, datetime=None):
          
  
          def weights(x):
              if sum(x['amount']) != 0:
                  return pd.Timestamp(self.datetime
                                     ) - pd.to_datetime(x.datetime.max())
              else:
                  return np.nan
  
          if datetime is None:
              return self.history_table.set_index(
                  'datetime',
                  drop=False
              ).sort_index().groupby('code').apply(weights).dropna()
          else:
              return self.history_table.set_index(
                  'datetime',
                  drop=False
              ).sort_index().loc[:datetime].groupby('code').apply(weights
                                                                 ).dropna()
docs: 持仓时间
  
          Keyword Arguments:
              datetime {[type]} -- [description] (default: {None})