-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPM
52 lines (41 loc) · 1.54 KB
/
PPM
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
import json
import requests
import pandas as pd
import datetime as dt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
# get bars of symbol in interval (1m/1h/1d), limit its the number of bars (1500 max by the site)
def get_bars(symbol="BTCUSDT", interval="1h", limit="1500"):
url = 'https://fapi.binance.com/fapi/v1/klines' + '?symbol=' + symbol + '&interval=' + interval + '&limit=' + limit
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]
return df
avg = 0
df = get_bars()
# take only the close price
df = df[['c']].astype('float')
# number of future prices predict
forecast_out = 1
# prepar the data
df['prediction'] = df[['c']].shift(-forecast_out)
x = np.array(df.drop(['prediction'], 1))
x = x[:-forecast_out]
y = np.array(df['prediction'])
y = y[:-forecast_out]
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.5)
# build and train LR model
lr = LinearRegression()
lr.fit(x_train, y_train)
# lr score himself by the test
lr_con = lr.score(x_test, y_test)
print("lr score:", round(lr_con * 100, 2), "%")
# predict next price
x_forecast = np.array(df.drop(['prediction'], 1))[-forecast_out:]
lr_pred = lr.predict(x_forecast)
print("next price:", round(lr_pred[0]))