def run_hertz_function(): time_before = pendulum.now() # Getting the value of USD in BTS market = Market("USD:BTS") # Set reference market to USD:BTS price = market.ticker()[ "quoteSettlement_price"] # Get Settlement price of USD price.invert( ) # Switching from quantity of BTS per USD to USD price of one BTS. #Hertz variables: #Change only for alternative Algorithm Based Assets. hertz_reference_timestamp = "2015-10-13T14:12:24+00:00" # Bitshares 2.0 genesis block timestamp hertz_current_timestamp = pendulum.now().timestamp( ) # 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. hertz_core_exchange_rate = 0.80 # Calculate the current value of Hertz in USD hertz_value = get_hertz_feed(hertz_reference_timestamp, hertz_current_timestamp, hertz_period_days, hertz_phase_days, hertz_reference_asset_value, hertz_amplitude) hertz = Price(hertz_value, "USD/HERTZ" ) # Limit the hertz_usd decimal places & convert from float. cg = CoinGeckoAPI() # Initialise coingecko bts_usd_coingecko = cg.get_price( ids='bitshares', vs_currencies='usd') # Price of BTS in USD from coingecko bts_usd_coingecko_value = Price(bts_usd_coingecko["bitshares"]["usd"], "USD/BTS") # Price format hertz_bts = bts_usd_coingecko_value.invert() * hertz.as_quote( "HERTZ") # Feed price hertz_cer = hertz_bts * hertz_core_exchange_rate # Some printed outputs #print("Price of HERTZ in USD: {}".format(hertz)) #print("Price of HERTZ in BTS: {}".format(hertz_bts)) #print("Price of BTS in USD: {}".format(bts_usd_coingecko_value)) #print("Price of USD in BTS: {}".format(bts_usd_coingecko_value.invert())) # Unlock the Bitshares wallet hertz.bitshares.wallet.unlock("LOCAL_WALLET_PASSWORD") """ Publish the price feed to the BTS DEX Make sure you change the 'account' before executing. Don't change any of the other values. """ pprint( hertz.bitshares.publish_price_feed( "HERTZ", hertz_bts, cer=hertz_cer, # Setting in line with Wackou's price feed scripts mssr=110, mcr=200, account="REPLACE_WITH_YOUR_USERNAME"))
def type_intern(self, symbol): """ Process a price from a formula """ asset = Asset(symbol, full=True) short_backing_asset = Asset( asset["bitasset_data"]["options"]["short_backing_asset"]) backing_symbol = short_backing_asset["symbol"] asset["short_backing_asset"] = short_backing_asset if self.assetconf(symbol, "type") != "formula": return if self.assetconf(symbol, "reference") == "extern": price = eval( self.assetconf(symbol, "formula").format(**self.price_result)) elif self.assetconf(symbol, "reference") == "intern": # Parse the forumla according to ref_asset if self.assethasconf(symbol, "ref_asset"): ref_asset = self.assetconf(symbol, "ref_asset") market = Market("%s:%s" % (ref_asset, backing_symbol)) ticker_raw = market.ticker() ticker = {} for k, v in ticker_raw.items(): if isinstance(v, Price): ticker[k] = float(v.as_quote("BTS")) elif isinstance(v, Amount): ticker[k] = float(v) price = eval( str(self.assetconf(symbol, "formula")).format(**ticker)) else: price = eval(str(self.assetconf(symbol, "formula"))) else: 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) cer = self.get_cer(symbol, price) self.price_result[symbol] = { "price": float(price.as_quote(backing_symbol)), "cer": float(cer), "number": 1, "short_backing_symbol": backing_symbol, "mean": float(price), "median": float(price), "weighted": float(price), "mssr": self.assetconf(symbol, "maximum_short_squeeze_ratio"), "mcr": self.assetconf(symbol, "maintenance_collateral_ratio"), "std": 0.0, "number": 1, }
def get_hertz_value(api_key: hug.types.text, request, hug_timer=15): """Retrieve reference Hertz feed price value in JSON.""" if (check_api_token(api_key) == True): # Check the api key google_analytics(request, 'get_hertz_value') # Getting the value of USD in BTS market = Market("USD:BTS") # Set reference market to USD:BTS price = market.ticker()["quoteSettlement_price"] # Get Settlement price of USD price.invert() # Switching from quantity of BTS per USD to USD price of one BTS. hertz_reference_timestamp = "2015-10-13T14:12:24+00:00" # Bitshares 2.0 genesis block timestamp hertz_current_timestamp = pendulum.now().timestamp() # Current timestamp for reference within the hertz script hertz_amplitude = 0.14 # 14% fluctuating the price feed $+-0.14 (1% 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. hertz_value = get_hertz_feed(hertz_reference_timestamp, hertz_current_timestamp, hertz_period_days, hertz_phase_days, hertz_reference_asset_value, hertz_amplitude) hertz = Price(hertz_value, "USD/HERTZ") # Limit the hertz_usd decimal places & convert from float. # Calculate HERTZ price in BTS (THIS IS WHAT YOU PUBLISH!) hertz_bts = price.as_base("BTS") * hertz.as_quote("HERTZ") unofficial_data = {'hertz_price_in_usd': hertz['price'], 'hertz_price_in_bts': hertz_bts['price'], 'core_exchange_rate': hertz_bts['price']*0.80, 'usd_price_in_bts': 1/price['price'], 'bts_price_in_usd': price['price']} ######## try: target_asset = Asset("HERTZ", full=True) except: return {'valid_asset': False, 'valid_key': True, 'took': float(hug_timer)} try: bitasset_data = target_asset['bitasset_data_id'] except: bitasset_data = None extracted_object = extract_object(target_asset) bitasset_data = extracted_object['bitasset_data'] current_feeds = bitasset_data['current_feed'] current_feed_settlement_price = current_feeds['settlement_price'] current_feed_cer = current_feeds['core_exchange_rate'] if (int(current_feed_settlement_price['base']['amount']) > 0 and int(current_feed_settlement_price['quote']['amount']) > 0): current_feed_settlement_price['api_calculated_rate'] = int(current_feed_settlement_price['quote']['amount'])/int(current_feed_settlement_price['base']['amount']) else: current_feed_settlement_price['api_calculated_rate'] = 0 if (int(current_feed_cer['base']['amount']) > 0 and int(current_feed_cer['quote']['amount']) > 0): current_feed_cer['api_calculated_rate'] = int(current_feed_cer['quote']['amount'])/int(current_feed_cer['base']['amount']) else: current_feed_cer['api_calculated_rate'] = 0 witness_feeds = bitasset_data['feeds'] witness_feed_data = {} witness_iterator = 0 for witness_feed in witness_feeds: # Extract that data! witness_id = witness_feed[0] witness_iterator += 1 try: target_account = Account(str(witness_id)) except: print("Witness account doesn't work?!") extracted_object = extract_object(target_account) witness_name = extracted_object['name'] publish_timestamp = witness_feed[1][0] feed_data = witness_feed[1][1] settlement_price = feed_data['settlement_price'] if (int(settlement_price['quote']['amount']) > 0): maintenance_collateral_ratio = feed_data['maintenance_collateral_ratio'] maximum_short_squeeze_ratio = feed_data['maximum_short_squeeze_ratio'] core_exchange_rate = feed_data['core_exchange_rate'] settlement_price_before = int(settlement_price['quote']['amount'])/int(settlement_price['base']['amount']) core_exchange_rate_before = int(core_exchange_rate['quote']['amount'])/(int(core_exchange_rate['base']['amount'])) settlement_price['api_calculated_rate'] = settlement_price_before / 10 core_exchange_rate['api_calculated_rate'] = core_exchange_rate_before / 10 try: target_witness = Witness(witness_name) except: target_witness = None if (target_witness is not None): witness_role_data = extract_object(target_witness) witness_identity = witness_role_data['id'] witness_url = witness_role_data['url'] witness_feed_data[str(witness_iterator)] = {'witness_account_id': witness_id, 'witness_name': witness_name, 'witness_id': witness_identity, 'witness_url': witness_url, 'publish_timestamp': publish_timestamp, 'settlement_price': settlement_price, 'maintenance_collateral_ratio': maintenance_collateral_ratio, 'maximum_short_squeeze_ratio': maximum_short_squeeze_ratio, 'core_exchange_rate': core_exchange_rate} else: witness_feed_data[str(witness_iterator)] = {'witness_account_id': witness_id, 'witness_name': witness_name, 'witness_id': "N/A", 'witness_url': "#", 'publish_timestamp': publish_timestamp, 'settlement_price': settlement_price, 'maintenance_collateral_ratio': maintenance_collateral_ratio, 'maximum_short_squeeze_ratio': maximum_short_squeeze_ratio, 'core_exchange_rate': core_exchange_rate} else: continue return {'unofficial_reference': unofficial_data, 'witness_feeds': witness_feed_data, 'current_feeds': current_feeds, 'valid_key': True, 'took': float(hug_timer)} else: # API KEY INVALID! return {'valid_key': False, 'took': float(hug_timer)}
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. # Calculate the current value of Hertz in USD hertz_value = get_hertz_feed(hertz_reference_timestamp, hertz_current_timestamp, hertz_period_days, hertz_phase_days, hertz_reference_asset_value, hertz_amplitude) hertz = Price(hertz_value, "USD/HERTZ" ) # Limit the hertz_usd decimal places & convert from float. print(hertz) # Calculate HERTZ price in BTS (THIS IS WHAT YOU PUBLISH!) hertz_bts = price.as_base("BTS") * hertz.as_quote("HERTZ") print("Hz-Quote: {}".format(hertz.as_quote("HERTZ"))) print("base: {}".format(price.as_base("BTS"))) hertz_core_exchange_rate = 0.80 # 20% offset, CER > Settlement! hertz_cer = hertz_bts * hertz_core_exchange_rate # Some printed outputs print("Price of HERTZ in USD: {}".format(hertz)) print("Price of HERTZ in BTS: {}".format(hertz_bts)) print("Price of BTS in USD: {}".format(price)) print("Price of USD in BTS: {}".format(price.invert())) # Unlock the Bitshares wallet hertz.bitshares.wallet.unlock("LOCAL_WALLET_PASSWORD") """