Created
December 12, 2017 14:58
-
-
Save ryosism/0816c372fffae9146a186c84fef5f8dc to your computer and use it in GitHub Desktop.
仮想通貨取引所poloniexを使ったトレードシミュレーター(12月10日作成)
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
from time import sleep | |
from datetime import datetime, date, timezone, timedelta | |
import time | |
import poloniex | |
import pandas as pd | |
polo = poloniex.Poloniex() | |
above15minute = 0 | |
above10minute = 0 | |
above5minute = 0 | |
down = '\033[91m' | |
up = '\033[32m' | |
ENDC = '\033[0m' | |
# 5分足のデータを取得、0.005にすることで5分足のデータの一番最新だけを取得できる | |
# 最初は3個前のデータから必要なため、0.015に設定 | |
chart = polo.returnChartData('USDT_BTC', period=300, start=time.time()-polo.DAY*0.015, end=time.time()) | |
data = pd.DataFrame(chart) | |
prev = float(data.close[2]) | |
prevprev = float(data.close[1]) | |
prevprevprev = float(data.close[0]) | |
class Bot: | |
status = 0 | |
hold = 0 | |
result = 0 | |
bought_length = 0 | |
# 変数一覧------------------------------- | |
# status = 現在の状況 | |
# 0 : 購入なし(購入可能) | |
# 2 : 購入済み(売却可能) | |
# 初期値は0からスタート | |
# hold : 購入した値段 | |
# buy()を行う際に現在のレートが代入され、sell()を行う際に値が0になる | |
# 初期値は0からスタート | |
# result = 利益 | |
# 買値と売値の差が、売却のたびに更新される | |
# bought_length = 購入してからの経過時間 | |
# 購入してからの経過時間をカウント | |
# ------------------------------------- | |
bot = Bot() | |
def rate(now, prev): | |
if now != 0 and prev != 0: | |
return round(now/prev * 100 - 100, 4) | |
else: | |
return 'error (zero divide.)' | |
def buy(status, now): | |
if status == 0: | |
bot.hold = now | |
bot.status = 2 | |
print('------------購入--------------') | |
print('価格 : {0}'.format(now)) | |
print('-----------------------------') | |
else: | |
print('すでに購入済み') | |
def sell(status, now): | |
if status == 2: | |
bot.result += now - bot.hold | |
print('result = {0}'.format(bot.result)) | |
bot.hold = 0 | |
bot.status = 0 | |
print('------------売却--------------') | |
print('価格 : {0}'.format(now)) | |
print('-----------------------------') | |
else: | |
print('購入していない') | |
def status_check(prevprevprev, prevprev, prev, now): | |
if bot.status == 2: | |
bot.bought_length += 1 | |
# 購入直後に2回連続で下落したら即売却 | |
if 1 > bot.bought_length > 5: | |
if prevprev > prev > now: | |
sell(bot.status, now) | |
if prevprevprev < prevprev < prev == now: | |
print('上昇トレンド停止') | |
sell(bot.status, now) | |
elif prevprevprev > prevprev > prev == now: | |
print('下降トレンド停止') | |
buy(bot.status, now) | |
else: | |
print('トレンド変化なし') | |
while 1: | |
now = datetime.now() | |
if now.second == 0: | |
print('------------------------------------------------------------') | |
# 5分足のデータを取得、0.005にすることで5分足のデータの一番最新だけを取得できる | |
chart = polo.returnChartData('USDT_BTC', period=300, start=time.time()-polo.DAY*0.005, end=time.time()) | |
data = pd.DataFrame(chart) | |
print(str(now.year) + '/' + str(now.month) + '/' + str(now.day) + '/ ' + str(now.hour) + ':' + str(now.minute) + ':' + str(now.second)) | |
now = float(data.close[0]) | |
status_check(prevprevprev, prevprev, prev, now) | |
if now > prev: | |
print ('BTC/USDT = {0}{1} (+{2}%){3} 利益 : {4} 購入状態 : {5}'.format(up, now, rate(now, prev), ENDC, bot.result, bot.status)) | |
elif now < prev: | |
print ('BTC/USDT = {0}{1} ({2}%){3} 利益 : {4} 購入状態 : {5}'.format(down, now, rate(now, prev), ENDC, bot.result, bot.status)) | |
else: | |
print ('BTC/USDT = {0}{1}{2} 利益 : {3} 購入状態 : {4}'.format(ENDC, now, ENDC, bot.result, bot.status)) | |
# 一番簡単な値の更新 | |
prevprevprev = prevprev | |
prevprev = prev | |
prev = now | |
# タイムアウトの解消(これで解決するかわからないけど・・・) | |
if prevprevprev == 0 and prevprev == 0 and prev == 0 and now == 0: | |
sleep(300) | |
# BANされないように一応 | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment