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

AttributeError: 'tuple' object has no attribute 'keys' #44

Closed sharpwood closed 6 years ago

sharpwood commented 6 years ago

When I ran the following example code:

from btgym import BTgymEnv MyEnvironment = BTgymEnv(filename='/home/Code/btgym-master/examples/data/DAT_ASCII_EURUSD_M1_2016.csv', episode_duration={'days': 2, 'hours': 23, 'minutes': 55}, drawdown_call=50, state_shape=(4,20), port=5555, verbose=1, )

[@-pc btgym-master]$ cd /home/Code/btgym-master ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" python /home/.vscode/extensions/ms-python.python-2018.3.1/pythonFiles/PythonTools/visualstudio_py_launcher.py /home/Code/btgym-master 33543 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /home/Code/btgym-master/examples/test.py [2018-04-02 03:29:58.845135] INFO: BTgymAPIshell_0: Base Dataset class used. [2018-04-02 03:29:58.845766] INFO: BTgymAPIshell_0: Connecting data_server... [2018-04-02 03:29:58.936939] INFO: BTgymDataServer_0: PID: 6136 [2018-04-02 03:29:59.969214] INFO: SimpleDataSet_0: Loaded 372678 records from </home//Code/btgym-master/examples/data/DAT_ASCII_EURUSD_M1_2016.csv>. [2018-04-02 03:30:00.145812] INFO: SimpleDataSet_0: Data summary: open high low close volume count 372678.000000 372678.000000 372678.000000 372678.000000 372678.0 mean 1.107109 1.107198 1.107019 1.107108 0.0 std 0.024843 0.024840 0.024847 0.024844 0.0 min 1.035250 1.035470 1.035220 1.035220 0.0 25% 1.092140 1.092230 1.092040 1.092140 0.0 50% 1.113530 1.113610 1.113450 1.113530 0.0 75% 1.124710 1.124780 1.124630 1.124710 0.0 max 1.161440 1.161600 1.160770 1.161450 0.0 [2018-04-02 03:30:00.152136] INFO: BTgymAPIshell_0: ...done. [2018-04-02 03:30:00.154973] INFO: BTgymAPIshell_0: Base Cerebro class used. Base Strategy class used. Traceback (most recent call last): File "/home/liguodong/Code/btgym-master/examples/test.py", line 28, in verbose=1, File "/home/liguodong/Code/btgym-master/btgym/envs/backtrader.py", line 349, in init if 'raw_state' in self.params['strategy']['state_shape'].keys(): AttributeError: 'tuple' object has no attribute 'keys'

Kismuz commented 6 years ago

@sharpwood , see https://github.com/Kismuz/btgym/blob/master/examples/setting_up_environment_basic.ipynb ... state_shape should be dictionary:

from gym import spaces
import numpy as np
................
# Set observation shape. By convention, FIRST dimension 
# is time embedding dimensionality;
# that's basically means we get sequence of 30 last  
# [o,h,l,c] candels as our one-step environment observation:

state_shape=dict(raw_state=spaces.Box(low=0, high=1, shape=(30,4), dtype=np.float32)),

# BTgym uses multi-modal observation space which is basically dictionary
# consisting of simple gym spaces (Box, discrete, etc.)
# For the built-in `raw_state` setting high and low are dummy, because
# environment will infer values from entire dataset statistic.

...so your code can look like:

import warnings
warnings.filterwarnings("ignore") 

from btgym import BTgymEnv
from gym import spaces
import numpy as np

MyEnvironment = BTgymEnv(filename='../examples/data/DAT_ASCII_EURUSD_M1_2016.csv',
episode_duration={'days': 2, 'hours': 23, 'minutes': 55},
drawdown_call=50,
state_shape=dict(
    raw_state=spaces.Box(
        shape=(20, 4),
        low=-100,
        high=100,
        dtype=np.float32
    )
),
port=92201,
data_port=92200,
verbose=1,
)

init_obs = MyEnvironment.reset()

print(init_obs)

MyEnvironment.close()
sharpwood commented 6 years ago

Thanks you