[PYTHON] Buy and sell cryptocurrencies using Zaif API

environment

- MacOS Sierra 10.12.6
- Python 3.5.3 :: Anaconda 4.4.0 (x86_64)
- Jupyter 4.3.0

background

――The Zaif exchange is a valuable virtual currency exchange with virtual currencies that can be bought and sold in pairs with Japanese yen only here, such as NEM and Monacoin, as well as Bitcoin. --The Zaif exchange can make its website so heavy that it can't even log in, let alone trade, if the volume of transactions surges, such as when Bitcoin-related (mainly negative) news is reported.

problem

--If you cannot log in to the Zaif exchange website due to overload on the server or DDoS attack, you will not be able to buy or sell virtual currency from the website.

Solution

--The Zaif exchange publishes an API that allows you to obtain trading price information and make transactions, and even if the website is heavy, the API often survives. Therefore, by using the API, you can buy and sell virtual currencies even if you cannot log in to the website. --It is necessary to buy and sell while checking the price movement at that time. See here for how to check price movements without logging in to the website: Get cryptocurrency trading price and create chart with API of Zaif exchange / items / ed9637a383b1ed49bf5f) (The chart generated by this procedure is here)

Advance preparation

--In order to buy and sell virtual currency by API, it is necessary to obtain API key and API secret for trading in advance. This can be obtained by logging in to the Zaif exchange and going to the Account> Developer API> API keys settings screen. ** For safety reasons, it is recommended to give only the minimum required privileges to the API key ** (this time, "info" and "trade" are required). ** Also, the acquired API key and API secret must never be disclosed to others. ** **

procedure

Using ZaifAPI, simply manually buy and sell virtual currencies.

--Import zaifapi module

from zaifapi import ZaifPublicApi, ZaifTradeApi

--Buy at limit price (set limit sale)

key = "****************************" #Enter API key
secret = "****************************" #Enter API secret
zaif = ZaifTradeApi(key, secret)

price = 23 #Set the price to buy and sell (buy here for 23 yen)
amount = 1000 #Set the amount to buy and sell (buy 1000XEM here)
limit = 28 #Set a limit selling price (here, sell for 28 yen)
currency_pair = "xem_jpy" #Set a currency pair to buy and sell (here, buy NEM in Japanese Yen)
action = "bid" #Set buy or sell (buy is set here)

#Execute buying and selling
zaif.trade(currency_pair=currency_pair, action=action, price=price, amount=amount, limit=limit)

--Sell at limit price (set limit buy)

key = "****************************" #Enter API key
secret = "****************************" #Enter API secret
zaif = ZaifTradeApi(key, secret)

price = 28 #Set the price to buy and sell (here, sell for 28 yen)
amount = 1000 #Set the amount to buy and sell (here 1000XEM is sold)
limit = 23 #Set a limit selling price (buy here for 23 yen)
currency_pair = "xem_jpy" #Set a currency pair to buy and sell (here, buy NEM in Japanese Yen)
action = "ask" #Set buy or sell (set sell here)

#Execute buying and selling
zaif.trade(currency_pair=currency_pair, action=action, price=price, amount=amount, limit=limit)

Summary

――If you can buy and sell virtual currency using API, you can buy and sell even if you can not log in to the website of the exchange as long as the API is working, so you can trade without missing the time of buying and selling. can do. ** In particular, it is important to be prepared to buy and sell with the API, as the situation where the website is overloaded often occurs when the market is volatile. ** **

Recommended Posts

Buy and sell cryptocurrencies using Zaif API
zipline Function to buy and sell stocks
[Cistre] Buy and sell with moving average MACD ♬
Try using ChatWork API and Qiita API in Python
Send and receive Gmail via the Gmail API using Python
Speech transcription procedure using Python and Google Cloud Speech API
Get data using Ministry of Internal Affairs and Communications API
Predict gender from name using Gender API and Pykakasi in Python
[Go language] Collect and save Vtuber images using Twitter API
Get product name and lowest price using Amazon Product Advertising API
Get the weather using the API and let the Raspberry Pi speak!
I made a Chatbot using LINE Messaging API and Python
Aggregate and analyze product prices using Rakuten Product Search API [Python]