Hello everyone!
Needing your help to figure out on how to resolve the error message i get after running code below (credit: https://github.com/SravB/Algorithmic-Trading):
Needing your help to figure out on how to resolve the error message i get after running code below (credit: https://github.com/SravB/Algorithmic-Trading):
#Algorithmic Trading with Machine Learning
#imports
from time import *
from sklearn import tree
import datetime as dt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import time
start_time = time.time()
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
#trading algorithm
def algo(t):
features = []
labels = []
for i in range(len(t) - acc + 1):
features.append(t[-1*acc:-1])
#1 means price went up
if t[-1] > t[-2]:
labels.append(1)
else:
labels.append(0)
clf = tree.DecisionTreeClassifier()
clf.fit(features, labels)
#if clf.predict(t[-1*acc+1:])[0] == 1:
if clf.predict(t[-1*acc+1:])[0] == 1:
return 1
else:
return 0
#fields
acc = 10
Points = []
dates = []
CashRecords = []
Cash = 100
Bought = False
days = 0
decision = 0
stockSymbol = 'AAPL'
style.use('ggplot')
start = dt.datetime(2015,1,1)
end = dt.datetime(2016,12,31)
#importing data
df = web.DataReader(stockSymbol,'yahoo',start,end)
df.to_csv('data.csv')
df = pd.read_csv('data.csv', parse_dates = True)
for i in df[['Close']]:
count = 0
for j in df[i]:
Points.append(round(j,2))
for i in df[['Date']]:
count = 0
for j in df[i]:
dates.append(dt.datetime.strptime(j, "%Y-%m-%d"))
#graph labels
plt.figure(num = stockSymbol)
plt.title(stockSymbol + " Stock Algorithmic Trading Analysis")
plt.xlabel('Date')
plt.ylabel('Stock Price / Cash')
while days <= len(df[['Close']]) - 1:
#stock info
days += 1
StockPrice = Points[days - 1]
if days == 1:
initP = StockPrice
initC = Cash
#your money
if Bought == True:
Cash = round(Cash*StockPrice/Points[days-2],2)
c = "green"
else:
c = "red"
CashRecords.append(Cash)
if days > acc:
decision = algo(Points[:days])
if Bought == True:
if decision == 0:
Bought = False
else:
if decision == 1:
Bought = True
plt.plot(dates[days - 2:days], Points[days - 2:days], color=c)
print("Ending Cash: " + str(CashRecords[-1]))
print("Expected Cash: " + str(round(CashRecords[0] * Points[-1] / Points[0],2)))
print("Performance: " + str(round(100 * CashRecords[-1] * Points[0] / (Points[-1] * CashRecords[0]),2)) + "%")
plt.plot(dates, CashRecords, color='blue')
plt.show()The error message:Error:Traceback (most recent call last):
File "C:/Users/.../Desktop/algoscore_lab.py", line 100, in <module>
decision = algo(Points[:days])
File "C:/Users/.../Desktop/algoscore_lab.py", line 38, in algo
if clf.predict(t[-1*acc+1:])[0] == 1:
File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree\_classes.py", line 419, in predict
X = self._validate_X_predict(X, check_input)
File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\tree\_classes.py", line 380, in _validate_X_predict
X = check_array(X, dtype=DTYPE, accept_sparse="csr")
File "C:\Users\...\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array
raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[106.26 107.75 111.89 112.01 109.25 110.22 109.8 106.82 105.99].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
