-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtests.py
58 lines (46 loc) · 1.59 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from trading_gym import ACTION, Observation, Exchange, TradeEnv
import random
def test_exchange():
nav = 1000
ex = Exchange(nav=nav)
charge = ex.get_charge(ACTION.PUSH, 10)
ex.step(ACTION.PUSH, Observation(close=10))
assert ex.profit == -charge
assert ex.fixed_profit == -charge
charge += ex.get_charge(ACTION.HOLD, 11)
ex.step(ACTION.HOLD, Observation(close=11))
assert ex.floating_profit == nav * 0.1
assert ex.profit == -charge + nav * 0.1
charge += ex.get_charge(ACTION.PUT, 12)
ex.step(ACTION.PUT, Observation(close=12))
assert ex.profit == -charge + nav * 0.2
print(ex.transaction)
def test_render():
env = TradeEnv(data_path='./data/test_exchange.json')
action = 0
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
def test_custom_reward_func():
def reward_fn(exchange):
return 2
env = TradeEnv(data_path='./data/test_exchange.json',
get_reward_func=reward_fn)
env.reset()
obs, reward, done, info = env.step(ACTION.PUSH)
assert reward == 2
def reward_fn(exchange):
return exchange.floating_profit
env = TradeEnv(data_path='./data/test_exchange.json',
get_reward_func=reward_fn)
env.reset()
obs, reward, done, info = env.step(ACTION.PUSH)
obs, reward, done, info = env.step(ACTION.HOLD)
assert reward == env.exchange.floating_profit
if __name__ == '__main__':
test_render()