File size: 5,223 Bytes
68a5fc3 | 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 | import pandas as pd
import numpy as np
import time
import requests
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense,Dropout,Bidirectional
from keras.layers import LSTM
data=requests.get('https://api.twelvedata.com/time_series?symbol=BTC/INR&timezone=Asia/Kolkata&start_date=2023-03-01 00:00:00&end_date=2023-03-20 00:00:00&order=ASC&interval=5min&outputsize=5000&apikey=e76157c75c3a42649e168c5c206e88ca').json()
#print(data)
data_final=pd.DataFrame(data['values'])
#print(data_final)
scaler=MinMaxScaler(feature_range=(0,1))
scaled_data=scaler.fit_transform(data_final['close'].values.reshape(-1,1))
timeinterval=24
prediction=1
x_train=[]
y_train=[]
for i in range(timeinterval,len(scaled_data)-prediction):
x_train.append(scaled_data[i-timeinterval:i,0])
y_train.append(scaled_data[i+prediction,0])
x_train=np.array(x_train)
y_train=np.array(y_train)
x_train=np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
model=Sequential()
model.add(Bidirectional(LSTM(256,return_sequences=True,input_shape=(x_train.shape[1],1),activation='relu')))
model.add(Dropout(0.3))
model.add(Bidirectional(LSTM(128,return_sequences=True,activation='tanh')))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(64,activation='relu')))
model.add(Dropout(0.1))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['accuracy'])
model.fit(x_train,y_train,epochs=15,batch_size=100)
predict=[]
exchange=[]
i=0
while(i<6):
testapi='https://api.twelvedata.com/time_series?symbol=BTC/INR&interval=5min&outputsize=3000&timezone=Asia/Kolkata&order=ASC&apikey=e76157c75c3a42649e168c5c206e88ca'
testdata=requests.get(testapi).json()
testdatafinal=pd.DataFrame(testdata['values'])
bitcoinprice=pd.to_numeric(testdatafinal['close'],errors='coerce').values
testinputs=testdatafinal['close'].values
testinputs=testinputs.reshape(-1,1)
modelinputs=scaler.fit_transform(testinputs)
x_test=[]
for x in range(timeinterval,len(modelinputs)):
x_test.append(modelinputs[x-timeinterval:x,0])
x_test=np.array(x_test)
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
prediction_price=model.predict(x_test)
prediction_price=scaler.inverse_transform(prediction_price)
"""plt.plot(bitcoinprice,label='Bitcoin Prices')
plt.plot(prediction_price,label='Predicted Prices')
plt.title('Predicting Bitcoin Price')
plt.xlabel('5min Interval')
plt.ylabel('Price')
plt.legend()
plt.show()"""
exchangeapi='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e88ca'
exchangedata=requests.get(exchangeapi).json()
print(exchangedata)
exchangefinal=exchangedata['rate']
exchange.append(exchangefinal)
lastdata=modelinputs[len(modelinputs)+1-timeinterval:len(modelinputs)+1,0]
lastdata=np.array(lastdata)
lastdata=np.reshape(lastdata,(1,lastdata.shape[0],1))
prediction=model.predict(lastdata)
prediction=scaler.inverse_transform(prediction)
predict.append(prediction[0][0])
print(prediction)
i+=1
print(i)
time.sleep(300)
"""testapi='https://api.twelvedata.com/time_series?symbol=BTC/INR&interval=5min&outputsize=3000&apikey=e76157c75c3a42649e168c5c206e88ca'
testdata=requests.get(testapi).json()
testdatafinal=pd.DataFrame(testdata['values'])
bitcoinprice=pd.to_numeric(testdatafinal['close'],errors='coerce').values
testinputs=testdatafinal['close'].values
testinputs=testinputs.reshape(-1,1)
modelinputs=scaler.fit_transform(testinputs)
x_test=[]
for x in range(timeinterval,len(modelinputs)):
x_test.append(modelinputs[x-timeinterval:x,0])
x_test=np.array(x_test)
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
prediction_price=model.predict(x_test)
prediction_price=scaler.inverse_transform(prediction_price)
plt.plot(bitcoinprice,label='Bitcoin Prices')
plt.plot(prediction_price,label='Predicted Prices')
plt.title('Predicting Bitcoin Price')
plt.xlabel('5min Interval')
plt.ylabel('Price')
plt.legend()
plt.show()
predict=[]
exchange=[]
exchangeapi='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e8'
exchangedata=requests.get(exchangeapi).json()
exchangefinal=pd.DataFrame(exchangedata['rate'])
exchange.append(exchangefinal)
lastdata=modelinputs[len(modelinputs)+1-timeinterval:len(modelinputs)+1,0]
lastdata=np.array(lastdata)
lastdata=np.reshape(lastdata,(1,lastdata.shape[0],1))
prediction=model.predict(lastdata)
prediction=scaler.inverse_transform(prediction)
predict.append(prediction)
print(prediction)"""
exchange1api='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e88ca'
exchange1data=requests.get(exchange1api).json()
exchange1final=exchange1data['rate']
exchange.append(exchange1final)
exchange.pop(0)
print(exchange)
print(predict)
plt.plot(exchange,label='Bitcoin Price')
plt.plot(predict,label='Predicted Prices')
plt.title('Predicting Bitcoin Price')
plt.xlabel('5min Interval')
plt.ylabel('Price')
plt.legend()
plt.show() |