Kismuz / btgym

Scalable, event-driven, deep-learning-friendly backtesting library
https://kismuz.github.io/btgym/
GNU Lesser General Public License v3.0
985 stars 260 forks source link

BTgymEnv init with error 'Dict' object has no attribute 'shape' #22

Closed kolesnikov closed 6 years ago

kolesnikov commented 6 years ago

Andrew, first thank you for your work! In the last commit initialization env crashes with an error. What could be the problem? Thanks in advance.

MyCerebro = bt.Cerebro()

MyCerebro.addstrategy(
    DevStrat_4_6,
    drawdown_call=5, # max % to loose, in percent of initial cash
    target_call=10,  # max % to win, same
    skip_frame=10,
)

MyDataset = BTgymDataset(
    #filename='.data/DAT_ASCII_EURUSD_M1_201703.csv',
    #filename='./data/DAT_ASCII_EURUSD_M1_201704.csv',
    filename='./data/test_sine_1min_period256_delta0002.csv',
    start_weekdays={0, 1, 2, 3},
    episode_duration={'days': 0, 'hours': 23, 'minutes': 55},
    start_00=False,
    time_gap={'hours': 6},
)

kwargs=dict(
         dataset=MyDataset,
         engine=MyCerebro,
         render_modes=['episode', 'human','external'],
         render_state_as_image=True,
         render_ylabel='OHL_diff.',
         render_size_episode=(12,8),
         render_size_human=(9, 4),
         render_size_state=(11, 3),
         render_dpi=75,
         port=5000,
         data_port=4999,
         connect_timeout=60,
         verbose=0,  # better be 0
     )
env = BTgymEnv(**kwargs)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-7-2abf37cd704a> in <module>()
----> 1 env = BTgymEnv(**kwargs)

/var/www/btgym/btgym/envs/backtrader.py in __init__(self, *args, **kwargs)
    334 
    335         # Set observation space shape from engine/strategy parameters:
--> 336         self.observation_space = DictSpace(self.params['strategy']['state_shape'])
    337 
    338         self.log.debug('Obs. shape: {}'.format(self.observation_space.spaces))

/var/www/btgym/btgym/spaces.py in __init__(self, spaces)
     37         """
     38         super(DictSpace, self).__init__(spaces)
---> 39         self.shape = self._get_shape()
     40 
     41     def _get_shape(self):

/var/www/btgym/btgym/spaces.py in _get_shape(self)
     40 
     41     def _get_shape(self):
---> 42         return OrderedDict([(k, space.shape) for k, space in self.spaces.items()])
     43 
     44 

/var/www/btgym/btgym/spaces.py in <listcomp>(.0)
     40 
     41     def _get_shape(self):
---> 42         return OrderedDict([(k, space.shape) for k, space in self.spaces.items()])
     43 
     44 

AttributeError: 'Dict' object has no attribute 'shape'
Kismuz commented 6 years ago

@kolesnikov, Seems I have violated my own deprecation rules and used new gym.spaces.Dict instead of btgym.spaces.DictSpace wrapper which has been made exactly to avoid this error :| Do the following:

kolesnikov commented 6 years ago

works great, thanks!