-
Notifications
You must be signed in to change notification settings - Fork 0
/
import numpy.txt
157 lines (93 loc) · 3.61 KB
/
import numpy.txt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
numpy
pandas
scikit-learn
keras
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# Load the data
df = pd.read_csv("https://query1.finance.yahoo.com/v7/finance/download/YHOO?period1=1577836800&period2=1609459199&interval=1d&events=history")
# Split the data into training and testing sets
X = df[['Open', 'High', 'Low', 'Close', 'Volume']]
y = df['Close']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Create and train the model
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Make predictions on the testing data
y_pred = regressor.predict(X_test)
# Evaluate the model
score = regressor.score(X_test, y_test)
print("R-squared value:", score)
tensorflow
import alpaca_trade_api as tradeapi
# Replace with your API key
api = tradeapi.REST('APCA-API-KEY-ID', 'APCA-API-SECRET-KEY', base_url='https://paper-api.alpaca.markets')
# Retrieve the historical data for a stock
stock = 'AAPL'
barset = api.get_barset(stock, 'day', limit=30)
aapl_bars = barset[stock]
# Print out the data
for bar in aapl_bars:
print("Date:", bar.t, "Open:", bar.o, "High:", bar.h, "Low:", bar.l, "Close:", bar.c, "Volume:", bar.v)
pip freeze > requirements.txt
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense
import alpaca_trade_api as tradeapi
# Replace with your API key
api = tradeapi.REST('APCA-API-KEY-ID', 'APCA-API-SECRET-KEY', base_url='https://paper-api.alpaca.markets')
# Retrieve the historical data for a stock
stock = 'AAPL'
barset = api.get_barset(stock, 'day', limit=30)
aapl_bars = barset[stock]
# Print out the data
for bar in aapl_bars:
print("Date:", bar.t, "Open:", bar.o, "High:", bar.h, "Low:", bar.l, "Close:", bar.c, "Volume:", bar.v)
# Load the stock data
df = pd.read_csv("Yahoo_stock_data.csv")
# Scale the data
scaler = MinMaxScaler(feature_range=(0, 1))
df = scaler.fit_transform(df)
# Split the data into training and testing sets
train_size = int(len(df) * 0.8)
test_size = len(df) - train_size
train, test = df[0:train_size,:], df[train_size:len(df),:]
# Convert the data into a 3D array (a x b x c)
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return np.array(dataX), np.array(dataY)
# Use the function to create dataset
look_back = 1
X_train, y_train = create_dataset(train, look_back)
X_test, y_test = create_dataset(test, look_back)
# Reshape the data for the LSTM model
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))
# Build the LSTM model
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
# Train the model
model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=2)
# Make predictions
trainPredict = model.predict(X_train)
testPredict = model.predict(X_test)
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2
# create the environment
env = gym.make('CartPole-v1')
# create a wrapper to convert the environment to a vectorized environment
vec_env = DummyVecEnv([lambda: env])
# define the model
model = PPO2(MlpPolicy, vec_env, verbose=1)
# train the model
model.learn(total_timesteps=10000)