Skip to content

Commit a896866

Browse files
committed
Merge branch 'develop'
2 parents 871a693 + 5cf8e74 commit a896866

17 files changed

+768
-251
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"request": "launch",
1111
"program": "${file}",
1212
"console": "integratedTerminal",
13-
"justMyCode": true
13+
"justMyCode": false
1414
}
1515
]
1616
}

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## [0.5.0] - 2024-01-30
2+
### Added:
3+
- Added `MACD` indicator to `indicators` file.
4+
- Added `reward.AccountValueChangeReward` object to calculate reward based on the change in the account value.
5+
- Added `scalers.ZScoreScaler` that doesn't require min and max to transform data, but uses mean and std instead.
6+
- Added `ActionSpace` object to handle the action space of the agent.
7+
- Added support for continuous actions. (float values between 0 and 1)
8+
9+
### Changed:
10+
- Updated all indicators to have `config` parameter, that we can use so we can serialize the indicators. (save/load configurations to/from file)
11+
- Changed `reward.simpleReward` to `reward.SimpleReward` Object.
12+
- Updated `state.State` to have `open`, `high`, `low`, `close` and `volume` attributes.
13+
- Updated `data_feeder.PdDataFeeder` to be serializable by including `save_config` and `load_config` methods.
14+
- Included trading fees into `trading_env.TradingEnv` object.
15+
- Updated `trading_env.TradingEnv` to have `reset` method, which resets the environment to the initial state.
16+
- Included `save_config` and `load_config` methods into `trading_env.TradingEnv` object, so we can save/load the environment configuration.
17+
118
## [0.4.0] - 2024-01-02
219
### Added:
320
- Created `indicators` file, where I added `BolingerBands`, `RSI`, `PSAR`, `SMA` indicators

experiments/playing_random_sinusoid.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
from finrock.data_feeder import PdDataFeeder
55
from finrock.trading_env import TradingEnv
66
from finrock.render import PygameRender
7-
from finrock.scalers import MinMaxScaler
8-
from finrock.reward import simpleReward
9-
from finrock.indicators import BolingerBands, SMA, RSI, PSAR
7+
from finrock.scalers import ZScoreScaler
8+
from finrock.reward import AccountValueChangeReward
9+
from finrock.indicators import BolingerBands, SMA, RSI, PSAR, MACD
10+
from finrock.metrics import DifferentActions, AccountValue, MaxDrawdown, SharpeRatio
1011

1112
df = pd.read_csv('Datasets/random_sinusoid.csv')
1213

@@ -16,23 +17,30 @@
1617
BolingerBands(data=df, period=20, std=2),
1718
RSI(data=df, period=14),
1819
PSAR(data=df),
20+
MACD(data=df),
1921
SMA(data=df, period=7),
20-
SMA(data=df, period=25),
21-
SMA(data=df, period=99),
2222
]
2323
)
2424

2525
env = TradingEnv(
2626
data_feeder = pd_data_feeder,
27-
output_transformer = MinMaxScaler(min=pd_data_feeder.min, max=pd_data_feeder.max),
27+
output_transformer = ZScoreScaler(),
2828
initial_balance = 1000.0,
2929
max_episode_steps = 1000,
3030
window_size = 50,
31-
reward_function = simpleReward
31+
reward_function = AccountValueChangeReward(),
32+
metrics = [
33+
DifferentActions(),
34+
AccountValue(),
35+
MaxDrawdown(),
36+
SharpeRatio(),
37+
]
3238
)
3339
action_space = env.action_space
3440
input_shape = env.observation_space.shape
3541

42+
env.save_config()
43+
3644
pygameRender = PygameRender(frame_rate=60)
3745

3846
state, info = env.reset()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import pandas as pd
3+
import tensorflow as tf
4+
tf.get_logger().setLevel('ERROR')
5+
for gpu in tf.config.experimental.list_physical_devices('GPU'):
6+
tf.config.experimental.set_memory_growth(gpu, True)
7+
8+
from finrock.data_feeder import PdDataFeeder
9+
from finrock.trading_env import TradingEnv
10+
from finrock.render import PygameRender
11+
12+
13+
df = pd.read_csv('Datasets/random_sinusoid.csv')
14+
df = df[-1000:]
15+
16+
model_path = "runs/1704798174"
17+
18+
pd_data_feeder = PdDataFeeder.load_config(df, model_path)
19+
env = TradingEnv.load_config(pd_data_feeder, model_path)
20+
21+
action_space = env.action_space
22+
input_shape = env.observation_space.shape
23+
pygameRender = PygameRender(frame_rate=120)
24+
25+
agent = tf.keras.models.load_model(f'{model_path}/ppo_sinusoid_actor.h5')
26+
27+
state, info = env.reset()
28+
pygameRender.render(info)
29+
rewards = 0.0
30+
while True:
31+
# simulate model prediction, now use random action
32+
action = agent.predict(np.expand_dims(state, axis=0), verbose=False)[0][:-1]
33+
34+
state, reward, terminated, truncated, info = env.step(action)
35+
rewards += reward
36+
pygameRender.render(info)
37+
38+
if terminated or truncated:
39+
print(rewards)
40+
for metric, value in info['metrics'].items():
41+
print(metric, value)
42+
state, info = env.reset()
43+
rewards = 0.0
44+
pygameRender.reset()
45+
pygameRender.render(info)

experiments/testing_ppo_sinusoid.py renamed to experiments/testing_ppo_sinusoid_discrete.py

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,27 @@
88
from finrock.data_feeder import PdDataFeeder
99
from finrock.trading_env import TradingEnv
1010
from finrock.render import PygameRender
11-
from finrock.scalers import MinMaxScaler
12-
from finrock.reward import simpleReward
13-
from finrock.metrics import DifferentActions, AccountValue, MaxDrawdown, SharpeRatio
14-
from finrock.indicators import BolingerBands, RSI, PSAR, SMA
1511

1612

1713
df = pd.read_csv('Datasets/random_sinusoid.csv')
1814
df = df[-1000:]
1915

20-
pd_data_feeder = PdDataFeeder(
21-
df,
22-
indicators = [
23-
BolingerBands(data=df, period=20, std=2),
24-
RSI(data=df, period=14),
25-
PSAR(data=df),
26-
SMA(data=df, period=7),
27-
SMA(data=df, period=25),
28-
SMA(data=df, period=99),
29-
]
30-
)
16+
model_path = "runs/1704746665"
3117

32-
env = TradingEnv(
33-
data_feeder = pd_data_feeder,
34-
output_transformer = MinMaxScaler(min=pd_data_feeder.min, max=pd_data_feeder.max),
35-
initial_balance = 1000.0,
36-
max_episode_steps = 1000,
37-
window_size = 50,
38-
reward_function = simpleReward,
39-
metrics = [
40-
DifferentActions(),
41-
AccountValue(),
42-
MaxDrawdown(),
43-
SharpeRatio(),
44-
]
45-
)
18+
pd_data_feeder = PdDataFeeder.load_config(df, model_path)
19+
env = TradingEnv.load_config(pd_data_feeder, model_path)
4620

4721
action_space = env.action_space
4822
input_shape = env.observation_space.shape
4923
pygameRender = PygameRender(frame_rate=120)
5024

51-
agent = tf.keras.models.load_model('runs/1702982487/ppo_sinusoid_actor.h5')
25+
agent = tf.keras.models.load_model(f'{model_path}/ppo_sinusoid_actor.h5')
5226

5327
state, info = env.reset()
5428
pygameRender.render(info)
5529
rewards = 0.0
5630
while True:
5731
# simulate model prediction, now use random action
58-
# action = np.random.randint(0, action_space)
5932
prob = agent.predict(np.expand_dims(state, axis=0), verbose=False)[0]
6033
action = np.argmax(prob)
6134

experiments/training_ppo_sinusoid.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)