﻿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:]:
		vec.append(float(line.split(",")[4])) # close 收盤價

	return vec

# returns the sigmoid
def sigmoid(x):
	return 1 / (1 + math.exp(-x))

# returns an an n-day state representation ending at time t
def getState(data, t, n,maxd,mind,amount,money,his):
	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]))
	res.append(sigmoid((maxd-data[t])))
	res.append(sigmoid((data[t] - mind)))
	res.append(amount)
	res.append(money)
	if math.isnan(his):
		res.append(0)
	else:
		res.append(sigmoid((data[t] - his)))
	return np.array([res])
