Helper script

In this script, we will create functions that will be helpful for training, via the following steps:

  1. Import the numpy and math modules, as follows:
import numpy as npimport math
  1. Next, define a function to format the price to two decimal places, to reduce the ambiguity of the data:
def formatPrice(n):        if n>=0:        curr = "$"    else:        curr = "-$"    return (curr +"{0:.2f}".format(abs(n)))
  1. Return a vector of stock data from the CSV file. Convert the closing stock prices from the data to vectors, and return a vector of all stock prices, as follows:
def getStockData(key):    datavec = []    lines = open("data/" + key + ".csv", "r").read().splitlines()        for line in lines[1:]:        datavec.append(float(line.split(",")[4]))        return datavec
  1. Next, ...

Get Python Reinforcement Learning Projects now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.