forked from matinaghaei/Portfolio-Management-ActorCriticRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_agent.py
41 lines (35 loc) · 1.46 KB
/
random_agent.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
#%%
import warnings
warnings.filterwarnings('ignore')
from env.environment import PortfolioEnv
import numpy as np
import pandas as pd
from pyfolio import timeseries
env = PortfolioEnv(state_type='indicators')
action_shape = env.action_shape()
intervals = env.get_intervals()
buy_hold_history = env.buy_hold_history(*intervals['testing'])
stats = []
iteration = 1
while iteration <= 50:
print(f'interation {iteration}')
observation = env.reset(*intervals['testing'])
wealth_history = [env.get_wealth()]
done = False
while not done:
action = np.eye(*action_shape)[np.random.choice(*action_shape)]
observation_, reward, done, info, wealth = env.step(action, softmax=False)
observation = observation_
# print(f"random - Date: {info.date()},\tBalance: {int(env.get_balance())},\t"
# f"Cumulative Return: {int(wealth) - 1000000},\tShares: {env.get_shares()}")
wealth_history.append(wealth)
returns = pd.Series(wealth_history, buy_hold_history.index).pct_change().dropna()
stats.append(timeseries.perf_stats(returns))
iteration += 1
#%%
annual = [data['Annual return'] for data in stats]
print(f'Annual return\tmean: {np.mean(annual)}, std: {np.std(annual)}')
sharp = [data['Sharpe ratio'] for data in stats]
print(f'Sharpe ratio\tmean: {np.mean(sharp)}, std: {np.std(sharp)}')
drawdown = [data['Max drawdown'] for data in stats]
print(f'Max drawdown\tmean: {np.mean(drawdown)}, std: {np.std(drawdown)}')