Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HERTZ and HERO algorithmic based assets. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
45 changes: 45 additions & 0 deletions btsprice/aba.py
Original file line number Diff line number Diff line change
@@ -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

7 changes: 5 additions & 2 deletions btsprice/bts_price_after_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
10 changes: 7 additions & 3 deletions btsprice/feedprice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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)]
Expand All @@ -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])
Expand Down
13 changes: 11 additions & 2 deletions config.json.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down