Witness Feed Publishing with Automatic SBD/USD Peg

A simple witness feed publishing with SBD/USD peg

I have been working on the markets module for steemtools v1.1 (which is going to be released later this week), and I have finished a sample witness feed publishing script earlier today.

@dantheman has made a case for SBD/USD $1 peg, which would improve the confidence in SBD, as well as increase its usefulness in trade.

I have modified the witness feed publishing script to allow for easy toggling between supporting the peg vs. reporting the regular implied price.

Without further ado, lets look into the code:

import time

from steemtools.experimental import Transactions
from steemtools.markets import Markets
from steemtools.node import Node

settings = {
    "sleep_time_seconds": 60,
    "minimum_spread_pct": 1.0,
    "sbd_usd_peg": True,
}
witness = "furion"
wif = ""

if __name__ == '__main__':
    steem = Node().default()
    markets = Markets()

    def get_last_published_price():
        my_info = steem.rpc.get_witness_by_account(witness)
        price = 0
        if float(my_info["sbd_exchange_rate"]["quote"].split()[0]) != 0:
            price = float(my_info["sbd_exchange_rate"]["base"].split()[0]) / float(
                my_info["sbd_exchange_rate"]["quote"].split()[0])
        return price

    while True:
        print("\n" + time.ctime())
        last_price = get_last_published_price()
        print("Published STEEM/USD price is: " + format(last_price, ".3f"))

        current_price = markets.steem_usd_implied()
        print("Implied STEEM/USD price is: %.3f" % current_price)
        if settings['sbd_usd_peg']:
            current_price *= markets.sbd_usd_implied()
            print("Pegged STEEM/USD price is: %.3f" % current_price)

        # if price diverged for more than our defined %, update the feed
        spread = abs(markets.calc_spread(last_price, current_price))
        print("Spread Between Prices: %.3f%%" % spread)
        if spread > settings['minimum_spread_pct']:
            tx = Transactions().witness_feed_publish(current_price, witness, wif, sim_mode=False)
            # print(tx)
            print("Updated the witness price feed.")

        time.sleep(settings['sleep_time_seconds'])

The above script will check the price every minute, and update it, if the price deviates from our published price by more than 1%.

We can change the the refresh interval, percent tolerance as well as toggle the SBD/USD peg by manipulating the settings:

settings = {
    "sleep_time_seconds": 60,  # refresh interval in seconds
    "minimum_spread_pct": 1.0, # 1% spread tolerance
    "sbd_usd_peg": True,       # support 1 SBD == 1 USD peg
}

Sample Output:

# Sample Output:
Mon Sep 19 12:26:20 2016
Tue Sep 20 01:07:22 2016
Published STEEM/USD price is: 0.462
Implied STEEM/USD price is: 0.491
Pegged STEEM/USD price is: 0.462
Spread Between Prices: 0.058%

Tue Sep 20 01:08:23 2016
Published STEEM/USD price is: 0.462
Implied STEEM/USD price is: 0.484
Pegged STEEM/USD price is: 0.455
Spread Between Prices: 1.569%
Updated the witness price feed.

Tue Sep 20 01:09:25 2016
Published STEEM/USD price is: 0.455
Implied STEEM/USD price is: 0.484
Pegged STEEM/USD price is: 0.455
Spread Between Prices: 0.023%

Sources:
> Feed Publishing Script Source
> Markets Implementation Source
> Transactions Implementation Source

How are the prices calculated?

BTC/USD:
Bitcoin price is a VWAP (volume weighted average price) of the prices from the following exchanges:

  • Bitstamp
  • Coinbase
  • Bitfinex
  • OKCoin
  • BTC-E

The exchanges with more volume have higher weight in determining the price.

STEEM, SBD:
STEEM and SBD are based on median prices from STEEM/BTC and STEEM/SBD pairs on:

  • Poloniex
  • Bittrex

Support for decentralized platforms (ie. Bitshares) is coming soon.

How does the peg work?

This is where the magic happens. Lets break it down.

from steemtools.markets import Markets

m = Markets()

m.steem_usd_implied()
#> 0.490638718898

m.steem_sbd_implied()
#> 0.522381648604

m.sbd_usd_implied()
#> 0.939234217376

.
steem_usd_implied() gives us the USD value of STEEM, according to the STEEM/BTC trades happening on 3rd party exchanges such as Poloniex and Bittrex.
We calculate it using this formula:

STEEM_USD = STEEM_BTC * BTC_USD

.
steem_sbd_implied() gives us the SBD value of STEEM, according to the relationship between STEEM/BTC and SBD/BTC trading pairs on 3rd party exchanges.
We calculate it using this formula:

STEEM_SBD = STEEM_BTC / SBD_BTC

.
sbd_usd_implied() give us the implied USD value of SBD, according to the SBD/BTC and BTC/USD trading pairs on 3rd party exchanges.
We calculate it using this formula:

SBD_USD = SBD_BTC * BTC_USD

Applying the $1 SBD = $1 USD Peg
To apply the peg, we simply multiply the STEEM/USD price with the implied SBD/USD price.

current_price = markets.steem_usd_implied()
if settings['sbd_usd_peg']:
    current_price *= markets.sbd_usd_implied()

This way, our pegged price will aim to correct the deviation of SBD/USD from the desired peg by manipulating the price of SBD/STEEM downwards if the SBD/USD is below $1, and by manipulating the price of SBD/STEEM upwards, if the SBD/USD is trading above $1.

Further considerations

You might want to modify the way the script operates. For instance, if you would like to reduce the responsiveness of the script to the recent price changes, you may want to take the average of the last X prices like so:

# add a list of recent prices outside of while True loop
recent_prices = []

# add our most recent price to the list
recent_prices.append(current_price)

# take the average of last 10 recent prices
average_price = numpy.mean(recent_prices[-10:])

I also highly recommend you do your due diligence and familiarize yourself with the experimental and markets modules of steemtools before proceeding.

Installation

This script depends on steemtools, which is a high level library built on top of @xeroc's Piston stack.

To install steemtools, simply run:

pip install -U steemtools

First, you need to provide a wif.wif (private active key). You can do this via environment variables, or a safe extraction from encrypted Piston wallet, see get_active_key_from_piston_wallet here).

Then you can run the script:

python witness_node.py

Enjoy :)


@furion is now a witness #83. Thank you for voting.


H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center