Last active
December 7, 2016 11:02
-
-
Save darden1/accc2ab27ecc9f40ac6ac6479eb0998c to your computer and use it in GitHub Desktop.
PredictionBTCPriceWithDecisionTree.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import poloniex | |
import time | |
import datetime | |
from sklearn import tree | |
def main(): | |
# --トレーニング用パラメータ | |
theNumberOfTrainData=29 #トレーニングデータ数 | |
theNumberOfTrainAndKyoushiSet=30 #トレーニングデータと教師データのセット数 | |
studyTrialTimes = 200 #予測のトライアル数(予測結果がばらつくので1回の予測結果を出すために実施する試行回数(取りあえずこのままにしておいてください。) | |
print('-----------------------------------------') | |
print('BTC価格データダウンロード') | |
print('-----------------------------------------') | |
dateBTC, dataBTC = getDataPoloniex() | |
dateBTC.reverse() | |
dataBTC.reverse() | |
todayStr = str(datetime.datetime.today())[0:10] | |
print('今日は' + todayStr + 'です。PoloniexからBTC価格データをダウンロードします。') | |
print('取得データファイル中にある最新の日付は' + str(dateBTC[0])[0:10] + 'でその時のBTCオープン価格は$' + str(dataBTC[0]) + 'です。') | |
data = changeData(dataBTC) | |
print('-----------------------------------------') | |
print('明日のBTC価格予想') | |
print('-----------------------------------------') | |
print(str(dateBTC[0])[0:10] + 'までのBTCオープン価格データから' + str(dateBTC[0] + datetime.timedelta(days=1))[0:10] + 'のBTCオープン価格を予測します。') | |
print('・トレーニングデータ数: ' + str(theNumberOfTrainData)) | |
print('・トレーニングデータと教師データのセット数: ' + str(theNumberOfTrainAndKyoushiSet)) | |
upOrDownRatio=predictionTommorowBTC(data, theNumberOfTrainData, theNumberOfTrainAndKyoushiSet, studyTrialTimes) | |
if upOrDownRatio > 0.5: | |
print('\n明日のBTC価格は'+ str(round(upOrDownRatio*100,1)) +'%の確率で上昇します。') | |
else: | |
print('\n明日のBTC価格は' + str(round((1-upOrDownRatio) * 100,1)) + '%の確率で下落します。') | |
def getDataPoloniex(): | |
polo = poloniex.Poloniex() | |
polo.timeout = 2 | |
chartUSDT_BTC = polo.returnChartData('USDT_BTC', period=polo.DAY, start=time.time() - polo.DAY * 500, end=time.time()) | |
tmpDate = [chartUSDT_BTC[i]['date'] for i in range(len(chartUSDT_BTC))] | |
date = [datetime.datetime.fromtimestamp(tmpDate[i]).date() for i in range(len(tmpDate))] | |
data = [float(chartUSDT_BTC[i]['open']) for i in range(len(chartUSDT_BTC))] | |
return date ,data | |
def changeData(data): | |
newData=[] | |
for i in range(0, len(data) - 1): | |
newData.append(float(data[i] - data[i + 1]) / data[i + 1]) | |
newData.append(0) | |
return newData | |
def preparationTrainAndKyoushiSets(data,trainStartDay,theNumberOfTrainData,theNumberOfTrainAndKyoushiSet): | |
train_X = [] | |
train_y = [] | |
for i in range(0,theNumberOfTrainAndKyoushiSet): | |
train_X.append(data[trainStartDay+1+i:trainStartDay+theNumberOfTrainData+1+i]) | |
train_y.append([int(0 < data[trainStartDay+i])]) | |
return train_X, train_y | |
def predictionTommorowBTC(data,theNumberOfTrainData,theNumberOfTrainAndKyoushiSet, studyTrialTimes): | |
trainStartDay=0 | |
train_X, train_y = preparationTrainAndKyoushiSets(data,trainStartDay,theNumberOfTrainData,theNumberOfTrainAndKyoushiSet) | |
X=data[trainStartDay:trainStartDay+theNumberOfTrainData] | |
y_PredictionTommorow=[] | |
for i in range(0, studyTrialTimes): | |
clf = tree.DecisionTreeClassifier() | |
clf.fit(train_X, train_y) | |
y_PredictionTommorow.append(clf.predict([X])[0]) | |
upOrDownRatio = sum(y_PredictionTommorow) * 1.0 / len(y_PredictionTommorow) | |
return upOrDownRatio | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment