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 1 Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 2 is continued.

This time, we will finally create a ** bitcoin automatic trading bot-like ** that you can buy and sell by yourself!

specification

As an experiment to see if it works in combination ** Buy when it gets cheaper, and sell when it gets expensive! I made a stupid bot like **.

① Pass the previous transaction amount (this amount will be the criterion for judgment) ② If you have Bitcoin and the price goes up, sell it! (And update the previous transaction amount) ③ If you have Japanese Yen and the price drops, buy it! (And update the previous transaction amount) ④ If the transaction is not processed, wait 1 minute, if it is not processed even after waiting, cancel (and restore the previous transaction amount)

【reference】 -If the "order_id" of Response after transaction is not 0 (order_id is issued), the transaction is on hold.

Implementation

From this time, I changed the key and secret to json and store them in a separate file. []···folder [zaif]  |- main.py  |- [config] - zaif_keys.json |-...

main.py


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

import json
import time
from zaifapi import ZaifPublicApi  #Class that executes API that does not require authentication information published by Zaif
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)

zaif_keys_json = open('config/zaif_keys.json', 'r')
zaif_keys = json.load(zaif_keys_json)

KEY = zaif_keys["key"]
SECRET = zaif_keys["secret"]

if __name__ == '__main__':
    zaif_public = ZaifPublicApi()
    zaif_private = ZaifPrivateApi(KEY, SECRET)

    #Last transaction amount
    Last_transaction_amount = 122210

    #Information acquisition
    last_price = int(zaif_public.last_price('btc_jpy')["last_price"])
    trade_info = zaif_private.get_info2()
    funds_btc = trade_info["funds"]["btc"]
    funds_jpy = trade_info["funds"]["jpy"]
    print('■ This is the current information.')
    print('last_price: ' + str(last_price))
    print('funds_btc: ' + str(funds_btc))
    print('funds_jpy: ' + str(funds_jpy))
    cancel_flag = False
    order_id = 0
    last_price_old = 0

    #When you have btc and the price goes up
    if funds_btc != 0 and last_price > Last_transaction_amount:
        #Sell Bitcoin
        trade_result = zaif_private.trade(currency_pair="btc_jpy", action="ask", price=last_price, amount=funds_btc)
        print('■ I applied for the sale of Bitcoin.')
        pprint(trade_result)
        last_price_old = Last_transaction_amount
        Last_transaction_amount = last_price
        if trade_result["order_id"] != 0:
            cancel_flag = True
            order_id = trade_result["order_id"]
        else:
            print('■ The transaction is complete.')

    #When you have jpy and the price drops
    #(Minimum unit (0).0001btc minutes) when you have Japanese Yen or more)
    elif funds_jpy > Last_transaction_amount / 10000 and last_price < Last_transaction_amount:
        #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(float(funds_jpy) / last_price, 4) - 0.0001

        #Buy Bitcoin
        trade_result = zaif_private.trade(currency_pair="btc_jpy", action="bid", price=last_price, amount=amount)
        print('■ I applied for Bitcoin purchase')
        pprint(trade_result)
        last_price_old = Last_transaction_amount
        Last_transaction_amount = last_price
        if trade_result["order_id"] != 0:
            cancel_flag = True
            order_id = trade_result["order_id"]
        else:
            print('■ The transaction is complete.')

    #If the transaction is on hold, wait 60 seconds and cancel if it is still on hold
    if cancel_flag:
        print('■ Wait for 60 seconds.')
        time.sleep(60)
        trade_info = zaif_private.get_info2()
        if trade_info["open_orders"] > 0:
            print('■ Canceled.')
            pprint(zaif_private.cancel_order(order_id=order_id))
            Last_transaction_amount = last_price_old
        else:
            print('■ The transaction is complete.')
            pprint(trade_info)
    

Create a folder "config" in the same hierarchy as main.py and save it in the following json file.

zaif_keys.json


{
  "key" : "[Created Key]",
  "secret" : "[Created secret]"
}

■ Execution result When you have btc and the price goes up

python


■ This is the current information.
last_price: 126725
funds_btc: 0.0075
funds_jpy: 7.9355
■ I applied for the sale of Bitcoin.
{u'funds': {u'btc': 0.0, u'jpy': 7.9355, u'mona': 0.0, u'xem': 0.0},
 u'order_id': 156957485,
 u'received': 0.0,
 u'remains': 0.0075}
■ Wait for 60 seconds.
■ The transaction is complete.
{u'deposit': {u'btc': 0.0, u'jpy': 958.373, u'mona': 0.0, u'xem': 0.0},
 u'funds': {u'btc': 0.0, u'jpy': 958.373, u'mona': 0.0, u'xem': 0.0},
 u'open_orders': 0,
 u'rights': {u'info': 1, u'personal_info': 0, u'trade': 1, u'withdraw': 0},
 u'server_time': 1491323133}

■ Execution result When the price drops with jpy

python


■ This is the current information.
last_price: 126815
funds_btc: 0.0
funds_jpy: 959.048
■ I applied for Bitcoin purchase
{u'funds': {u'btc': 0.0, u'jpy': 7.9355, u'mona': 0.0, u'xem': 0.0},
 u'order_id': 156951488,
 u'received': 0.0,
 u'remains': 0.0075}
■ Wait for 60 seconds.
■ The transaction is complete.
{u'deposit': {u'btc': 0.0, u'jpy': 959.048, u'mona': 0.0, u'xem': 0.0},
 u'funds': {u'btc': 0.0, u'jpy': 7.9355, u'mona': 0.0, u'xem': 0.0},
 u'open_orders': 1,
 u'rights': {u'info': 1, u'personal_info': 0, u'trade': 1, u'withdraw': 0},
 u'server_time': 1491322779}

The result of moving in an infinite loop for one day

I processed the above code, executed it once every minute, and tried to move it for a day.

① Insert an infinite loop "while True:" before "# Get information" ② Insert "time.sleep (60)" at the end of the code

The result is ...!

Amount of the first transaction last night: * ¥ 957.8495 *

python


■ The transaction is complete.
{u'deposit': {u'btc': 0.0, u'jpy': 957.8495, u'mona': 0.0, u'xem': 0.0},
 u'funds': {u'btc': 0.0, u'jpy': 957.8495, u'mona': 0.0, u'xem': 0.0},
 u'open_orders': 0,
 u'rights': {u'info': 1, u'personal_info': 0, u'trade': 1, u'withdraw': 0},
 u'server_time': 1491324869}

Current money: * ¥ 958.8245 *

python


■ This is the current information.
last_price: 127740
funds_btc: 0.0
funds_jpy: 958.8245

When I calculate it. .. ..

Reflections or problems

・ You can only see the instantaneous value every minute (I don't know the fluctuation) -Since the transaction amount is only stored in a variable, it will fly if it falls due to an error. ・ It's not beautiful to keep running on the console of my home PC (PC fans are noisy when sleeping) ・ In the following cases (if the conditions are not met and you do not return), you will not be able to make any transactions. ・ If you have Bitcoin and the price drops ・ If you have Japanese Yen and the price has increased ・ Even if the price goes up even if it is 0.0001, it is not good to sell it (not profit ...)

in conclusion

Anyway, it was good that I could move without falling for a day. Next time, I would like to take advantage of my reflection points and ** run it on AWS Lambda **! With Lambda, it is executed every time, so you have to store the data in the DB so that the data does not fly.

Thank you for your hard work ~

Change log

2017/04/06: The description that key and secret were set to json and saved as a separate file was omitted. .. ..

Recommended Posts

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 1 [Information acquisition]
Beginners will make a Bitcoin automatic trading bot aiming for a lot of money! Part 2 [Transaction with API]
[Thinking stage] Predicting the daily price limit, strategy aiming for a lot of money-concept-editing