Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 45 additions & 65 deletions forex_python/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,51 @@ def get_latest_price(self, currency):
"""
Get latest price of one Bitcoin to valid currency 1BTC => X USD
"""
url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot'.format(currency)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi').get(currency, {}).get('rate_float', None)
if self._force_decimal:
return Decimal(price)
return price
price_str = data.get('data', {}).get('amount', None)
if price_str is not None:
if self._force_decimal:
return Decimal(price_str)
return float(price_str)
return None

def get_previous_price(self, currency, date_obj):
"""
Get price for one Bitcoin on given date
"""
start = date_obj.strftime('%Y-%m-%d')
end = date_obj.strftime('%Y-%m-%d')
url = (
'https://api.coindesk.com/v1/bpi/historical/close.json'
'?start={}&end={}&currency={}'.format(
start, end, currency
)
)
date_str = date_obj.strftime('%Y-%m-%d')
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot?date={}'.format(currency, date_str)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi', {}).get(start, None)
if self._force_decimal:
return Decimal(price)
return price
price_str = data.get('data', {}).get('amount', None)
if price_str is not None:
if self._force_decimal:
return Decimal(price_str)
return float(price_str)
raise RatesNotAvailableError("BitCoin Rates Source Not Ready For Given date")

def get_previous_price_list(self, currency, start_date, end_date):
"""
Get list of Bitcoin prices between two dates
"""
start = start_date.strftime('%Y-%m-%d')
end = end_date.strftime('%Y-%m-%d')
url = (
'https://api.coindesk.com/v1/bpi/historical/close.json'
'?start={}&end={}&currency={}'.format(
start, end, currency
)
)
response = requests.get(url)
if response.status_code == 200:
data = self._decode_rates(response)
price_dict = data.get('bpi', {})
return price_dict
return {}
from datetime import timedelta
price_dict = {}
curr_date = start_date
while curr_date <= end_date:
date_str = curr_date.strftime('%Y-%m-%d')
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot?date={}'.format(currency, date_str)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price_str = data.get('data', {}).get('amount', None)
if price_str is not None:
price_dict[date_str] = Decimal(price_str) if self._force_decimal else float(price_str)
curr_date += timedelta(days=1)
return price_dict

def convert_to_btc(self, amount, currency):
"""
Expand All @@ -81,14 +77,13 @@ def convert_to_btc(self, amount, currency):
else:
use_decimal = self._force_decimal

url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot'.format(currency)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi').get(currency, {}).get('rate_float', None)
if price:
if use_decimal:
price = Decimal(price)
price_str = data.get('data', {}).get('amount', None)
if price_str:
price = Decimal(price_str) if use_decimal else float(price_str)
try:
converted_btc = amount/price
return converted_btc
Expand All @@ -105,14 +100,13 @@ def convert_btc_to_cur(self, coins, currency):
else:
use_decimal = self._force_decimal

url = 'https://api.coindesk.com/v1/bpi/currentprice/{}.json'.format(currency)
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot'.format(currency)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi').get(currency, {}).get('rate_float', None)
if price:
if use_decimal:
price = Decimal(price)
price_str = data.get('data', {}).get('amount', None)
if price_str:
price = Decimal(price_str) if use_decimal else float(price_str)
try:
converted_amount = coins * price
return converted_amount
Expand All @@ -129,21 +123,14 @@ def convert_to_btc_on(self, amount, currency, date_obj):
else:
use_decimal = self._force_decimal

start = date_obj.strftime('%Y-%m-%d')
end = date_obj.strftime('%Y-%m-%d')
url = (
'https://api.coindesk.com/v1/bpi/historical/close.json'
'?start={}&end={}&currency={}'.format(
start, end, currency
)
)
date_str = date_obj.strftime('%Y-%m-%d')
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot?date={}'.format(currency, date_str)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi', {}).get(start, None)
if price:
if use_decimal:
price = Decimal(price)
price_str = data.get('data', {}).get('amount', None)
if price_str:
price = Decimal(price_str) if use_decimal else float(price_str)
try:
converted_btc = amount/price
return converted_btc
Expand All @@ -160,21 +147,14 @@ def convert_btc_to_cur_on(self, coins, currency, date_obj):
else:
use_decimal = self._force_decimal

start = date_obj.strftime('%Y-%m-%d')
end = date_obj.strftime('%Y-%m-%d')
url = (
'https://api.coindesk.com/v1/bpi/historical/close.json'
'?start={}&end={}&currency={}'.format(
start, end, currency
)
)
date_str = date_obj.strftime('%Y-%m-%d')
url = 'https://api.coinbase.com/v2/prices/BTC-{}/spot?date={}'.format(currency, date_str)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
price = data.get('bpi', {}).get(start, None)
if price:
if use_decimal:
price = Decimal(price)
price_str = data.get('data', {}).get('amount', None)
if price_str:
price = Decimal(price_str) if use_decimal else float(price_str)
try:
converted_btc = coins*price
return converted_btc
Expand Down