diff --git a/bitshares_pricefeed/pricefeed.py b/bitshares_pricefeed/pricefeed.py index b0aeec5..a781021 100644 --- a/bitshares_pricefeed/pricefeed.py +++ b/bitshares_pricefeed/pricefeed.py @@ -2,7 +2,7 @@ import numpy as num import time from pprint import pprint -from math import fabs, sqrt +from math import * from bitshares.account import Account from bitshares.asset import Asset from bitshares.price import Price @@ -75,7 +75,7 @@ def obtain_price_change(self, symbol): oldPrice = float("inf") self.price_result[symbol]["priceChange"] = (oldPrice - newPrice) / newPrice * 100.0 self.price_result[symbol]["current_feed"] = current_feed - self.price_result[symbol]["global_feed"] = asset["bitasset_data"]["current_feed"] + self.price_result[symbol]["global_feed"] = asset.feed def obtain_flags(self, symbol): """ This will add attributes to price_result and indicate the results @@ -437,7 +437,7 @@ def type_intern(self, symbol): ticker = {} for k, v in ticker_raw.items(): if isinstance(v, Price): - ticker[k] = float(v.as_quote("BTS")) + ticker[k] = float(v.as_quote(backing_symbol)) elif isinstance(v, Amount): ticker[k] = float(v) price = eval(str( @@ -448,13 +448,13 @@ def type_intern(self, symbol): raise ValueError("Missing 'reference' for asset %s" % symbol) orientation = self.assetconf(symbol, "formula_orientation", no_fail=True)\ - or "{}:{}".format(backing_symbol, symbol) # default value - price = Price(price, orientation) + or "{}:{}".format(symbol, backing_symbol) # default value + price = Price(price, orientation).as_quote(backing_symbol) cer = self.get_cer(symbol, price) self.price_result[symbol] = { - "price": float(price.as_quote(backing_symbol)), + "price": float(price), "cer": cer, "number": 1, "short_backing_symbol": backing_symbol, diff --git a/bitshares_pricefeed/ui.py b/bitshares_pricefeed/ui.py index 2382731..9155b7c 100644 --- a/bitshares_pricefeed/ui.py +++ b/bitshares_pricefeed/ui.py @@ -95,10 +95,11 @@ def print_prices(feeds): for symbol, feed in feeds.items(): if not feed: continue + collateral = feed["short_backing_symbol"] myprice = feed["price"] - blockchain = float(Price(feed["global_feed"]["settlement_price"])) + blockchain = feed["global_feed"]["settlement_price"].as_quote(collateral)['price'] if "current_feed" in feed and feed["current_feed"]: - last = float(feed["current_feed"]["settlement_price"]) + last = feed["current_feed"]["settlement_price"].as_quote(collateral)['price'] age = (str(datetime.utcnow() - feed["current_feed"]["date"])) else: last = -1.0 @@ -106,7 +107,7 @@ def print_prices(feeds): # Get Final Price according to price metric t.add_row([ symbol, - ("%s") % (feed["short_backing_symbol"]), + ("%s" % collateral), ("%s" % formatPrice(feed["price"])), ("%s" % formatPrice(feed["cer"])), ("%s (%s)" % (formatPrice(feed["mean"]), priceChange(myprice, feed.get("mean")))), diff --git a/tests/formula/conftest.py b/tests/formula/conftest.py new file mode 100644 index 0000000..1a99905 --- /dev/null +++ b/tests/formula/conftest.py @@ -0,0 +1,34 @@ +import pytest +import os +import yaml +import math + +@pytest.fixture +def conf(request): + confName = getattr(request.module, 'config') + basePath = os.path.join(os.path.dirname(__file__), '../../') + confDir = os.path.join(basePath, confName) + conf = yaml.load(open(confDir)) + producer = getattr(request.module, 'producer', 'null-account') + conf['producer'] = producer + return conf + +class Checkers: + @staticmethod + def check_price(prices, computedAssetName, collateralName, maxSpread): + print(prices) + assert computedAssetName in prices + assetPrice = prices[computedAssetName] + for field in ['mean', 'global_feed', 'price', 'weighted', 'cer', + 'priceChange', 'mssr', 'current_feed', 'short_backing_symbol', + 'median', 'mcr', 'std', 'number']: + assert field in assetPrice + assert collateralName == assetPrice['short_backing_symbol'] + feedPrice = assetPrice['global_feed']['settlement_price'].as_quote(collateralName)['price'] + computedPrice = assetPrice['price'] + assert math.fabs(1 - (computedPrice / feedPrice)) < maxSpread + +@pytest.fixture +def checkers(): + return Checkers + diff --git a/tests/formula/test_hero.py b/tests/formula/test_hero.py new file mode 100644 index 0000000..499c529 --- /dev/null +++ b/tests/formula/test_hero.py @@ -0,0 +1,9 @@ +from bitshares_pricefeed.pricefeed import Feed + +config = 'bitshares_pricefeed/examples/hero.yaml' + +def test_hero_computation(conf, checkers): + feed = Feed(conf) + feed.derive({'HERO'}) + prices = feed.get_prices() + checkers.check_price(prices, 'HERO', 'BTS', 0.1) diff --git a/tests/formula/test_hertz.py b/tests/formula/test_hertz.py new file mode 100644 index 0000000..6c303f3 --- /dev/null +++ b/tests/formula/test_hertz.py @@ -0,0 +1,9 @@ +from bitshares_pricefeed.pricefeed import Feed + +config = 'bitshares_pricefeed/examples/hertz.yaml' + +def test_hertz_computation(conf, checkers): + feed = Feed(conf) + feed.derive({'HERTZ'}) + prices = feed.get_prices() + checkers.check_price(prices, 'HERTZ', 'BTS', 0.1) diff --git a/tests/formula/test_xcd.py b/tests/formula/test_xcd.py new file mode 100644 index 0000000..037cee7 --- /dev/null +++ b/tests/formula/test_xcd.py @@ -0,0 +1,9 @@ +from bitshares_pricefeed.pricefeed import Feed + +config = 'bitshares_pricefeed/examples/XCD.yaml' + +def test_XCD_computation(conf, checkers): + feed = Feed(conf) + feed.derive({'XCD'}) + prices = feed.get_prices() + checkers.check_price(prices, 'XCD', 'USD', 0.1)