forked from itamariuser/Stockex_Model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_Graph.py
66 lines (47 loc) · 1.66 KB
/
Create_Graph.py
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
import plotly as py
import pandas as pd
import numpy as np
import LSTM
from datetime import datetime
from datetime import time as dt_tm
from datetime import date as dt_date
import plotly.plotly as py
import plotly.tools as plotly_tools
import plotly.graph_objs as go
import os
import tempfile
os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
from matplotlib.finance import quotes_historical_yahoo
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
from IPython.display import HTML
# ********************* Importing AAPL INFO
x = []
y = []
ma = []
def moving_average(interval, window_size):
window = np.ones(int(window_size))/float(window_size)
return np.convolve(interval, window, 'same')
date1 = dt_date( 2017, 1, 1 )
date2 = dt_date(2017 , 12, 12 )
quotes = quotes_historical_yahoo('AAPL', date1, date2)
if len(quotes) == 0:
print ("Couldn't connect to yahoo trading database")
else:
dates = [q[0] for q in quotes]
y = [q[1] for q in quotes]
for date in dates:
x.append(datetime.fromordinal(int(date))\
.strftime('%Y-%m-%d')) # Plotly timestamp format
ma = moving_average(y, 10)
# ************************* END importing AAPL INFO
# Graph Creation
xy_data = go.Scatter( x=x, y=y, mode='markers', marker=dict(size=4), name='AAPL' )
# vvv clip first and last points of convolution
mov_avg = go.Scatter( x=x[5:-4], y=ma[5:-4], \
line=dict(width=2,color='red',opacity=0.5), name='Moving average' )
data = [xy_data, mov_avg]
py.iplot(data, filename='apple stock moving average')
# Graph URL
first_plot_url = py.plot(data, filename='apple stock moving average', auto_open=False,)
print (first_plot_url)