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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.