Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file

Get Bitcoin rate from Bitflyer at regular intervals and save it to a file

Install pybitflyer

pip install pybitflyer

API Key

Can be obtained from the following after registration https://lightning.bitflyer.jp/developer

bitFlyer Lightning API

・ Restrictions https://lightning.bitflyer.jp/docs?lang=ja&_ga=2.103916546.34290189.1503318908-1417182779.1500704462

API restrictions

The HTTP API limits the number of calls as follows.

The Private API is limited to about 200 times per minute.
The maximum number of times per IP address is about 500 times per minute.


Average daily contract price is 0.Users less than 01 may be limited to approximately 10 Private API calls per minute the next day.
Manual ordering is not restricted.

・ Acquisition of Ticker

https://lightning.bitflyer.jp/docs?lang=ja&_ga=2.103916546.34290189.1503318908-1417182779.1500704462#ticker

Source

Get every 5 seconds

import pybitflyer
import time
from datetime import datetime
from pytz import timezone
from dateutil import parser
import pytz

TICKER_KEYS = ["product_code", 
                "timestamp", 
                "tick_id", 
                "best_bid", 
                "best_ask",
                "best_bid_size",
                "best_ask_size",
                "total_bid_depth",
                "total_ask_depth",
                "ltp",
                "volume",
                "volume_by_product"]

API_KEY = "XXXXXXXXXXXXXXXXXXXXXX" #Obtained from My Page after logging in to Bitflyer
API_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXX" #Obtained from My Page after logging in to Bitflyer
                
def to_jst(datestr):
    return pytz.timezone('UTC').localize(parser.parse(datestr)).astimezone(timezone('Asia/Tokyo'))
     
if __name__ == '__main__':
    api = pybitflyer.API(api_key = API_KEY, api_secret = API_SECRET)
    
    now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
    ticker_data = open(now + "_ticker_data.tsv", "w")
    for ticker_key in TICKER_KEYS:
        ticker_data.write(ticker_key + "\t")
    ticker_data.write("\n")
    while True:
        ticker = api.ticker(product_code="BTC_JPY")
        for ticker_key in TICKER_KEYS:
            if ticker_key == "timestamp":
                data = to_jst(str(ticker[ticker_key]).replace("T", " ")).strftime("%Y/%m/%d %H:%M:%S")
            else:
                data = str(ticker[ticker_key])
            ticker_data.write(data + "\t")
        ticker_data.write("\n")
        ticker_data.flush()
        time.sleep(5)
    ticker_data.close()

result

cap.PNG

Remarks

It will fail due to a connection error, so if you do it properly, you need to take measures against the error.

Recommended Posts

Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
Get BTC / JPY board information from Python --bitflyer
Process Splunk execution results using Python and save to a file
Get OCTA simulation conditions from a file and save with pandas
[python] Send the image captured from the webcam to the server and save it
[Python] How to save the installed package and install it in a new environment at once Mac environment
[Python] Concatenate a List containing numbers and write it to an output file.
Get mail from Gmail and label it with Python3
Python script to create a JSON file from a CSV file
[Python] Start a batch file from Python and pass variables.
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
Get the matched string with a regular expression and reuse it when replacing on Python3
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Recursively get the Excel list in a specific folder with python and write it to Excel.
I made a server with Python socket and ssl and tried to access it from a browser
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
Get an image from a web page and resize it
How to run a Python file at a Windows 10 command prompt
Temporarily save a Python object and reuse it in another Python
Python regular expression basics and tips to learn from scratch
Get a Python web page, character encode it, and display it
Get a global IP and export it to Google Spreadsheets
A python beginner tried to intern at an IT company
I want to get information from fstab at the ssh connection destination and execute a command
Get 1000 posts in Python order from all Slack channels and put them together in a txt file
[Python] How to scrape a local html file and output it as CSV using Beautiful Soup
Read the old Gakushin DC application Word file (.doc) from Python and try to operate it.
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
How to get followers and followers from python using the Mastodon API
How to get a string from a command line argument in python
[Python] How to get & change rows / columns / values from a table.
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Everything from building a Python environment to running it on Windows
Pass the selected item in Tablacus Explorer from JScript to python and rename it all at once
A story about someone who wanted to import django from a python interactive shell and save things to a DB
I want to pass an argument to a python function and execute it from PHP on a web server
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
[Python Kivy] How to get the file path by dragging and dropping
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
Tips: [Python] Randomly restore and extract an array from a fasta file
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
How to get a value from a parameter store in lambda (using python)
Connect to postgreSQL from Python and use stored procedures in a loop.
Read CSV file with Python and convert it to DataFrame as it is
Start the webcam to take a still image and save it locally
Make a decision tree from 0 with Python and understand it (4. Data structure)
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
How to save the feature point information of an image in a file and use it for matching