forked from viuts/q-trading-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfunctions.py
32 lines (25 loc) · 787 Bytes
/
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
import numpy as np
import math
# prints formatted price
def formatPrice(n):
return ("-$" if n < 0 else "$") + "{0:.2f}".format(abs(n))
# returns the vector containing stock data from a fixed file
def getStockDataVec(key):
vec = []
lines = open("data/" + key + ".csv", "r").read().splitlines()
for line in lines[1:]:
close = line.split(",")[4]
if close != 'null':
vec.append(float(line.split(",")[4]))
return vec
# returns the sigmoid
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# returns an an n-day state representation ending at time t
def getState(data, t, n):
d = t - n + 1
block = data[d:t + 1] if d >= 0 else -d * [data[0]] + data[0:t + 1] # pad with t0
res = []
for i in range(n - 1):
res.append(sigmoid(block[i + 1] - block[i]))
return np.array([res])