示例#1
0
def aggregate_rates():
    """
    sort and aggregate data from external sources
    calculate medians
    """
    sources = refresh_forex_rates()
    aggregate = {}
    for source, prices in sources.items():
        for pair, price in prices.items():
            if pair in aggregate.keys():
                aggregate[pair].append((price, source))
            else:
                aggregate[pair] = [(price, source)]
    medians = {
        k: (
            sigfig(median([i[0] for i in v])),
            len([i[0] for i in v]),
            [i[1] for i in v],
        )
        for k, v in aggregate.items()
    }
    return {
        "sources": sources,
        "aggregate": aggregate,
        "medians": medians,
    }
示例#2
0
def create_qty_rate(prices, gateway):
    """
    fetch a qty rate dictionary for the correct gateway
    """
    if gateway == "DEEX":
        qty_rate = deex_qty_rate(prices)
    if gateway == "XBTSX":
        qty_rate = xbtsx_qty_rate(prices)
    if gateway == "RUDEX":
        qty_rate = rudex_qty_rate(prices)

    for key, val in qty_rate.items():
        qty_rate[key] = {"qty": val["qty"], "rate": sigfig(val["rate"])}

    return qty_rate
示例#3
0
def true_dex_last(pair):
    """
    ensure 3 consecutive requests to random nodes return same response
    """
    rpc = reconnect(None)
    lasts = []
    while True:
        try:
            last = sigfig(float(rpc_last(rpc, pair)))
        except:
            continue
        lasts.append(last)
        lasts = lasts[-3:]
        print(pair, lasts)
        if (len(lasts) == 3) and (len(list(set(lasts))) == 1):
            break
        rpc = reconnect(rpc)
    try:
        rpc.close()
    except:
        pass

    return last
def thresh(storage, process, epoch, pid, cache):  # DONE
    """
    Make calls for data, shake out any errors
    There are 20 threshing process running in parallel
    They are each periodically terminated and respawned
    """
    handshake_bs = []
    ping_bs = []
    block_bs = []
    reject_bs = []
    storage["access"] = 0
    storage["data_latency"] = 0
    while True:
        storage["mean_ping"] = 0.5
        try:
            nodes = get_nodes()
            static_nodes = public_nodes()
            shuffle(nodes)
            node = nodes[0]
            storage["bw_depth"] = max(int(len(nodes) / 6), 1)
            # CHECK BLACK AND WHITE LISTS
            black = race_read(doc="blacklist.txt")[-storage["bw_depth"]:]
            white = race_read(doc="whitelist.txt")[-storage["bw_depth"]:]
            try:
                start = time()
                metanode = bitshares_trustless_client()
                storage["access"] = time() - start
                ping = storage["mean_ping"] = metanode["ping"]
                blacklist = metanode["blacklist"][-storage["bw_depth"]:]
                whitelist = metanode["whitelist"][-storage["bw_depth"]:]
                blocktime = metanode["blocktime"]
                storage["data_latency"] = time() - blocktime
                del metanode
                if len(blacklist) > len(black):
                    black = blacklist
                    race_write("blacklist.txt", json_dump(black))
                if len(whitelist) > len(white):
                    white = whitelist
                    race_write("whitelist.txt", json_dump(white))
            except BaseException:
                pass
            if node in black:
                raise ValueError("blacklisted")
            if node in white:
                raise ValueError("whitelisted")
            # connect to websocket
            rpc, handshake_latency, handshake_max = wss_handshake(
                storage, node)
            # use each node several times
            utilizations = UTILIZATIONS
            if (time() - cache["begin"]) < 100:
                utilizations = 1
            for util in range(utilizations):
                sleep(THRESH_PAUSE)
                # Database calls w/ data validations
                ping_latency, ping_max = rpc_ping_latency(rpc, storage)
                block_latency, block_max, blocktime = rpc_block_latency(
                    rpc, storage)
                set_timing = "                  " + "speed/max/ratio/cause/rate"
                if handshake_max == 5:
                    set_timing = "                  " + it(
                        "cyan", "RESOLVING MEAN NETWORK SPEED")
                # timing analysis for development
                ping_r = ping_latency / ping_max
                block_r = block_latency / block_max
                handshake_r = handshake_latency / handshake_max
                ping_b = int(bool(int(ping_r)))
                block_b = int(bool(int(block_r)))
                handshake_b = int(bool(int(handshake_r)))
                reject_b = int(bool(ping_b + block_b + handshake_b))
                ping_bs.append(ping_b)
                block_bs.append(block_b)
                reject_bs.append(reject_b)
                handshake_bs.append(handshake_b)
                ping_bs = ping_bs[-100:]
                block_bs = block_bs[-100:]
                reject_bs = reject_bs[-100:]
                handshake_bs = handshake_bs[-100:]
                ping_p = sum(ping_bs) / max(1, len(ping_bs))
                block_p = sum(block_bs) / max(1, len(block_bs))
                reject_p = sum(reject_bs) / max(1, len(reject_bs))
                handshake_p = sum(handshake_bs) / max(1, len(handshake_bs))
                ping_b = str(ping_b).ljust(7)
                block_b = str(block_b).ljust(7)
                handshake_b = str(handshake_b).ljust(7)
                reject = "".ljust(7)
                if reject_b:
                    reject = it("cyan", "X".ljust(7))
                optimizing = it("cyan", "OPTIMIZING".ljust(7))
                if (time() - cache["begin"]) > 200:
                    optimizing = "".ljust(7)
                # last, history, orderbook, balances, orders
                last = rpc_last(rpc, cache)
                # print(last)
                now = to_iso_date(time())
                then = to_iso_date(time() - 3 * 86400)
                ids = [cache["asset_id"], cache["currency_id"]]
                precisions = [
                    cache["asset_precision"], cache["currency_precision"]
                ]
                # CPU, RAM, io_count data REQUIRES MODULE INSTALL
                try:
                    proc = psutil_Process()
                    descriptors = proc.num_fds()
                    usage = ("grep 'cpu ' /proc/stat | awk " +
                             "'{usage=($2+$4)*100/($2+$4+$5)}" +
                             " END {print usage }' ")
                    cpu = "%.3f" % (float(popen(usage).readline()))
                    ram = "%.3f" % (100 * float(proc.memory_percent()))
                    io_count = list(proc.io_counters())[:2]
                except Exception as error:
                    if DEV:
                        print(trace(error))
                metanode = bitshares_trustless_client()
                m_last = {}
                ping = 0.5
                keys = ["bifurcating the metanode...."]
                try:
                    keys = metanode["keys"]
                    ping = storage["mean_ping"] = metanode["ping"]
                    m_last = metanode["last"]
                except BaseException:
                    pass
                del metanode
                try:
                    cex = race_read("pricefeed_cex.txt")
                    forex = race_read("pricefeed_forex.txt")
                    final = race_read("pricefeed_final.txt")
                except:
                    pass
                # aggregate gateway:bts data
                usds = []
                btcs = []
                usd_dict = {}
                btc_dict = {}
                try:
                    for key, val in m_last.items():
                        if "BTC" in key:
                            btcs.append(val)
                            btc_dict[key] = val
                        elif "USD" in key:
                            usds.append(val)
                            usd_dict[key] = val
                    # eliminate outliers
                    usd = median(usds)
                    btc = median(btcs)
                    # calculate the gateway btcusd for reference only
                    implied_btcusd = usd / btc
                except:
                    print(it("cyan", "WARN: GATHERING PRICES"))
                sceletus_output = {}
                try:
                    sceletus_output = race_read(doc="sceletus_output.txt")
                except:
                    pass
                try:
                    honest_cross_rates = race_read(
                        doc="honest_cross_rates.txt")
                except:
                    pass

                runtime = int(time()) - cache["begin"]
                # storage['bw_depth'] = max(int(len(nodes) / 6), 1)
                if (len(white) < storage["bw_depth"]) or (len(black) <
                                                          storage["bw_depth"]):
                    alert = it("cyan", "    BUILDING BLACK AND WHITE LISTS")
                else:
                    alert = ""
                if nodes == static_nodes:
                    alert += " ::WARN:: USING STATIC NODE LIST"
                upm = 0
                try:
                    upm = len(storage["updates"])
                except:
                    pass
                # in the event data passes all tests, then:
                # print, winnow the node, and nascent trend the maven
                print_market(storage, cache)
                print(keys)
                print("")
                print("runtime:epoch:pid:upm", it("green", runtime), epoch,
                      pid, upm)
                try:
                    print("fds:processes        ", descriptors, process, "of",
                          PROCESSES)
                except BaseException:
                    print("processes:           ", process, "of", PROCESSES)
                try:
                    print("cpu:ram:io_count     ", cpu, ram, io_count)
                except BaseException:
                    pass
                print("utilization:node     ", str(util + 1).ljust(3), node)
                print(
                    "total:white:black    ",
                    len(static_nodes),
                    len(nodes),
                    len(white),
                    len(black),
                    alert,
                )
                print(set_timing)
                print(
                    "block latency        ",
                    "%.2f %.1f %.1f %s %.2f" %
                    (block_latency, block_max, block_r, block_b, block_p),
                )
                print(
                    "handshake            ",
                    "%.2f %.1f %.1f %s %.2f" % (
                        handshake_latency,
                        handshake_max,
                        handshake_r,
                        handshake_b,
                        handshake_p,
                    ),
                )
                print(
                    "ping                 ",
                    "%.2f %.1f %.1f %s %.2f" %
                    (ping_latency, ping_max, ping_r, ping_b, ping_p),
                )
                print(
                    "mean ping            ",
                    (it("purple", ("%.3f" % ping))),
                    "       %s %.2f" % (reject, reject_p),
                    optimizing,
                )
                print("")
                try:
                    print(
                        "DEX BTS:BTC",
                        it("cyan", ("%.16f" % btc)),
                        it("purple", btc_dict),
                    )  # json_dump(btc_dict, indent=0, sort_keys=True)))
                    print(
                        "DEX BTS:USD",
                        "%.16f" % usd,
                        it("purple", usd_dict),
                    )
                    print(
                        "DEX BTC:USD",
                        it("yellow", ("%.4f" % implied_btcusd)),
                        "(IMPLIED)",
                    )
                except:
                    pass
                try:
                    for key, val in cex.items():
                        print(
                            "CEX",
                            key,
                            it("cyan", ("%.8f" % val["median"])),
                            {
                                k: ("%.8f" % v["last"])
                                for k, v in val["data"].items()
                            },
                        )
                    print(
                        "\nFOREX  " +
                        "inverse ::: pair ::: min ::: mid ::: max ::: qty ::: source"
                    )
                    for key, val in forex["medians"].items():
                        fxdata = [i[0] for i in forex["aggregate"][key]]
                        print(
                            it("cyan",
                               str(sigfig(1 / val[0])).rjust(12)),
                            it("green", key),
                            str(min(fxdata)).ljust(11),
                            it("cyan",
                               str(val[0]).ljust(11)),
                            str(max(fxdata)).ljust(11),
                            len(fxdata),
                            " ".join(
                                [i[1][:4] for i in forex["aggregate"][key]]),
                        )
                    print(
                        "FINAL INVERSE",
                        {k: sigfig(v)
                         for k, v in final["inverse"].items()},
                    )
                    print("FINAL FEED", it("green", final["feed"]))
                    print("FEED CLOCK", it("yellow", final["time"]))
                    try:
                        print("HONEST CROSS RATES", honest_cross_rates)
                    except:
                        pass
                    try:
                        print(
                            "SCELETUS  ",
                            it("purple", sceletus_output),
                        )
                    except:
                        pass
                    stale = time() - final["time"]["unix"]
                    if stale > 4000:
                        print(
                            it(
                                "red",
                                f"WARNING YOUR FEED IS STALE BY {stale} SECONDS"
                            ))
                except:
                    pass

                # send the maven dictionary to nascent_trend()
                # Must be JSON type
                # 'STRING', 'INT', 'FLOAT', '{DICT}', or '[LIST]'
                maven = {}
                maven["ping"] = (19 * storage["mean_ping"] +
                                 ping_latency) / 20  # FLOAT
                maven["last"] = last  # precision() STRING
                maven["whitelist"] = white  # LIST
                maven["blacklist"] = black  # LIST
                maven["blocktime"] = blocktime  # INT
                nascent_trend(maven)
                # winnow this node to the whitelist
                winnow(storage, "whitelist", node)
                # clear namespace
                del maven
                del last
                del io_count
                del alert
                del ram
                del cpu
                del keys
                del now
                del runtime
                del descriptors
                del proc
            try:
                sleep(0.0001)
                rpc.close()
            except Exception as error:
                if DEV:
                    print(trace(error))
            continue
        except Exception as error:
            try:
                if DEV:
                    print(trace(error))
                sleep(0.0001)
                rpc.close()
            except BaseException:
                pass
            try:
                msg = trace(error) + node
                if (("ValueError" not in msg)
                        and ("StatisticsError" not in msg)
                        and ("result" not in msg) and ("timeout" not in msg)
                        and ("SSL" not in msg)):
                    if (("WebSocketTimeoutException" not in msg)
                            and ("WebSocketBadStatusException" not in msg)
                            and ("WebSocketAddressException" not in msg)
                            and ("ConnectionResetError" not in msg)
                            and ("ConnectionRefusedError" not in msg)):
                        msg += "\n" + str(format_exc())
                if DEV:  # or ((time() - cache["begin"]) > 60):
                    print(msg)
                if "listed" not in msg:
                    race_append(doc="metanodelog.txt", text=msg)
                winnow(storage, "blacklist", node)
                del msg
            except BaseException:
                pass
            continue
示例#5
0
def gather_data(name, wif, trigger):
    """
    primary event loop
    """
    # purge the IPC text pipe
    race_write("pricefeed_final.txt", {})
    race_write("pricefeed_forex.txt", {})
    race_write("pricefeed_cex.txt", {})
    race_write("pricefeed_dex.txt", {})
    race_write("sceletus_output.txt", [])
    race_write("honest_cross_rates.txt", {})
    race_write("feed.txt", {})
    # begin the dex pricefeed (metanode fork)
    dex_process = Process(target=pricefeed_dex)
    dex_process.daemon = False
    dex_process.start()
    # dex_process.join(10)
    dex = {}
    # wait until the first dex pricefeed writes to file
    while dex == {}:
        dex = race_read_json("pricefeed_dex.txt")
    updates = 1
    while True:
        try:
            # collect forex and cex data
            forex = pricefeed_forex()  # takes about 30 seconds
            cex = pricefeed_cex()  # takes about 30 seconds
            # read the latest dex data
            dex = race_read_json("pricefeed_dex.txt")
            # localize forex rates
            usdcny = forex["medians"]["USD:CNY"][0]
            # usdeur = forex["medians"]["USD:EUR"][0]
            # usdgbp = forex["medians"]["USD:GBP"][0]
            # usdrub = forex["medians"]["USD:RUB"][0]
            # usdjpy = forex["medians"]["USD:JPY"][0]
            # usdkrw = forex["medians"]["USD:KRW"][0]
            usdxau = forex["medians"]["USD:XAU"][0]
            usdxag = forex["medians"]["USD:XAG"][0]
            # localize cex rates
            btcusd = cex["BTC:USD"]["median"]
            cex_btsbtc = cex["BTS:BTC"]["median"]
            cex_btsbtc_list = []
            for key, val in cex["BTS:BTC"]["data"].items():
                cex_btsbtc_list.append(val["last"])
            # attain dex BTS:BTC median
            dex_btsbtc_list = [v for k, v in dex["last"].items() if "BTC" in k]
            dex_btsbtc = median(dex_btsbtc_list)
            # finalize btsbtc by taking median of all cex and dex btsbtc prices
            btsbtc = median(dex_btsbtc_list + cex_btsbtc_list)
            # create feed prices for crypto altcoins: LTC, ETH, XRP
            # btcltc = 1/cex["LTC:BTC"]["median"]
            btceth = 1 / cex["ETH:BTC"]["median"]
            btcxrp = 1 / cex["XRP:BTC"]["median"]
            # btsltc = btsbtc * btcltc
            btseth = btsbtc * btceth
            btsxrp = btsbtc * btcxrp
            # create implied bts us dollar price
            btsusd = btsbtc * btcusd
            # create implied bts priced in forex terms
            feed = {
                "BTC:ETH": btceth,
                "BTC:XRP": btcxrp,
                "BTS:ETH": btseth,
                "BTS:XRP": btsxrp,
                "BTS:BTC": btsbtc,
                "BTS:USD": btsusd,
                "BTS:CNY": (btsusd * usdcny),
                # "BTS:EUR": (btsusd * usdeur),
                # "BTS:GBP": (btsusd * usdgbp),
                # "BTS:RUB": (btsusd * usdrub),
                # "BTS:JPY": (btsusd * usdjpy),
                # "BTS:KRW": (btsusd * usdkrw),
                "BTS:XAU": (btsusd * usdxau),
                "BTS:XAG": (btsusd * usdxag),
            }
            feed = {k: sigfig(v) for k, v in feed.items()}
            # forex priced in bts terms; switch symbol and 1/price
            inverse_feed = {
                (k[-3:] + ":" + k[:3]): sigfig(1 / v) for k, v in feed.items()
            }
            # aggregate full price calculation for jsonbin.io
            current_time = {
                "unix": int(time.time()),
                "local": time.ctime() + " " + time.strftime("%Z"),
                "utc": time.asctime(time.gmtime()) + " UTC",
                "runtime": int(time.time() - BEGIN),
                "updates": updates,
            }
            prices = {
                "time": current_time,
                "cex": cex,
                "dex": dex,
                "forex": forex,
                "inverse": inverse_feed,
                "feed": feed,
            }
            # update final output on disk
            race_write(doc="feed.txt", text=feed)
            race_write(doc="pricefeed_final.txt", text=json_dumps(prices))
            # publish feed prices to the blockchain
            if trigger["feed"] == "y":
                time.sleep(3)
                print("\n", it("red", "PUBLISHING TO BLOCKCHAIN"))
                time.sleep(5)
                publish_feed(prices, name, wif)
            # upload production data matrix to jsonbin.io
            if trigger["jsonbin"] == "y":
                time.sleep(3)
                print("\n", it("red", "UPLOADING TO JSONBIN"))
                time.sleep(5)
                update_jsonbin(prices)
            # buy/sell reference rates with two accounts
            msg = "DEMO SCELETUS REFERENCE RATES"
            if trigger["sceletus"] == "y":
                if trigger["cancel"] == "y":
                    time.sleep(3)
                    print("\n", it("red", "CANCEL ALL IN ALL MARKETS"))
                    time.sleep(5)
                    cancel_all_markets(name, wif)
                msg = msg.replace("DEMO ", "")
            time.sleep(3)
            print("\n", it("red", msg))
            time.sleep(5)
            sceletus_orders, sceletus_output = sceletus(
                prices, name, wif, trigger["sceletus"]
            )
            race_append("sceletus_orders.txt", ("\n\n" + json_dumps(sceletus_orders)))
            race_write("sceletus_output.txt", json_dumps(sceletus_output))

            appendage = (
                "\n" + str(int(time.time())) + " " + time.ctime() + " " + str(feed)
            )
            race_append(doc="feed_append.txt", text=appendage)
            updates += 1
            time.sleep(REFRESH)
        except Exception as error:
            print(error)
            time.sleep(10)  # try again in 10 seconds
示例#6
0
def create_honest_cross_rates(prices):
    """
    return a dictionary of forex/cex median cross prices for usd cny and btc
    """
    usdxau = prices["forex"]["medians"]["USD:XAU"][0]
    usdxag = prices["forex"]["medians"]["USD:XAG"][0]
    usdcny = prices["forex"]["medians"]["USD:CNY"][0]
    btcusd = prices["cex"]["BTC:USD"]["median"]
    ethbtc = prices["cex"]["ETH:BTC"]["median"]
    xrpbtc = prices["cex"]["XRP:BTC"]["median"]
    btccny = btcusd * usdcny
    usdbtc = 1 / btcusd
    cnyusd = 1 / usdcny
    cnybtc = 1 / btccny
    xagusd = 1 / usdxag
    xauusd = 1 / usdxau

    xauxag = xauusd * usdxag
    xaubtc = xauusd * usdbtc
    xagbtc = xagusd * usdbtc
    xaucny = xauusd * usdcny
    xagcny = xagusd * usdcny

    btcxau = 1 / xaubtc
    btcxag = 1 / xagbtc
    cnyxau = 1 / xaucny
    cnyxag = 1 / xagcny
    xagxau = 1 / xauxag

    btceth = 1 / ethbtc
    btcxrp = 1 / xrpbtc

    cnyeth = cnybtc * btceth
    cnyxrp = cnybtc * btcxrp
    usdeth = usdbtc * btceth
    usdxrp = usdbtc * btcxrp
    xageth = xagbtc * btceth
    xagxrp = xagbtc * btcxrp
    xaueth = xaubtc * btceth
    xauxrp = xaubtc * btcxrp

    ethcny = 1 / cnyeth
    ethusd = 1 / usdeth
    ethxag = 1 / xageth
    ethxau = 1 / xaueth
    xrpcny = 1 / cnyxrp
    xrpusd = 1 / usdxrp
    xrpxag = 1 / xagxrp
    xrpxau = 1 / xauxrp

    ethxrp = ethbtc * btcxrp
    xrpeth = 1 / ethxrp

    honest_cross_rates = {
        "CNY:USD": sigfig(cnyusd),
        "CNY:XAG": sigfig(cnyxag),
        "CNY:XAU": sigfig(cnyxau),
        "CNY:BTC": sigfig(cnybtc),
        "CNY:ETH": sigfig(cnyeth),
        "CNY:XRP": sigfig(cnyxrp),
        "USD:CNY": sigfig(usdcny),
        "USD:XAG": sigfig(usdxag),
        "USD:XAU": sigfig(usdxau),
        "USD:BTC": sigfig(usdbtc),
        "USD:ETH": sigfig(usdeth),
        "USD:XRP": sigfig(usdxrp),
        "XAU:CNY": sigfig(xaucny),
        "XAU:USD": sigfig(xauusd),
        "XAU:XAG": sigfig(xauxag),
        "XAU:BTC": sigfig(xaubtc),
        "XAU:ETH": sigfig(xaueth),
        "XAU:XRP": sigfig(xauxrp),
        "XAG:CNY": sigfig(xagcny),
        "XAG:USD": sigfig(xagusd),
        "XAG:XAU": sigfig(xagxau),
        "XAG:BTC": sigfig(xagbtc),
        "XAG:ETH": sigfig(xageth),
        "XAG:XRP": sigfig(xagxrp),
        "BTC:CNY": sigfig(btccny),
        "BTC:USD": sigfig(btcusd),
        "BTC:XAU": sigfig(btcxau),
        "BTC:XAG": sigfig(btcxag),
        "BTC:ETH": sigfig(btceth),
        "BTC:XRP": sigfig(btcxrp),
        "ETH:CNY": sigfig(ethcny),
        "ETH:USD": sigfig(ethusd),
        "ETH:XAU": sigfig(ethxau),
        "ETH:XAG": sigfig(ethxag),
        "ETH:BTC": sigfig(ethbtc),
        "ETH:XRP": sigfig(ethxrp),
        "XRP:CNY": sigfig(xrpcny),
        "XRP:USD": sigfig(xrpusd),
        "XRP:XAU": sigfig(xrpxau),
        "XRP:XAG": sigfig(xrpxag),
        "XRP:ETH": sigfig(xrpeth),
        "XRP:BTC": sigfig(xrpbtc),
    }
    race_write(doc="honest_cross_rates.txt",
               text=json_dumps(honest_cross_rates))
    return honest_cross_rates
示例#7
0
def sceletus(prices, name, wif, do_sceletus):
    """
    update the historic chart on the blockchain using buy/sell broker(order) method
    for each pair to be skeleton'd, for each agent on opposing sides of the book
    """
    orders = []
    order_dict = {}
    header = {}
    tick = prices["time"]["updates"]
    # create a list of all pairs to be skeleton'd
    pairs = create_pairs(tick)
    # websocket handshake
    rpc = reconnect(None)
    # remote procedure price of bts to Bitassets rates
    bitassets = fetch_bts_bitassets(rpc)
    account_id = ""
    if do_sceletus:
        account_id = rpc_lookup_accounts(rpc, {"account_name": name})
    # build qty_rate dict
    qty_rate = create_qty_rate(prices, bitassets)

    # each agent will place a limit order for each market pair
    for pair in pairs:
        amount = sigfig(qty_rate[pair]["qty"])
        price = sigfig(qty_rate[pair]["rate"])
        order_dict[pair] = [amount, price]
        # sort pair into dict of asset and currency
        asset = pair.split(":")[0]
        currency = pair.split(":")[1]
        pair_dict = {
            "asset": asset,
            "currency": currency,
        }
        # flash the curreny pair being bought/sold
        msg = "\033c\n\n\n"
        msg += it("red", "SCELETUS")
        msg += "\n\n\n"
        for i in range(50):
            msg += ("\n       " + asset + "  :  " + currency + "   @   " +
                    it("cyan", price) + "   qty   " + it("yellow", amount))
        print(msg)
        # make rpc for A.B.C id's and precisions
        (
            asset_id,
            asset_precision,
            currency_id,
            currency_precision,
        ) = rpc_lookup_asset_symbols(rpc, pair_dict)
        # update the header respectively
        header["asset_id"] = asset_id
        header["currency_id"] = currency_id
        header["asset_precision"] = asset_precision
        header["currency_precision"] = currency_precision
        # perform buy ops for agents[0] and sell ops for agents[1]
        for idx in range(2):
            # build the header with correct account info
            header["account_name"] = name
            header["account_id"] = account_id
            header["wif"] = wif
            operation = ["buy", "sell"][idx]
            # build the final edict for this agent*pair with operation and qty_rate
            edict = {
                "op": operation,
                "amount": amount,
                "price": price,
                "expiration": 0,
            }
            # fetch the nodes list and shuffle
            nodes = race_read_json("nodes.txt")
            random.shuffle(nodes)
            # build the order with agent appropriate header and edict for this pair
            order = {
                "header": header,
                "edicts": [edict],
                "nodes": nodes,
            }
            # print the outcome and if not a demo, then live execution
            orders.append({
                "time": time.ctime(),
                "unix": int(time.time()),
                "tick": tick,
                "name": name,
                "op": operation,
                "pair": pair,
                "price": price,
                "amount": amount,
            })

            if do_sceletus:
                broker(order)

    return orders, order_dict
示例#8
0
def create_qty_rate(prices, bitassets):
    """
    QUANTITY GENERALLY:
    CNY, USD are precision 4 :: BTS is precision 5 :: XAG,XAG,BTC are precision 8
    graphene integer currency and asset quanitites must be greater than 300
    to enacts a dust transaction with precision 0.333% or finer:
    pairs ending in BTC assets are all 0.00000300 qty
    pairs ending in XAU assets are all 0.00001800 qty
    pairs ending in XAG assets are all 0.0.0015 qty
    pairs ending in USD and CNY assets are all 0.03 qty, except BTC:CNY 0.2 qty
    pairs ending in BTS require case by case approach
    some additional quirks may become apparent with use

    RATE GENERALLY:  (XYZ:ABC * ABC:JKL) = XYZ:JKL
    """
    # calculate skeleton honest-to-honest rates
    rates = create_honest_cross_rates(prices)
    # 20 HONEST to HONEST
    honest_to_honest = {
        "HONEST.XRP:HONEST.BTC": {
            "qty": 0.3,
            "rate": rates["XRP:BTC"]
        },
        "HONEST.CNY:HONEST.BTC": {
            "qty": 0.3,
            "rate": rates["CNY:BTC"]
        },
        "HONEST.USD:HONEST.BTC": {
            "qty": 0.03,
            "rate": rates["USD:BTC"]
        },
        "HONEST.XAG:HONEST.BTC": {
            "qty": 0.0003,
            "rate": rates["XAG:BTC"]
        },
        "HONEST.ETH:HONEST.BTC": {
            "qty": 0.00003,
            "rate": rates["ETH:BTC"]
        },
        "HONEST.XAU:HONEST.BTC": {
            "qty": 0.000003,
            "rate": rates["XAU:BTC"]
        },
        "HONEST.XRP:HONEST.XAU": {
            "qty": 0.3,
            "rate": rates["XRP:XAU"]
        },
        "HONEST.CNY:HONEST.XAU": {
            "qty": 0.3,
            "rate": rates["CNY:XAU"]
        },
        "HONEST.USD:HONEST.XAU": {
            "qty": 0.03,
            "rate": rates["USD:XAU"]
        },
        "HONEST.XAG:HONEST.XAU": {
            "qty": 0.0003,
            "rate": rates["XAG:XAU"]
        },
        "HONEST.ETH:HONEST.XAU": {
            "qty": 0.00003,
            "rate": rates["ETH:XAU"]
        },
        "HONEST.BTC:HONEST.XAU": {
            "qty": 0.000003,
            "rate": rates["BTC:XAU"]
        },
        "HONEST.XRP:HONEST.XAG": {
            "qty": 0.3,
            "rate": rates["XRP:XAG"]
        },
        "HONEST.CNY:HONEST.XAG": {
            "qty": 0.3,
            "rate": rates["CNY:XAG"]
        },
        "HONEST.USD:HONEST.XAG": {
            "qty": 0.03,
            "rate": rates["USD:XAG"]
        },
        "HONEST.ETH:HONEST.XAG": {
            "qty": 0.00003,
            "rate": rates["ETH:XAG"]
        },
        "HONEST.BTC:HONEST.XAG": {
            "qty": 0.000003,
            "rate": rates["BTC:XAG"]
        },
        "HONEST.XAU:HONEST.XAG": {
            "qty": 0.000003,
            "rate": rates["XAU:XAG"]
        },
        "HONEST.XRP:HONEST.USD": {
            "qty": 0.3,
            "rate": rates["XRP:USD"]
        },
        "HONEST.CNY:HONEST.USD": {
            "qty": 0.3,
            "rate": rates["CNY:USD"]
        },
        "HONEST.XAG:HONEST.USD": {
            "qty": 0.0003,
            "rate": rates["XAG:USD"]
        },
        "HONEST.ETH:HONEST.USD": {
            "qty": 0.00003,
            "rate": rates["ETH:USD"]
        },
        "HONEST.XAU:HONEST.USD": {
            "qty": 0.000003,
            "rate": rates["XAU:USD"]
        },
        "HONEST.BTC:HONEST.USD": {
            "qty": 0.000003,
            "rate": rates["BTC:USD"]
        },
        "HONEST.XRP:HONEST.CNY": {
            "qty": 0.3,
            "rate": rates["XRP:CNY"]
        },
        "HONEST.USD:HONEST.CNY": {
            "qty": 0.03,
            "rate": rates["USD:CNY"]
        },
        "HONEST.XAG:HONEST.CNY": {
            "qty": 0.0003,
            "rate": rates["XAG:CNY"]
        },
        "HONEST.ETH:HONEST.CNY": {
            "qty": 0.00003,
            "rate": rates["ETH:CNY"]
        },
        "HONEST.XAU:HONEST.CNY": {
            "qty": 0.000003,
            "rate": rates["XAU:CNY"]
        },
        "HONEST.BTC:HONEST.CNY": {
            "qty": 0.000003,
            "rate": rates["BTC:CNY"]
        },
        "HONEST.CNY:HONEST.XRP": {
            "qty": 0.3,
            "rate": rates["CNY:XRP"]
        },
        "HONEST.USD:HONEST.XRP": {
            "qty": 0.03,
            "rate": rates["USD:XRP"]
        },
        "HONEST.XAG:HONEST.XRP": {
            "qty": 0.0003,
            "rate": rates["XAG:XRP"]
        },
        "HONEST.ETH:HONEST.XRP": {
            "qty": 0.00003,
            "rate": rates["ETH:XRP"]
        },
        "HONEST.XAU:HONEST.XRP": {
            "qty": 0.000003,
            "rate": rates["XAU:XRP"]
        },
        "HONEST.BTC:HONEST.XRP": {
            "qty": 0.000003,
            "rate": rates["BTC:XRP"]
        },
        "HONEST.CNY:HONEST.ETH": {
            "qty": 0.3,
            "rate": rates["CNY:ETH"]
        },
        "HONEST.XRP:HONEST.ETH": {
            "qty": 0.3,
            "rate": rates["XRP:ETH"]
        },
        "HONEST.USD:HONEST.ETH": {
            "qty": 0.03,
            "rate": rates["USD:ETH"]
        },
        "HONEST.XAG:HONEST.ETH": {
            "qty": 0.0003,
            "rate": rates["XAG:ETH"]
        },
        "HONEST.XAU:HONEST.ETH": {
            "qty": 0.000003,
            "rate": rates["XAU:ETH"]
        },
        "HONEST.BTC:HONEST.ETH": {
            "qty": 0.000003,
            "rate": rates["BTC:ETH"]
        },
    }
    # 5 HONEST TO BTS
    honest_to_bts = {
        "HONEST.XRP:BTS": {
            "qty": 0.3,
            "rate": prices["inverse"]["XRP:BTS"]
        },
        "HONEST.CNY:BTS": {
            "qty": 0.3,
            "rate": prices["inverse"]["CNY:BTS"]
        },
        "HONEST.USD:BTS": {
            "qty": 0.03,
            "rate": prices["inverse"]["USD:BTS"]
        },
        "HONEST.XAG:BTS": {
            "qty": 0.0003,
            "rate": prices["inverse"]["XAG:BTS"]
        },
        "HONEST.ETH:BTS": {
            "qty": 0.00003,
            "rate": prices["inverse"]["ETH:BTS"]
        },
        "HONEST.XAU:BTS": {
            "qty": 0.000003,
            "rate": prices["inverse"]["XAU:BTS"]
        },
        "HONEST.BTC:BTS": {
            "qty": 0.000003,
            "rate": prices["inverse"]["BTC:BTS"]
        },
    }
    # 5 HONEST TO GATEWAY BTC
    hcnygbtc = float(prices["inverse"]["CNY:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    husdgbtc = float(prices["inverse"]["USD:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    hxaggbtc = float(prices["inverse"]["XAG:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    hxaugbtc = float(prices["inverse"]["XAU:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    hbtcgbtc = float(prices["inverse"]["BTC:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    hxrpgbtc = float(prices["inverse"]["XRP:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    hethgbtc = float(prices["inverse"]["ETH:BTS"]) * float(
        prices["dex"]["last"]["GDEX.BTC"])
    honest_to_gdexbtc = {
        "HONEST.CNY:GDEX.BTC": {
            "qty": 0.3,
            "rate": hcnygbtc
        },
        "HONEST.USD:GDEX.BTC": {
            "qty": 0.03,
            "rate": husdgbtc
        },
        "HONEST.XAG:GDEX.BTC": {
            "qty": 0.003,
            "rate": hxaggbtc
        },
        "HONEST.XAU:GDEX.BTC": {
            "qty": 0.000003,
            "rate": hxaugbtc
        },
        "HONEST.BTC:GDEX.BTC": {
            "qty": 0.000003,
            "rate": hbtcgbtc
        },
        "HONEST.ETH:GDEX.BTC": {
            "qty": 0.00003,
            "rate": hethgbtc
        },
        "HONEST.XRP:GDEX.BTC": {
            "qty": 0.3,
            "rate": hxrpgbtc
        },
    }
    # 15 HONEST to BITASSETS: CNY, USD, BTC
    hcnycny = float(prices["inverse"]["CNY:BTS"]) * float(bitassets["BTS:CNY"])
    husdcny = float(prices["inverse"]["USD:BTS"]) * float(bitassets["BTS:CNY"])
    hxagcny = float(prices["inverse"]["XAG:BTS"]) * float(bitassets["BTS:CNY"])
    hxaucny = float(prices["inverse"]["XAU:BTS"]) * float(bitassets["BTS:CNY"])
    hbtccny = float(prices["inverse"]["BTC:BTS"]) * float(bitassets["BTS:CNY"])
    hethcny = float(prices["inverse"]["ETH:BTS"]) * float(bitassets["BTS:CNY"])
    hxrpcny = float(prices["inverse"]["XRP:BTS"]) * float(bitassets["BTS:CNY"])

    hcnyusd = float(prices["inverse"]["CNY:BTS"]) * float(bitassets["BTS:USD"])
    husdusd = float(prices["inverse"]["USD:BTS"]) * float(bitassets["BTS:USD"])
    hxagusd = float(prices["inverse"]["XAG:BTS"]) * float(bitassets["BTS:USD"])
    hxauusd = float(prices["inverse"]["XAU:BTS"]) * float(bitassets["BTS:USD"])
    hbtcusd = float(prices["inverse"]["BTC:BTS"]) * float(bitassets["BTS:USD"])
    hethusd = float(prices["inverse"]["ETH:BTS"]) * float(bitassets["BTS:USD"])
    hxrpusd = float(prices["inverse"]["XRP:BTS"]) * float(bitassets["BTS:USD"])

    hcnybtc = float(prices["inverse"]["CNY:BTS"]) * float(bitassets["BTS:BTC"])
    husdbtc = float(prices["inverse"]["USD:BTS"]) * float(bitassets["BTS:BTC"])
    hxagbtc = float(prices["inverse"]["XAG:BTS"]) * float(bitassets["BTS:BTC"])
    hxaubtc = float(prices["inverse"]["XAU:BTS"]) * float(bitassets["BTS:BTC"])
    hbtcbtc = float(prices["inverse"]["BTC:BTS"]) * float(bitassets["BTS:BTC"])
    hethbtc = float(prices["inverse"]["ETH:BTS"]) * float(bitassets["BTS:BTC"])
    hxrpbtc = float(prices["inverse"]["XRP:BTS"]) * float(bitassets["BTS:BTC"])

    honest_to_bitcny = {
        "HONEST.CNY:CNY": {
            "qty": 0.3,
            "rate": hcnycny
        },
        "HONEST.USD:CNY": {
            "qty": 0.03,
            "rate": husdcny
        },
        "HONEST.XAG:CNY": {
            "qty": 0.003,
            "rate": hxagcny
        },
        "HONEST.XAU:CNY": {
            "qty": 0.000003,
            "rate": hxaucny
        },
        "HONEST.BTC:CNY": {
            "qty": 0.000003,
            "rate": hbtccny
        },
        "HONEST.ETH:CNY": {
            "qty": 0.00003,
            "rate": hethcny
        },
        "HONEST.XRP:CNY": {
            "qty": 0.3,
            "rate": hxrpcny
        },
    }
    honest_to_bitusd = {
        "HONEST.CNY:USD": {
            "qty": 0.3,
            "rate": hcnyusd
        },
        "HONEST.USD:USD": {
            "qty": 0.03,
            "rate": husdusd
        },
        "HONEST.XAG:USD": {
            "qty": 0.003,
            "rate": hxagusd
        },
        "HONEST.XAU:USD": {
            "qty": 0.00003,
            "rate": hxauusd
        },
        "HONEST.BTC:USD": {
            "qty": 0.00003,
            "rate": hbtcusd
        },
        "HONEST.ETH:USD": {
            "qty": 0.00003,
            "rate": hethusd
        },
        "HONEST.XRP:USD": {
            "qty": 0.3,
            "rate": hxrpusd
        },
    }
    honest_to_bitbtc = {
        "HONEST.CNY:BTC": {
            "qty": 0.3,
            "rate": hcnybtc
        },
        "HONEST.USD:BTC": {
            "qty": 0.03,
            "rate": husdbtc
        },
        "HONEST.XAG:BTC": {
            "qty": 0.003,
            "rate": hxagbtc
        },
        "HONEST.XAU:BTC": {
            "qty": 0.000003,
            "rate": hxaubtc
        },
        "HONEST.BTC:BTC": {
            "qty": 0.000003,
            "rate": hbtcbtc
        },
        "HONEST.ETH:BTC": {
            "qty": 0.00003,
            "rate": hethbtc
        },
        "HONEST.XRP:BTC": {
            "qty": 0.3,
            "rate": hxrpbtc
        },
    }
    qty_rate = {}
    qty_rate.update(honest_to_bts)
    qty_rate.update(honest_to_bitcny)
    qty_rate.update(honest_to_bitusd)
    qty_rate.update(honest_to_bitbtc)
    qty_rate.update(honest_to_gdexbtc)
    qty_rate.update(honest_to_honest)

    qty_rate = {
        k: {k2: sigfig(v2)
            for k2, v2 in v.items()}
        for k, v in qty_rate.items()
    }

    # repackage dict to handle XRP1 and ETH1 as well
    qty_rate2 = {}
    for key, val in qty_rate.items():
        # include all standard standard variants
        qty_rate2[key] = val
        # handle the ETH1 variants
        if "ETH" in key:
            qty_rate2[key.replace("ETH", "ETH1")] = val
        # handle the XRP1 variants
        if "XRP" in key:
            qty_rate2[key.replace("XRP", "XRP1")] = val
        # handle the ETH1 to XRP1 crossrate and vice versa
        if ("ETH" in key) and ("XRP" in key):
            qty_rate2[key.replace("XRP", "XRP1").replace("ETH", "ETH1")] = val

    return qty_rate2
示例#9
0
def sceletus(prices, gateway, name, wif, do_sceletus):
    """
    update the historic chart on the blockchain using buy/sell broker(order) method
    for each pair to be skeleton'd, for each agent on opposing sides of the book
    """
    orders = []
    order_dict = {}
    header = {}
    # build qty_rate dict
    qty_rate = create_qty_rate(prices, gateway)
    print("\033c")
    print_logo()
    print("\n\nQTY_RATE")
    for k, v in qty_rate.items():
        print(k, v, it("cyan", sigfig(1 / v["rate"])))
    time.sleep(5)
    print(it("green", "Begin sceletus buy/sell ops..."))
    time.sleep(5)
    account_id = ""
    if do_sceletus:
        rpc = reconnect(None)
        account_id = rpc_lookup_accounts(rpc, {"account_name": name})
    for pair in qty_rate.keys():
        amount = sigfig(qty_rate[pair]["qty"])
        price = sigfig(qty_rate[pair]["rate"])
        order_dict[pair] = [amount, price]
        # sort pair into dict of asset and currency
        asset = pair.split(":")[0]
        currency = pair.split(":")[1]
        pair_dict = {
            "asset": asset,
            "currency": currency,
        }
        # flash the curreny pair being bought/sold
        msg = "\033c\n\n\n"
        msg += it("red", "SCELETUS")
        msg += "\n\n\n"
        for _ in range(50):
            msg += ("\n       " + asset + "  :  " + currency + "   @   " +
                    it("cyan", price) + "   qty   " + it("yellow", amount))
        print(msg)
        # make rpc for A.B.C id's and precisions
        (
            asset_id,
            asset_precision,
            currency_id,
            currency_precision,
        ) = rpc_lookup_asset_symbols(rpc, pair_dict)
        # update the header respectively
        header["asset_id"] = asset_id
        header["currency_id"] = currency_id
        header["asset_precision"] = asset_precision
        header["currency_precision"] = currency_precision
        # perform buy ops for agents[0] and sell ops for agents[1]
        for idx in range(2):
            # build the header with correct account info
            header["account_name"] = name
            header["account_id"] = account_id
            header["wif"] = wif
            operation = ["buy", "sell"][idx]
            # build the final edict for this agent*pair with operation and qty_rate
            edict = {
                "op": operation,
                "amount": amount,
                "price": price,
                "expiration": 0,
            }
            # fetch the nodes list and shuffle
            nodes = race_read_json("nodes.txt")
            shuffle(nodes)
            # build the order with agent appropriate header and edict for this pair
            order = {
                "header": header,
                "edicts": [edict],
                "nodes": nodes,
            }
            # print the outcome and if not a demo, then live execution
            orders.append({
                "time": time.ctime(),
                "unix": int(time.time()),
                "name": name,
                "op": operation,
                "pair": pair,
                "price": price,
                "amount": amount,
            })

            if do_sceletus:
                broker(order)
    if do_sceletus:
        print("\033c")
        print_logo()
        print(it("cyan", "OPEN ORDERS"))
        rpc_open_orders(rpc, name)

    return orders, order_dict