Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 2 [Transaction with API]

Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! It is a continuation of Part 1.

This time, I would like to move the API ** that requires authentication information and actually trade ✧ + (0 ° ・ ∀ ・) + wktk✧ I will verify the movement immediately.

Verification

API that requires authentication information: Let's run ** ZaifPrivateApi **. This time, we verified information acquisition (get_info ()) and transaction (trade ()).

0. Preparation

This API requires two keys, key and secret, so create it with ** zaif **.

  1. Register a new user in zaif and log in

  2. Select the account in the upper right Capture.PNG

  3. Select API KEY of API for developers Capture 5.PNG

  4. Select get Verification Code Capture 2.PNG

  5. A 6-digit code will be sent to the registered email address, so enter it. Capture 3.PNG

  6. Enter the name of the Key and select the authority to associate with the key (link info, trade) Capture 4.PNG

  7. Press create to generate the key

  8. Make a copy of the Keys and secret of Keys and make a note of them.

1. 1. Information acquisition

** 0. Get transaction information using the Key and secret created in Preparation **.

main.py


# -*- coding: utf-8 -*-

from zaifapi import ZaifPrivateApi  #Class that executes API that requires authentication information published by Zaif
from pprint import pprint  #For display(It displays json nicely)

if __name__ == '__main__':
    key = '[Key created in preparation]'
    secret = '[Secret created in preparation]'

    zaif = ZaifPrivateApi(key, secret)
    pprint(zaif.get_info())

■ Execution result

python


{u'deposit': {u'btc': 0.0, u'jpy': 0.0, u'mona': 0.0, u'xem': 0.0},
 u'funds': {u'btc': 0.0, u'jpy': 0.0, u'mona': 0.0, u'xem': 0.0},
 u'open_orders': 0,
 u'rights': {u'info': 1, u'personal_info': 0, u'trade': 0, u'withdraw': 0},
 u'server_time': 1491068366,
 u'trade_count': 0}

I got the deposit, funds, order quantity (open_orders), authority information (rights), and trade count (trade_count). It seems that a lightweight version of get_info2 () that does not get the number of trades is also available.

2. Trade

Now that the warm-up is over, it's today's main dish "** Trading **".

I will put about 1,000 yen for the transaction test. There was no credit card payment in the "Japanese Yen Deposit / Withdrawal" menu, so Buy Bitcoin directly from Buy Bitcoin with Credit Card. キャプチャ6.PNG

Enter the required information and purchase. I bought it safely! ヾ (o´∀`o) ノ キャプチャ7.PNG

**···Hmm? ** ** キャプチャ8.PNG It seems that about 7.6% was taken as a commission ... (´ ・ ω ・ `)

Since it is the lowest purchase price, the commission is probably relatively high ... Regain your mind and go to the verification of trade (`・ ω ・ ´)

2-1. Trade (sale of Bitcoin)

Document says that you should pass currency_pair, action, price, and amount. キャプチャ10.PNG

Set each value in the correct order. Sell all your Bitcoin (0.0076btc). For the sale price, specify the current Bitcoin price of ¥ 121,800. キャプチャ9.PNG ... I'm angry. If the argument is wrong.

python


TypeError: trade() takes exactly 1 argument (5 given)

When I check the error on the console, it is said that I am passing 5 even though only 1 is entered. (The document says that 4 parameters are required. I'm passing 4 parameters ...)

When I went around, the ancestor solved it. (Thank you. It was very helpful.) Apparently, the way to specify the parameters was wrong. キャプチャ11.PNG

main.py


# -*- coding: utf-8 -*-

from zaifapi import ZaifPrivateApi  #Class that executes API that requires authentication information published by Zaif
from pprint import pprint  #For display(It displays json nicely)

if __name__ == '__main__':
    key = '[Key created in preparation]'
    secret = '[Secret created in preparation]'

    zaif = ZaifPrivateApi(key, secret)
    #Sell Bitcoin
    pprint(zaif.trade(currency_pair="btc_jpy", action="ask", price=121800, amount=0.0076))

■ Execution result

python


{u'funds': {u'btc': 0.0, u'jpy': 925.946, u'mona': 0.0, u'xem': 0.0},
 u'order_id': 0,
 u'received': 925.946,
 u'remains': 0.0}

It moved ~ ヾ (゚ ω ゚ *) ノ It is successful because btc becomes 0 and jpy increases.

2-2. Trade (buying Bitcoin)

Now that we have sold Bitcoin, we will try to buy Bitcoin in the same way. I will buy as much Bitcoin as I can with the Japanese Yen (¥ 925.946) I have. The purchase amount is calculated by the amount of money you have (¥ 925) ÷ the current price of 1bitcoin (¥ 122,210).

main.py


# -*- coding: utf-8 -*-

from zaifapi import ZaifPrivateApi  #Class that executes API that requires authentication information published by Zaif
from pprint import pprint  #For display(It displays json nicely)

if __name__ == '__main__':
    key = '[Key created in preparation]'
    secret = '[Secret created in preparation]'

    zaif = ZaifPrivateApi(key, secret)

    #Current price of 1 Bitcoin
    price = 122210

    #Since the API supports up to 4 digits after the decimal point, round()
    #If the 5th decimal place is moved up, there will be a shortage of assets.(- 0.0001)
    amount = round(925.0/price, 4) - 0.0001

    #Buy Bitcoin
    pprint(zaif.trade(currency_pair="btc_jpy", action="bid", price=price, amount=amount))

■ Execution result

python


{u'funds': {u'btc': 0.0075, u'jpy': 9.4085, u'mona': 0.0, u'xem': 0.0},
 u'order_id': 0,
 u'received': 0.0075,
 u'remains': 0.0}

I bought 0.0075 Bitcoin ヾ (゚ ω ゚ *) ノ Japanese yen remains ¥ 9.4805, but it seems that there is no help for the specifications below the minimum unit of 0.0001 Bitcoin (¥ 12.221).

2-3. About other methods of ZaifPrivateApi

In addition to get_info (), get_info2 (), trade () verified this time, the following methods are prepared. [Details]

 - get_personal_info :Get the nickname and image URL used for chat
 - get_id_info :Get personal information such as user ID and email address
 - trade_history :Get transaction history
 - active_orders :Get a list of currently valid orders
 - cancel_order :Cancel an order
 - withdraw :Make a cryptocurrency withdrawal request
 - deposit_history :Get deposit history
 - withdraw_history :Get withdrawal history

in conclusion

This time, I was able to ** get transaction information ** and ** transaction *! ヽ ( ´∀ `) Eight (´∀ ` *) ノ I stumbled on the way, but I'm glad I moved safely. Now that we have obtained the information and verified the transaction, Next time, combine the contents verified this time and repeat "Get information ▶ Sell / Buy ▶ Return to the beginning" I would like to create something that will trade without permission. ** I think we can finally make something that can be called a bot ...! !! I expect **.

Thank you for your hard work! !!

Recommended Posts

Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 2 [Transaction with API]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 3 [Local Bot]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 4 [Serverless]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 3 [Local Bot]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 4 [Serverless]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 2 [Transaction with API]
After hitting the Qiita API with Python to get a list of articles for beginners, we will visit the god articles
(For beginners) Try creating a simple web API with Django
Forex automatic trading with genetic algorithm Part 3 Actual trading with Oanda API
Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
[Let's play with Python] Aiming for automatic sentence generation ~ Completion of automatic sentence generation ~
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Let's make a WEB application for phone book with flask Part 3
Let's make a WEB application for phone book with flask Part 4
Is it possible to enter a venture before listing and make a lot of money with stock options?
I have lived a life with a lot of "happiness". [Use COTOHA API to make "human disqualification" "happy"]
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 1)
Create a Twitter BOT service with GAE / P + Tweepy + RIOT API! (Part 2)
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda