# Get cryptocurrency price

This article is for those who want to get the price of virtual currency with Python . I will publish my own code, so you can copy and paste it to Google Colab and run it as it is.

## code

It will be the whole code. If you only need the code, you can copy and paste it to GoogleColab and execute it to get the price of the virtual currency and output it to a file. It is also possible to get multiple values ​​by changing only the parameters.

``````# Install library to use bitmex API
!pip install ccxt
# coding:utf-8
import ccxt
import pandas as pd
from datetime import datetime, timedelta
import pytz

# Specify where to save the file
file_path = './vc_data.csv'

# Number of data you want
data_volume = 1000

# Specify the type, duration, and number of data to be acquired
vckind = 'BTC/USD'
vcduration = '1d' #'1h','5m','1m'

def get_virtual_currency(file_path,data_volume,vckind,vcduration):
bitmex = ccxt.bitmex()

# Get current time on bitmex
now_time = bitmex.fetch_ticker(vckind)['timestamp']

# Calculate the number of data acquired
from_time_difference = 0
if(vcduration == '1d'):
from_time_difference = 24 * 3600 * data_volume
elif(vcduration == '1h'):
from_time_difference = 3600 * data_volume
elif(vcduration == '5m'):
from_time_difference = 60 * 5 * data_volume
elif(vcduration == '1m'):
from_time_difference = 60 * 1 * data_volume

# Specify the data time you wish to retrieve. Multiply by 1000 for milliseconds.
from_time = now_time - from_time_difference * 1000

# Get Data
candles = bitmex.fetch_ohlcv(vckind, timeframe = vcduration, limit=1000,since = from_time)

# Formatting data with column names(unixtime,open,high,low,close,volume)
res = pd.DataFrame(None)
r = pd.DataFrame(candles)
res['time'] = r.apply(lambda d:datetime.fromtimestamp(int(d/1000)))
res['time'] = res['time'].apply(lambda d:pytz.utc.localize(d).astimezone(pytz.timezone("Asia/Tokyo")))
res['time'] = res['time'].apply(lambda d:d.strftime('%Y/%m/%d %H:%M:%S'))
res['open'],res['high'],res['low'],res['close'],res['volume'] = r,r,r,r,r

# Save formatted data in csv (exclude index information as it is unnecessary)
res.to_csv(file_path,index=0)
res = pd.DataFrame(None)
r = pd.DataFrame(None)

# main
# Retrieve virtual transit data and save to file_path
get_virtual_currency(file_path,data_volume,vckind,vcduration)

# Load and print saved data
print(data)``````

## How to use the code

Lines 9 to 15 specify the parameters for the acquired data.

By changing this parameter, you can get the price of various cryptocurrencies up to the current price.

• file_path: csv save destination of acquired data (Google Drive path is also possible)
• data_volume: number of rows of data to retrieve
• vkind: the type of data to retrieve
• vcduration: Interval of data to retrieve

By executing this parameter on line 55, you can get virtual currency data and output a file.

``get_virtual_currency(file_path,data_volume,vckind,vcduration)``

## Get all available data

I think that it is better to have many types of data when building AI for automatic trading. Let’s get the data that can be automatically acquired. However, if you make a request with a high frequency, it will be regarded as a DDOS attack and an error will occur. So I try to request it at regular intervals.

``````import time
# Set parameters together
data_volume = 1000
vckind = ['BTC/USD', 'ETH/USD' ,'XRP/USD']
vcduration = ['1d','1h','5m','1m']

# Data acquisition based on each parameter
for kind in vckind:
for duration in vcduration:
file_path = './vc_data'+'_'+kind.replace('/','_')+'_'+duration.replace('/','_')+'.csv'
get_virtual_currency(file_path,data_volume,kind,duration)