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
import requests
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 8 |
+
|
| 9 |
+
import tensorflow as tf
|
| 10 |
+
from keras.models import Sequential
|
| 11 |
+
from keras.layers import Dense,Dropout,Bidirectional
|
| 12 |
+
from keras.layers import LSTM
|
| 13 |
+
|
| 14 |
+
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()
|
| 15 |
+
#print(data)
|
| 16 |
+
data_final=pd.DataFrame(data['values'])
|
| 17 |
+
#print(data_final)
|
| 18 |
+
scaler=MinMaxScaler(feature_range=(0,1))
|
| 19 |
+
scaled_data=scaler.fit_transform(data_final['close'].values.reshape(-1,1))
|
| 20 |
+
timeinterval=24
|
| 21 |
+
prediction=1
|
| 22 |
+
|
| 23 |
+
x_train=[]
|
| 24 |
+
y_train=[]
|
| 25 |
+
|
| 26 |
+
for i in range(timeinterval,len(scaled_data)-prediction):
|
| 27 |
+
x_train.append(scaled_data[i-timeinterval:i,0])
|
| 28 |
+
y_train.append(scaled_data[i+prediction,0])
|
| 29 |
+
|
| 30 |
+
x_train=np.array(x_train)
|
| 31 |
+
y_train=np.array(y_train)
|
| 32 |
+
x_train=np.reshape(x_train,(x_train.shape[0],x_train.shape[1],1))
|
| 33 |
+
|
| 34 |
+
model=Sequential()
|
| 35 |
+
|
| 36 |
+
model.add(Bidirectional(LSTM(256,return_sequences=True,input_shape=(x_train.shape[1],1),activation='relu')))
|
| 37 |
+
model.add(Dropout(0.3))
|
| 38 |
+
model.add(Bidirectional(LSTM(128,return_sequences=True,activation='tanh')))
|
| 39 |
+
model.add(Dropout(0.2))
|
| 40 |
+
model.add(Bidirectional(LSTM(64,activation='relu')))
|
| 41 |
+
model.add(Dropout(0.1))
|
| 42 |
+
model.add(Dense(1,activation='sigmoid'))
|
| 43 |
+
model.compile(loss='mean_squared_error',optimizer='adam',metrics=['accuracy'])
|
| 44 |
+
model.fit(x_train,y_train,epochs=15,batch_size=100)
|
| 45 |
+
predict=[]
|
| 46 |
+
exchange=[]
|
| 47 |
+
|
| 48 |
+
i=0
|
| 49 |
+
while(i<6):
|
| 50 |
+
testapi='https://api.twelvedata.com/time_series?symbol=BTC/INR&interval=5min&outputsize=3000&timezone=Asia/Kolkata&order=ASC&apikey=e76157c75c3a42649e168c5c206e88ca'
|
| 51 |
+
testdata=requests.get(testapi).json()
|
| 52 |
+
testdatafinal=pd.DataFrame(testdata['values'])
|
| 53 |
+
|
| 54 |
+
bitcoinprice=pd.to_numeric(testdatafinal['close'],errors='coerce').values
|
| 55 |
+
testinputs=testdatafinal['close'].values
|
| 56 |
+
testinputs=testinputs.reshape(-1,1)
|
| 57 |
+
modelinputs=scaler.fit_transform(testinputs)
|
| 58 |
+
|
| 59 |
+
x_test=[]
|
| 60 |
+
for x in range(timeinterval,len(modelinputs)):
|
| 61 |
+
x_test.append(modelinputs[x-timeinterval:x,0])
|
| 62 |
+
|
| 63 |
+
x_test=np.array(x_test)
|
| 64 |
+
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
|
| 65 |
+
prediction_price=model.predict(x_test)
|
| 66 |
+
prediction_price=scaler.inverse_transform(prediction_price)
|
| 67 |
+
|
| 68 |
+
"""plt.plot(bitcoinprice,label='Bitcoin Prices')
|
| 69 |
+
plt.plot(prediction_price,label='Predicted Prices')
|
| 70 |
+
plt.title('Predicting Bitcoin Price')
|
| 71 |
+
plt.xlabel('5min Interval')
|
| 72 |
+
plt.ylabel('Price')
|
| 73 |
+
plt.legend()
|
| 74 |
+
plt.show()"""
|
| 75 |
+
exchangeapi='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e88ca'
|
| 76 |
+
exchangedata=requests.get(exchangeapi).json()
|
| 77 |
+
print(exchangedata)
|
| 78 |
+
|
| 79 |
+
exchangefinal=exchangedata['rate']
|
| 80 |
+
exchange.append(exchangefinal)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
lastdata=modelinputs[len(modelinputs)+1-timeinterval:len(modelinputs)+1,0]
|
| 84 |
+
lastdata=np.array(lastdata)
|
| 85 |
+
lastdata=np.reshape(lastdata,(1,lastdata.shape[0],1))
|
| 86 |
+
prediction=model.predict(lastdata)
|
| 87 |
+
prediction=scaler.inverse_transform(prediction)
|
| 88 |
+
predict.append(prediction[0][0])
|
| 89 |
+
print(prediction)
|
| 90 |
+
i+=1
|
| 91 |
+
print(i)
|
| 92 |
+
time.sleep(300)
|
| 93 |
+
|
| 94 |
+
"""testapi='https://api.twelvedata.com/time_series?symbol=BTC/INR&interval=5min&outputsize=3000&apikey=e76157c75c3a42649e168c5c206e88ca'
|
| 95 |
+
testdata=requests.get(testapi).json()
|
| 96 |
+
testdatafinal=pd.DataFrame(testdata['values'])
|
| 97 |
+
|
| 98 |
+
bitcoinprice=pd.to_numeric(testdatafinal['close'],errors='coerce').values
|
| 99 |
+
testinputs=testdatafinal['close'].values
|
| 100 |
+
testinputs=testinputs.reshape(-1,1)
|
| 101 |
+
modelinputs=scaler.fit_transform(testinputs)
|
| 102 |
+
|
| 103 |
+
x_test=[]
|
| 104 |
+
for x in range(timeinterval,len(modelinputs)):
|
| 105 |
+
x_test.append(modelinputs[x-timeinterval:x,0])
|
| 106 |
+
|
| 107 |
+
x_test=np.array(x_test)
|
| 108 |
+
x_test=np.reshape(x_test,(x_test.shape[0],x_test.shape[1],1))
|
| 109 |
+
prediction_price=model.predict(x_test)
|
| 110 |
+
prediction_price=scaler.inverse_transform(prediction_price)
|
| 111 |
+
|
| 112 |
+
plt.plot(bitcoinprice,label='Bitcoin Prices')
|
| 113 |
+
plt.plot(prediction_price,label='Predicted Prices')
|
| 114 |
+
plt.title('Predicting Bitcoin Price')
|
| 115 |
+
plt.xlabel('5min Interval')
|
| 116 |
+
plt.ylabel('Price')
|
| 117 |
+
plt.legend()
|
| 118 |
+
plt.show()
|
| 119 |
+
predict=[]
|
| 120 |
+
exchange=[]
|
| 121 |
+
exchangeapi='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e8'
|
| 122 |
+
exchangedata=requests.get(exchangeapi).json()
|
| 123 |
+
exchangefinal=pd.DataFrame(exchangedata['rate'])
|
| 124 |
+
exchange.append(exchangefinal)
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
lastdata=modelinputs[len(modelinputs)+1-timeinterval:len(modelinputs)+1,0]
|
| 130 |
+
lastdata=np.array(lastdata)
|
| 131 |
+
lastdata=np.reshape(lastdata,(1,lastdata.shape[0],1))
|
| 132 |
+
prediction=model.predict(lastdata)
|
| 133 |
+
prediction=scaler.inverse_transform(prediction)
|
| 134 |
+
predict.append(prediction)
|
| 135 |
+
print(prediction)"""
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
exchange1api='https://api.twelvedata.com/exchange_rate?symbol=BTC/INR&timezone=Asia/Kolkata&apikey=e76157c75c3a42649e168c5c206e88ca'
|
| 140 |
+
exchange1data=requests.get(exchange1api).json()
|
| 141 |
+
exchange1final=exchange1data['rate']
|
| 142 |
+
exchange.append(exchange1final)
|
| 143 |
+
exchange.pop(0)
|
| 144 |
+
print(exchange)
|
| 145 |
+
print(predict)
|
| 146 |
+
|
| 147 |
+
plt.plot(exchange,label='Bitcoin Price')
|
| 148 |
+
plt.plot(predict,label='Predicted Prices')
|
| 149 |
+
plt.title('Predicting Bitcoin Price')
|
| 150 |
+
plt.xlabel('5min Interval')
|
| 151 |
+
plt.ylabel('Price')
|
| 152 |
+
plt.legend()
|
| 153 |
+
plt.show()
|