Created
September 20, 2016 08:39
-
-
Save anonymous/6ff959e0cf31dd5f900be9a1937a3562 to your computer and use it in GitHub Desktop.
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 hashlib | |
import hmac | |
import json | |
import requests | |
import time | |
import urllib | |
import websocket | |
# ZaifのApiキーを入れる | |
key = '' | |
secret = '' | |
# 取引Apiを利用する下準備 | |
trade_url = 'https://api.zaif.jp/tapi' | |
nonce = time.time() | |
method = 'trade' | |
# websocketの下準備 | |
ws_url = 'ws://api.zaif.jp:8888/stream?currency_pair=btc_jpy' | |
ws_conn = websocket.create_connection(ws_url) | |
# 売り板の先頭価格を取得 | |
# ask_top_rateが今回購入する価格 | |
res = ws_conn.recv() | |
ask_top_rate = json.loads(res)['asks'][0][0] | |
# btc_jpyで0.0001btcをbid(購入)するよう取引Apiで送信するデータの挿入 | |
payload = {'nonce': str(nonce), 'method': method, 'currency_pair': 'btc_jpy', | |
'action': 'bid', 'price': int(ask_top_rate), 'amount': 0.0001} | |
headers = {'key': key, 'sign': hmac.new(secret, | |
urllib.urlencode(payload), | |
hashlib.sha512).hexdigest()} | |
# 取引Apiを操作して購入注文を出す | |
requests.post(trade_url, data=urllib.urlencode(payload), headers=headers, timeout=3).json() | |
# 画面に何も表示がないと不安なので定期的に画面に何かを表示するカウント | |
count = 0 | |
# 無限ループスタート | |
while True: | |
# 買い板の先頭価格を取得 | |
res = ws_conn.recv() | |
bid_top_rate = json.loads(res)['bids'][0][0] | |
# もしask_top_rate(購入価格)より買い板の先頭価格が高ければ | |
if ask_top_rate < bid_top_rate: | |
# btc_jpyで0.0001btcをask(売却)するよう取引Apiで送信するデータの挿入 | |
nonce += 0.01 | |
payload = {'nonce': str(nonce), 'method': method, 'currency_pair': 'btc_jpy', | |
'action': 'ask', 'price': int(bid_top_rate), 'amount': 0.0001} | |
headers = {'key': key, 'sign': hmac.new(secret, | |
urllib.urlencode(payload), | |
hashlib.sha512).hexdigest()} | |
# 取引Apiを操作して売却注文を出す | |
requests.post(trade_url, data=urllib.urlencode(payload), headers=headers, timeout=3).json() | |
# 売却が終わったらループを終了する | |
break | |
# ループを一回転するとにカウント1増やす | |
count = count + 1 | |
# countが10を超えたら | |
if count > 10: | |
# 情報を表示 | |
print '購入した時の価格' | |
print ask_top_rate | |
print '現在の買い板先頭価格' | |
print bid_top_rate | |
# countを0に戻す | |
count = 0 | |
# ループへ戻る |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment