-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeCasting.py
99 lines (79 loc) · 2.98 KB
/
ForeCasting.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
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
from datetime import datetime
import config, csv
from binance.client import Client
import asyncio
from binance import AsyncClient, DepthCacheManager, BinanceSocketManager
from matplotlib import pyplot as plt
import pprint,json,numpy
from flask import jsonify
import pandas as pd
import datetime
from fbprophet import Prophet
from fbprophet.plot import plot, plot_plotly, plot_components_plotly
#create your own file name config and define your API_KEY and API_SECRET
client = Client(config.API_KEY, config.API_SECRET)
# prices = client.get_all_tickers()
# for price in prices:
# print(price)
#csvfile = open('2020_15minutes.csv', 'w', newline='')
#candlestick_writer = csv.writer(csvfile, delimiter=',')
candlesticks = client.get_historical_klines("BTCUSDT", AsyncClient.KLINE_INTERVAL_1MINUTE, "10 day ago UTC")
processed_closed_candlesticks = []
processed_time_candlesticks = []
for data in candlesticks:
candlestick = {
"time": data[0] /1000,
"open": data[1],
"high": data[2],
"low": data[3],
"close": data[4]
}
processed_closed_candlesticks.append(candlestick["close"])
processed_time_candlesticks.append(candlestick["time"])
print(processed_closed_candlesticks)
print(processed_time_candlesticks)
timestamps = []
for i in processed_time_candlesticks:
timestamp = datetime.datetime.fromtimestamp(i)
timestamps.append(timestamp)
timestamp_cleaned = []
for i in timestamps:
timestamp_clean = i.strftime('%Y-%m-%d %H:%M:%S')
timestamp_cleaned.append(timestamp_clean)
dataCom = list(zip(pd.DatetimeIndex(timestamp_cleaned),processed_closed_candlesticks))
df = pd.DataFrame(data=dataCom,columns=["ds","y"])
print( df.head())
#Training Model
m = Prophet(interval_width=0.95,daily_seasonality=True)
model = m.fit(df)
#making predictions
future = m.make_future_dataframe(periods=30,freq='min',include_history=False)
forecast = m.predict(future)
print(len(forecast['yhat']))
predicted_ds_y = numpy.array(forecast['yhat'][0]) - numpy.array(forecast['yhat'][len(forecast['yhat'])-1])
if predicted_ds_y < 0 :
predicted_direction = "down!!!!"
print(predicted_direction)
else:
predicted_direction ="up!!!!"
print(predicted_direction)
predicted_values = []
predicted_values = numpy.array(forecast['yhat'][-5:])
predicted_dates = []
predicted_dates = forecast['ds']
print(forecast[['ds','yhat']])
print(predicted_values)
print(predicted_dates)
from matplotlib import pyplot as plt
fig1 = m.plot(forecast)
fig2 = m.plot_components(forecast)
plt.show()
#validation
from fbprophet.diagnostics import cross_validation
from fbprophet.diagnostics import performance_metrics
#m_validate = cross_validation(m,horizon='365 days',period='100 day',initial='200 days')
#print(m_validate.head())
#m_performance = performance_metrics(m_validate)
#print(m_performance.head())
#print(forecast.head())
#print(forecast.tail())