From 02ab5212b9646bc5abcfccae614a8b1736b63ae3 Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Mon, 3 May 2021 12:55:59 +0200 Subject: [PATCH] Implement types. --- src/pricehist/cli.py | 2 +- src/pricehist/sources/coindesk.py | 2 +- src/pricehist/sources/coinmarketcap.py | 16 +++++++++++----- src/pricehist/sources/ecb.py | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pricehist/cli.py b/src/pricehist/cli.py index c3cb130..b385dd8 100644 --- a/src/pricehist/cli.py +++ b/src/pricehist/cli.py @@ -46,7 +46,7 @@ def cmd_fetch(args): source = sources.by_id[args.source]() output = outputs.by_type[args.output]() - prices = source.fetch(args.pair, args.start, args.end) + prices = source.fetch(args.pair, args.type, args.start, args.end) if args.renamebase or args.renamequote: prices = [ diff --git a/src/pricehist/sources/coindesk.py b/src/pricehist/sources/coindesk.py index 8af5884..0b8c74b 100644 --- a/src/pricehist/sources/coindesk.py +++ b/src/pricehist/sources/coindesk.py @@ -38,7 +38,7 @@ class CoinDesk: symbols = sorted([item["currency"] for item in data]) return symbols - def fetch(self, pair, start, end): + def fetch(self, pair, type, start, end): base, quote = pair.split("/") if base not in self.bases(): exit(f"Invalid base {base}") diff --git a/src/pricehist/sources/coinmarketcap.py b/src/pricehist/sources/coinmarketcap.py index 118799a..3535ac8 100644 --- a/src/pricehist/sources/coinmarketcap.py +++ b/src/pricehist/sources/coinmarketcap.py @@ -37,7 +37,7 @@ class CoinMarketCap: def quotes(): return [] - def fetch(self, pair, start, end): + def fetch(self, pair, type, start, end): base, quote = pair.split("/") url = "https://web-api.coinmarketcap.com/v1/cryptocurrency/ohlcv/historical" @@ -56,9 +56,15 @@ class CoinMarketCap: prices = [] for item in data["data"]["quotes"]: d = item["time_open"][0:10] - high = Decimal(str(item["quote"][quote]["high"])) - low = Decimal(str(item["quote"][quote]["low"])) - mid = sum([high, low]) / 2 - prices.append(Price(base, quote, d, mid)) + amount = self._amount(item["quote"][quote], type) + prices.append(Price(base, quote, d, amount)) return prices + + def _amount(self, data, type): + if type == "mid": + high = Decimal(str(data["high"])) + low = Decimal(str(data["low"])) + return sum([high, low]) / 2 + else: + return Decimal(str(data[type])) diff --git a/src/pricehist/sources/ecb.py b/src/pricehist/sources/ecb.py index 1efe952..a1ddb06 100644 --- a/src/pricehist/sources/ecb.py +++ b/src/pricehist/sources/ecb.py @@ -65,7 +65,7 @@ class ECB: "ZAR", ] - def fetch(self, pair, start, end): + def fetch(self, pair, type, start, end): base, quote = pair.split("/") if base not in self.bases(): exit(f"Invalid base {base}")