A Trading environment base on Gym
Trading-Gym
Trading-Gym is a trading environment base on Gym. For those who want to custom everything.
install
$ pip install trading-gym
Creating features with ta-lib is suggested, that will improve the performance of agent and make it easy to learn. You should install ta-lib before it. Take Ubuntu x64 for example.
$ wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
$ tar -zxvf ta-lib-0.4.0-src.tar.gz
$ cd ta-lib/
$ ./configure --prefix=$PREFIX
$ make install
$ export TALIBRARYPATH=$PREFIX/lib $ export TAINCLUDEPATH=$PREFIX/include
$ pip install TA-Lib
See more.
Examples
quick start
from trading_gym.env import TradeEnv
import random
env = TradeEnv(datapath='./data/testexchange.json') done = False obs = env.reset() for i in range(500): action = random.sample([0, 1, 2], 1)[0] obs, reward, done, info = env.step(action) env.render() if done: break
A sample train with stable-baselines
from trading_gym.env import TradeEnv
from stablebaselines.common.vecenv import DummyVecEnv
from stable_baselines import DQN
from stable_baselines.deepq.policies import MlpPolicy
datapath = './data/fakesin_data.json' env = TradeEnv(datapath=datapath, unit=50000, datakwargs={'useta': True}) env = DummyVecEnv([lambda: env])
model = DQN(MlpPolicy, env, verbose=2, learning_rate=1e-5) model.learn(200000)
obs = env.reset() for i in range(8000): action, _states = model.predict(obs) obs, rewards, done, info = env.step(action) env.render() if done: break

input format
[
{
"open": 10.0,
"close": 10.0,
"high": 10.0,
"low": 10.0,
"volume": 10.0,
"date": "2019-01-01 09:59"
},
{
"open": 10.1,
"close": 10.1,
"high": 10.1,
"low": 10.1,
"volume": 10.1,
"date": "2019-01-01 10:00"
}
]
actions
| Action | Value | | ------ | ----- | | PUT | 0 | | HOLD | 1 | | PUSH | 2 |
observation
- native obs: shape=(*, 51, 6), return 51 history data with OCHL
env = TradeEnv(datapath=datapath)
- obs with ta: shape=(*, 10), return obs using talib.
- - default feature:
['ema', 'wma', 'sma', 'sar', 'apo', 'macd', 'macdsignal', 'macdhist', 'adosc', 'obv']
env = TradeEnv(datapath=datapath, datakwargs={'useta': True})
Custom
custom obs
def customobsfeatures_func(history, info):
close = [obs.close for obs in history]
return close
env = TradeEnv(datapath=datapath, getobsfeaturesfunc=customobsfeaturesfunc, ops_shape=(1))
custom reward
def customrewardfunc(exchange):
return exchange.profit
env = TradeEnv(datapath=datapath, getrewardfunc=customrewardfunc)
Param exchange is entity of Exchange
Reward
- reward = fixed_profit
- profit = fixedprofit + floatingprofit
- floatingprofit = (latestprice - avg_price) * unit
- unit = int(nav / buyinprice)
- avgprice = ((buyin_price * unit) + charge) / unit
- fixedprofit = SUM([every floatingprofit after close position])