-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
43 lines (31 loc) · 1.26 KB
/
functions.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
import numpy as np
import math
from pandas_datareader import data
from time import gmtime
from time import strftime
# prints formatted price
from csv_reader import intraday_from_csv
def format_price(n):
return ("-" if n < 0 else '') + "{0:.0f}".format(abs(n))
def format_time(t):
return strftime("%H:%M:%S", gmtime(t))
# returns the vector containing stock data from a fixed file
def get_stock_data(key, start, end):
# # User pandas_reader.data.DataReader to load the desired data. As simple as that.
# panel_data = data.DataReader(key, 'yahoo', start, end)
#
# # Getting just the adjusted closing prices. This will return a Pandas DataFrame
# # The index in this DataFrame is the major index of the panel_data.
# return panel_data['Close'].tolist()
data = intraday_from_csv(start, end)
print('data length: ', len(data))
return data
# returns the sigmoid
def sigmoid(x):
return 1 / (1 + math.exp(-x))
# returns an an n-day state representation ending at time t
def get_state(data, t, n, inventory_size):
d = t - n + 1
block = data[d:t + 1] if d >= 0 else -d * [data[0]] + data[0:t + 1] # pad with t0
res = [sigmoid(block[i + 1] - block[i]) for i in range(n - 1)]
return np.array([np.append(inventory_size, res)])