Skip to content

Commit

Permalink
Test assets with formula, fix import issues and summary display.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zapata committed Jun 7, 2018
1 parent 2f74004 commit 2e138fc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
12 changes: 6 additions & 6 deletions bitshares_pricefeed/pricefeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions bitshares_pricefeed/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,19 @@ 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
age = "unknown"
# 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")))),
Expand Down
34 changes: 34 additions & 0 deletions tests/formula/conftest.py
Original file line number Diff line number Diff line change
@@ -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

9 changes: 9 additions & 0 deletions tests/formula/test_hero.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions tests/formula/test_hertz.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions tests/formula/test_xcd.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 2e138fc

Please sign in to comment.