Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 16 additions & 143 deletions app_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import requests
import json
import datetime as dt

#st.set_page_config(layout="wide")
import pymongo
from pymongo import MongoClient

# Set ignore warning
st.set_option('deprecation.showPyplotGlobalUse', False)
Expand All @@ -25,7 +25,7 @@


#Forecast and allow to collect cache for optimized performance
@st.cache(allow_output_mutation=True)
#@st.cache(allow_output_mutation=True)

def forecast(selection):

Expand Down Expand Up @@ -64,25 +64,21 @@ def forecast(selection):

# Get database for cryptocurrency
def get_df(symbol, interval = '1m'):

# Initialize PyMongo to work with MongoDBs
conn = 'mongodb://localhost:27017'
client = pymongo.MongoClient(conn)

# Access API and reformat
root_url = 'https://api.binance.com/api/v1/klines'
url = root_url + '?symbol=' + symbol + '&interval=' + interval
data = json.loads(requests.get(url).text)
df = pd.DataFrame(data)
df.columns = ['open_time',
'o', 'h', 'l', 'c', 'v',
'close_time', 'qav', 'num_trades',
'taker_base_vol', 'taker_quote_vol', 'ignore']
df.index = [dt.datetime.fromtimestamp(x/1000.0) for x in df.close_time]
df = df.reset_index()
df = df.rename(columns={"index":"ds","open_time": "time", "o": "open", "h":"high", "l":"low", "c":"y", "v":"volume"})
#df_isolated = pd.DataFrame(df, columns = ['ds', 'y'])

#df_isolated.to_csv(r'coin_price_action.csv', mode = 'w', header = True, index=False)
#df1 = pd.read_csv("coin_price_action.csv")
#select database
db = client['crypto_db']
#select the collection within the database
df_data = db.items

return df
#convert entire collection to Pandas dataframe
df = pd.DataFrame(list(df_data.find()))
df = df.drop(columns = ['open_time','o', 'h', 'l', 'c', 'v', 'index', 'ignore', '_id', 'time'])

return df


def dfdata(selection):
Expand Down Expand Up @@ -196,129 +192,6 @@ def dfdata(selection):
Twitter_NLP_Final.wrrrdcloud()


# #Test

# base_chart = {
# "values": [40, 10, 10, 10, 10, 10, 10],
# "labels": ["-", "-1", "-0.75", "-0.25", "0.25", "0.75", "1"],
# "domain": {"x": [0, .48]},
# "marker": {
# "colors": [
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)',
# 'rgb(255, 255, 255)'
# ],
# "line": {
# "width": 1
# }
# },
# "name": "Gauge",
# "hole": .4,
# "type": "pie",
# "direction": "clockwise",
# "rotation": 108,
# "showlegend": False,
# "hoverinfo": "none",
# "textinfo": "label",
# "textposition": "outside"
# }

# meter_chart = {
# "values": [50, 10, 10, 10, 10, 10],
# "labels": ["Twitter Sentiment", "Negative", "", "Neutral", "", "Positive"],
# "marker": {
# 'colors': [
# 'rgb(255, 255, 255)',
# 'rgb(255, 84, 84)',
# 'rgb(255,141,84)',
# 'rgb(255,250,84)',
# 'rgb(216,255,84)',
# 'rgb(130,255,84)'
# ]
# },
# "domain": {"x": [0, 0.48]},
# "name": "Gauge",
# "hole": .3,
# "type": "pie",
# "direction": "clockwise",
# "rotation": 90,
# "showlegend": False,
# "textinfo": "label",
# "textposition": "inside",
# "hoverinfo": "none"
# }

# layout = {
# 'xaxis': {
# 'showticklabels': False,
# 'showgrid': False,
# 'zeroline': False,
# },
# 'yaxis': {
# 'showticklabels': False,
# 'showgrid': False,
# 'zeroline': False,
# },
# 'shapes': [
# {
# 'type': 'path',
# 'path': 'M 0.235 0.5 L 0.24 0.65 L 0.245 0.5 Z',
# 'fillcolor': 'rgba(44, 160, 101, 0.5)',
# 'line': {
# 'width': 0.75
# },
# 'xref': 'paper',
# 'yref': 'paper'
# }
# ],
# 'annotations': [
# {
# 'xref': 'paper',
# 'yref': 'paper',
# 'x': 0.23,
# 'y': 0.45,
# 'text': '100',
# 'showarrow': False
# }
# ]
# }

# # we don't want the boundary now
# base_chart['marker']['line']['width'] = 0

# gauge2 = {"data": [base_chart, meter_chart],
# "layout": layout}

# st.plotly_chart(gauge2)

# #py.iplot(fig, filename='gauge-meter-chart')


# # Average Sentiment Score
# gauge_chart = go.Figure(go.Indicator(
# mode='gauge+number+delta',
# value = Twitter_NLP_Final.scrape_sentiment_score(),
# title = {'text': 'Twitter Sentiment (-1.0 Negative to 1.0 Positive)'},
# gauge = {
# 'axis': {'range': [-1, 1]},
# 'bar': {'color':'lightgrey'},
# 'steps': [
# {'range': [-1, -0.75], 'color': 'rgb(255, 84, 84)'},
# {'range': [-0.75, -0.25], 'color': 'rgb(255,141,84)'},
# {'range': [-0.25, 0.25], 'color': 'rgb(255,250,84)'},
# {'range': [0.25, 0.75], 'color': 'rgb(216,255,84)'},
# {'range': [0.75, 1], 'color': 'rgb(130,255,84)'}
# ]
# }
# ))

# st.plotly_chart(gauge_chart)





114 changes: 66 additions & 48 deletions prophet_remake.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import streamlit as st

import pymongo
from pymongo import MongoClient

def get_bars(symbol, interval = '1m'):

# Access API and reformat
Expand All @@ -43,63 +46,78 @@ def get_bars(symbol, interval = '1m'):
m = Prophet(changepoint_prior_scale=0.01, daily_seasonality=True).fit(df_isolated)
future = m.make_future_dataframe(periods=30, freq='1min')
fcst = m.predict(future)

# Initialize PyMongo to work with MongoDBs
conn = 'mongodb://localhost:27017'
client = pymongo.MongoClient(conn)

# Create traces for interactive plotly graph
trace = go.Scatter(
name = 'Actual price',
mode = 'markers',
x = list(fcst['ds']),
y = list(df_isolated['y']),
marker=dict(
color='#FFBAD2',
line=dict(width=1)
))
# Define database and collection
# Name & create mongo database (i.e nhl_db in this case)
# Create variable for collection within your mongo database
db = client.crypto_db
collection = db.items

trace1 = go.Scatter(
name = 'predict',
mode = 'lines',
x = list(fcst['ds']),
y = list(fcst['yhat']),
marker=dict(
color='red',
line=dict(width=3)
))
#Reformat
mongo_df = df.to_dict('records')

upper_band = go.Scatter(
name = 'upper band',
mode = 'lines',
x = list(fcst['ds']),
y = list(fcst['yhat_upper']),
line= dict(color='#57b88f'),
fill = 'tonexty'
)
#Load dictionary into mongo db
collection.insert_many(mongo_df)

# Create graph
figure = plot_plotly(m, fcst)
return figure

lower_band = go.Scatter(
name= 'lower band',
mode = 'lines',
x = list(fcst['ds']),
y = list(fcst['yhat_lower']),
line= dict(color='#1705ff'))
# # Create traces for interactive plotly graph
# trace = go.Scatter(
# name = 'Actual price',
# mode = 'markers',
# x = list(fcst['ds']),
# y = list(df_isolated['y']),
# marker=dict(
# color='#FFBAD2',
# line=dict(width=1)
# ))

tracex = go.Scatter(
name = 'Actual price',
mode = 'markers',
x = list(df1['ds']),
y = list(df1['y']),
marker=dict(
color='black',
line=dict(width=2)
))
# trace1 = go.Scatter(
# name = 'predict',
# mode = 'lines',
# x = list(fcst['ds']),
# y = list(fcst['yhat']),
# marker=dict(
# color='red',
# line=dict(width=3)
# ))

data = [tracex, trace1, lower_band, upper_band, trace]
# upper_band = go.Scatter(
# name = 'upper band',
# mode = 'lines',
# x = list(fcst['ds']),
# y = list(fcst['yhat_upper']),
# line= dict(color='#57b88f'),
# fill = 'tonexty'
# )

layout = dict(title='Price Prediction Using Prophet',
xaxis=dict(title = 'Dates', ticklen=2, zeroline=True))

# lower_band = go.Scatter(
# name= 'lower band',
# mode = 'lines',
# x = list(fcst['ds']),
# y = list(fcst['yhat_lower']),
# line= dict(color='#1705ff'))

figure = plot_plotly(m, fcst)
# tracex = go.Scatter(
# name = 'Actual price',
# mode = 'markers',
# x = list(df1['ds']),
# y = list(df1['y']),
# marker=dict(
# color='black',
# line=dict(width=2)
# ))

return figure
# data = [tracex, trace1, lower_band, upper_band, trace]

# layout = dict(title='Price Prediction Using Prophet',
# xaxis=dict(title = 'Dates', ticklen=2, zeroline=True))

#figure=dict(data=data,layout=layout)
#plt.savefig('btc03.png')
Expand Down