diff --git a/btsprice/aba.py b/btsprice/aba.py new file mode 100644 index 0000000..514b893 --- /dev/null +++ b/btsprice/aba.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +import datetime +import time +import math + +ABA = ["HERTZ", "HERO"] + +SECONDS_PER_DAY = 60 * 60 * 24 + +def _get_hertz_feed(reference_timestamp, current_timestamp, period_days, phase_days, reference_asset_value, amplitude): + """ + Given the reference timestamp, the current timestamp, the period (in days), the phase (in days), the reference asset value (ie 1.00) and the amplitude (> 0 && < 1), output the current hertz value. + You can use this formula for an alternative HERTZ asset! + Be aware though that extreme values for amplitude|period will create high volatility which could cause black swan events. BSIP 18 should help, but best tread carefully! + """ + hz_reference_timestamp = datetime.datetime.strptime(reference_timestamp, "%Y-%m-%dT%H:%M:%S").timestamp() # Retrieving the Bitshares2.0 genesis block timestamp + hz_period = SECONDS_PER_DAY * period_days + hz_phase = SECONDS_PER_DAY * phase_days + hz_waveform = math.sin(((((current_timestamp - (hz_reference_timestamp + hz_phase))/hz_period) % 1) * hz_period) * ((2*math.pi)/hz_period)) # Only change for an alternative HERTZ ABA. + hz_value = reference_asset_value + ((amplitude * reference_asset_value) * hz_waveform) + + return hz_value + +def compute_hertz(): + hertz_reference_timestamp = "2015-10-13T14:12:24" # Bitshares 2.0 genesis block timestamp + hertz_current_timestamp = datetime.datetime.now() # Current timestamp for reference within the hertz script + hertz_amplitude = 0.14 # 14% fluctuating the price feed $+-0.14 (2% per day) + hertz_period_days = 28 # Aka wavelength, time for one full SIN wave cycle. + hertz_phase_days = 0.908056 # Time offset from genesis till the first wednesday, to set wednesday as the primary Hz day. + hertz_reference_asset_value = 1.00 # $1.00 USD, not much point changing as the ratio will be the same. + + hz_value = _get_hertz_feed(hertz_reference_timestamp, hertz_current_timestamp.timestamp(), hertz_period_days, hertz_phase_days, hertz_reference_asset_value, hertz_amplitude) + + return hz_value + +def compute_hero(): + hero_reference_timestamp = datetime.date(1913, 12, 23) + current_timestamp = datetime.date.today() + hero_days_in_year = 365.2425 + hero_inflation_rate = 1.05 + + hero_value = hero_inflation_rate ** ((current_timestamp - hero_reference_timestamp).days / hero_days_in_year) + + return hero_value + diff --git a/btsprice/bts_price_after_match.py b/btsprice/bts_price_after_match.py index ca66744..6b91fa0 100644 --- a/btsprice/bts_price_after_match.py +++ b/btsprice/bts_price_after_match.py @@ -3,6 +3,7 @@ import copy from math import fabs from btsprice.misc import get_median +from btsprice.aba import compute_hertz, compute_hero # from pprint import pprint @@ -111,9 +112,7 @@ def compute_rate_cny(self): rate_cny["TCNY"] = rate_cny["CNY"] rate_cny["TUSD"] = rate_cny["USD"] - # price_btc_queue = {"CNY": [], "USD": []} price_btc_queue = [] - price_btc = {} for name in btc_ticker: quote = btc_ticker[name]["quote"] if quote not in rate_cny: @@ -124,6 +123,10 @@ def compute_rate_cny(self): price_btc = get_median(price_btc_queue) # print(price_btc, price_btc_queue) rate_cny["BTC"] = price_btc + + rate_cny["HERTZ"] = compute_hertz() * rate_cny["USD"] + rate_cny["HERO"] = compute_hero() * rate_cny["USD"] + self.rate_cny = rate_cny def update_orderbook(self): diff --git a/btsprice/feedprice.py b/btsprice/feedprice.py index 3c7afe3..9ff3af2 100644 --- a/btsprice/feedprice.py +++ b/btsprice/feedprice.py @@ -7,6 +7,7 @@ from btsprice.task_exchanges import TaskExchanges from btsprice.task_pusher import TaskPusher from btsprice.bts_price_after_match import BTSPriceAfterMatch +from btsprice.aba import ABA from btsprice.feedapi import FeedApi import time import logging @@ -101,7 +102,8 @@ def init_mpa_info(self): "SGD", "HKD", "RUB", "SEK", "NZD", "CNY", "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "USD", "SHENZHEN", "NASDAQC", "NIKKEI", - "HANGSENG", "SHANGHAI", "TCNY", "TUSD", "ARS"] + "HANGSENG", "SHANGHAI", "TCNY", "TUSD", "ARS", + "HERO", "HERTZ"] self.price_queue = {} for asset in peg_asset_list: self.price_queue[asset] = [] @@ -158,7 +160,8 @@ def get_median_price(self, bts_price_in_cny): continue self.price_queue[asset].append(bts_price_in_cny / self.bts_price.rate_cny[asset]) - if len(self.price_queue[asset]) > self.sample: + max_samples = self.sample if asset not in ABA else 1 + if len(self.price_queue[asset]) > max_samples: self.price_queue[asset].pop(0) median_price[asset] = sorted( self.price_queue[asset])[int(len(self.price_queue[asset]) / 2)] @@ -173,7 +176,8 @@ def get_average_price(self, bts_price_in_cny): continue self.price_queue[asset].append(bts_price_in_cny / self.bts_price.rate_cny[asset]) - if len(self.price_queue[asset]) > self.sample: + max_samples = self.sample if asset not in ABA else 1 + if len(self.price_queue[asset]) > max_samples: self.price_queue[asset].pop(0) average_price[asset] = sum( self.price_queue[asset])/len(self.price_queue[asset]) diff --git a/config.json.sample b/config.json.sample index e75cca0..1654923 100644 --- a/config.json.sample +++ b/config.json.sample @@ -3,7 +3,8 @@ "timer_minute": 2, "asset_list": [ "BTC", "SILVER", "GOLD", "TRY", "SGD", "HKD", "NZD", "CNY", - "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "USD", "KRW", "TUSD", "ARS"], + "MXN", "CAD", "CHF", "AUD", "GBP", "JPY", "EUR", "USD", "KRW", "TUSD", "ARS", + "HERTZ, "HERO"], "alias": { "RUBLE": "RUB" }, @@ -41,7 +42,15 @@ "maintenance_collateral_ratio": 1750, "maximum_short_squeeze_ratio": 1100 }, - "TUSD": {"maximum_short_squeeze_ratio": 1050} + "TUSD": {"maximum_short_squeeze_ratio": 1050}, + "HERO": { + "maintenance_collateral_ratio": 2000, + "maximum_short_squeeze_ratio": 1100 + }, + "HERTZ": { + "maintenance_collateral_ratio": 2000, + "maximum_short_squeeze_ratio": 1100 + } }, "pusher": { "common": "set enable to 1, if you want to subscribe data from pusher service",