-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparison between twap,vwap,model.py
148 lines (113 loc) · 3.82 KB
/
comparison between twap,vwap,model.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from cProfile import label
from turtle import color
from data import *
from actorcritic import *
from parameters import *
import pandas as pd
import csv
import requests
import time
import os
import math
import itertools
import numpy as np
import matplotlib.pyplot as plt
os.chdir(os.path.dirname(os.path.abspath(__file__)))
current_dir = os.getcwd()
ref_folder = os.path.join(current_dir, 'preprocessed_dir')
files = [name for name in os.listdir(ref_folder) if name!= 'vwap calculation.py']
file = files[ref_stock]
df = pd.read_csv(os.path.join(ref_folder, file))
df = df.iloc[-T*100:]
df = df.iloc[:T]
df = df.iloc[::-1]
print(len(df))
Q = sum(df['volume'].tolist())*0.01
print(Q)
volume_list = df['volume'].tolist()
price = df['vwap'].tolist()
volume_list2 = []
for i in range(len(volume_list)):
volume_list2.append(volume_list[i]*price[i])
twap_list = []
for i in range(len(df)):
twap_list.append(Q/T*price[i])
weight_vwap_list = []
for i in range(len(volume_list)):
weight_vwap_list.append(volume_list[i]/sum(volume_list))
vwap_list = []
for i in range(len(df)):
q = Q*weight_vwap_list[i]
vwap_list.append(q*price[i])
model_list = []
for k in range(1,2):
PATH = 'seed_' + str(k) + '_model.pt'
#print(PATH)
agent = Agent(model, is_eval=True, model_name=PATH)
env.reset()
state, _ = getState(all_stressed_test_list[-1], all_test_vwap_list[-1], env.time, state_size)
for j in range(state_size, len((all_stressed_test_list[15]))):
if j%3==0:
already_bought_tensor = torch.Tensor(env.already_bought_list)
left_time = T - env.time
left_time_tensor = torch.Tensor(list(range(left_time+1,left_time+1+state_size)))
private_input = torch.stack((already_bought_tensor, left_time_tensor)).reshape(state_size, 2)
private_input = private_input.to(device)
state = state.to(device)
action, _ = agent.act(private_input, state)
model_list.append((action*Q*price[j]).item())
'''if action*Q*price[j] > 1e6:
model_list.append((action*Q*price[j]).item())
model_list.append((action*Q*price[j]).item())
model_list.append((action*Q*price[j]).item())
else:
model_list.append((action*Q*price[j]).item())
model_list.append((action*Q*price[j]).item())
model_list.append((action*Q*price[j]).item())'''
print(df)
model_list2 = modellist
x = list(range(len(volume_list)))
plt.plot(twap_list, label = 'Trade by TWAP', color = 'b')
plt.fill_between(
x, volume_list2, alpha=0.25,
label="Total trade in the market"
)
plt.grid(alpha=0.2)
plt.ylim(0,3000000)
plt.xlabel('Minute of the Day')
plt.ylabel('Volume to Sell in USD')
plt.legend()
plt.title('Division of the Order in TWAP Strategy')
plt.show()
x1 = list(range(len(vwap_list)))
coef_vwap = np.polyfit(x1,vwap_list,5)
poly1d_fn_vwap = np.poly1d(coef_vwap)
plt.plot(x1, poly1d_fn_vwap(x1), 'b-', label = 'Regression Line')
plt.plot(vwap_list, label = 'Trade by VWAP', color = '0.8')
plt.fill_between(
x, volume_list2, alpha=0.25,
label="Total trade in the market"
)
plt.grid(alpha=0.2)
plt.ylim(0,3000000)
plt.xlabel('Minute of the Day')
plt.ylabel('Volume to Sell in USD')
plt.legend()
plt.title('Division of the Order in VWAP Strategy')
plt.show()
x1 = list(range(len(model_list2)))
coef_vwap = np.polyfit(x1,model_list2,5)
poly1d_fn_vwap = np.poly1d(coef_vwap)
plt.plot(x1, poly1d_fn_vwap(x1), 'b-', label = 'Regression Line')
plt.plot(model_list2, label = 'Trade by DRL', color = '0.8')
plt.fill_between(
x, volume_list2, alpha=0.25,
label="Total trade in the market"
)
plt.grid(alpha=0.2)
plt.ylim(0,3000000)
plt.xlabel('Minute of the Day')
plt.ylabel('Volume to Sell in USD')
plt.legend()
plt.title('Division of the Order in DRL Strategy')
plt.show()