示例#1
0
			def reducef(x, y):
				self.ensure_type(x, ast.literal)
				self.ensure_type(y, ast.literal)
				if x.tkn != y.tkn:
					raise(runtime_error("+: arguments must have same type"))
				if x.tkn == T_INT or x.tkn == T_FLOAT:
					return ast.literal(T_INT, x.content + y.content)
				elif x.tkn == T_RATIONAL:
					x1, x2 = x.content
					y1, y2 = y.content
					z2 = lcm(x2, y2)
					z1 = x1 * (z2 / x2) + y1 * (z2 / y2)
					return ast.literal(T_RATIONAL, (int(z1), int(z2)))
				elif x.tkn == T_COMPLEX:
					x1, x2 = x.content
					y1, y2 = x.content
					return ast.literal(T_COMPLEX, (x1 + y1, x2 + y2))
示例#2
0
def race_read(doc=''):
    # Concurrent Read from File Operation
    for i in range(10):
        try:
            with open(doc, 'r') as f:
                # ret = json_loads(f.read())
                ret = literal(f.read())
                return ret
        except Exception as e:
            msg = (time.ctime() + str(type(e).__name__) + str(e.args))
            print('race_read ' + msg)
            pass
    raise
示例#3
0
def race_read(doc=''):  # Concurrent Read from File Operation

    i = 0
    while True:
        time.sleep(BLIP * i**2)
        i += 1
        try:
            with open(doc, 'r') as f:
                ret = f.read()
                f.close()
                try:
                    ret = literal(ret)
                except:
                    try:
                        ret = ret.split(']')[0] + ']'
                        ret = literal(ret)
                    except:
                        try:
                            ret = ret.split('}')[0] + '}'
                            ret = literal(ret)
                        except:
                            if '{' in ret:
                                ret = {}
                            else:
                                ret = []
                break
        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            try:
                f.close()
            except:
                pass
        finally:
            try:
                f.close()
            except:
                pass
    return ret
示例#4
0
def race_read(doc=''):  # Concurrent Read from File Operation

    opened = 0
    while not opened:
        try:
            with open(doc, 'r') as f:
                ret = literal(f.read())
                opened = 1
        except Exception as e:
            print(e, type(e).__name__, e.args)
            print(str(doc) + ' RACE READ, try again...')
            pass
    return ret
示例#5
0
def race_read(doc=''):  # Concurrent Read from File Operation

    opened = 0
    while not opened:
        try:
            with open(doc, 'r') as f:
                #ret = json.loads(f.read())
                ret = literal(f.read())
                opened = 1
        except Exception as e:
            msg = msg_(e)
            race_append(doc='microDEX_log.txt', text=msg)
            pass
    return ret
示例#6
0
def race_read(doc=''):  # Concurrent Read from File Operation

    opened = 0
    while not opened:
        try:
            with open(doc, 'r') as f:
                # ret = json.loads(f.read())
                ret = literal(f.read())
                opened = 1
        except Exception as e:
            msg = (time.ctime() + str(type(e).__name__) + str(e.args))
            print('race_read ' + msg)
            time.sleep(0.1)
            pass
    return ret
示例#7
0
def watchdog():
    """
    Allows duplex keep alive communication to metaNODE
    """
    identity = 0  # metaNODE:1, botscript:0
    max_latency = 60
    warn = " !!!!! WARNING: the other app is not responding !!!!!"
    while True:
        try:
            try:
                with open("watchdog.txt", "r") as handle:
                    ret = handle.read()
                    handle.close()
                ret = literal(ret)
                response = int(ret[identity])
                now = int(time.time())
                latency = now - response
                if identity == 0:
                    msg = str([response, now])
                if identity == 1:
                    msg = str([now, response])
                with open("watchdog.txt", "w+") as handle:
                    handle.write(msg)
                    handle.close()
                msg = str(latency)
                if latency > max_latency:
                    msg += warn
                return msg
            except Exception as error:
                msg = str(type(error).__name__) + str(error.args)
                print(msg)
                now = int(time.time())
                with open("watchdog.txt", "w+") as handle:
                    handle.write(str([now, now]))
                    handle.close()
                    break
        except Exception as error:
            msg = str(type(error).__name__) + str(error.args)
            print(msg)
            try:
                handle.close()
            except:
                pass
        finally:
            try:
                handle.close()
            except:
                pass
示例#8
0
def database_call(node, call):

    while 1:
        try:
            call = call.replace("'",'"') # never use single quotes
            ws = websocket.create_connection(node)
            print('')
            print((call.split(',"params":')[1]).rstrip('}'))
            print('-----------------------------------------------------------')
            ws.send(call)
            # 'result' key of literally evaluated
            # string representation of dictionary from websocket
            ret = literal(ws.recv())['result']
            print (ret)
            ws.close()
            return ret
        except Exception as e:
            print (e.args)
            pass
示例#9
0
def race_read(doc=''):  # Concurrent Read from File Operation

    i = 0
    while 1:
        time.sleep(0.05 * random() * i**2)
        i += 1
        try:
            with open(doc, 'r') as f:
                ret = literal(f.read())
                f.close()
                break
        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            try:
                f.close()
            except:
                pass
        finally:
            try:
                f.close()
            except:
                pass
    return ret
示例#10
0
def verify_last_price():

    while True:
        try:
            # fetch list of good nodes from file maintained by nodes.py
            try:
                opened = 0
                while not opened:
                    with open('nodes.txt', 'r') as f:
                        node_list = f.read()
                        opened = 1
            except Exception as e:
                print (e)
                print ('nodes.txt failed, try again...')
                pass
            node_list = list(literal(node_list))
            # fetch last price from 5 dex nodes
            start = time.time()
            last_list = []
            nodes_used = []
            for i in range(len(node_list)):
                if len(last_list) < 5:
                    try:
                        m = market(node_list[i])
                        ret = satoshi(dex_last(m))
                        last_list.append(ret)
                        nodes_used.append(node_list[i])
                    except:
                        pass
            # calculate relative range
            rrange = (max(last_list) - min(last_list)) / mean(last_list)
            # check last list and return best last price with message
            msg = ''
            if len(set(last_list)) == 1:
                last = last_list[-1]
                msg += 'common'
            else:
                try:
                    last = mode(last_list)
                    msg += 'mode'
                except:
                    last = median(last_list)
                    msg += 'median'
                # override median or mode with latest if less than 2%
                # difference
                if rrange < 0.02:
                    last = last_list[-1]
                    msg = 'latest (' + msg + ')'
                else:
                    # create blacklist.txt if relative range too wide
                    print('BLACKLIST last ' + 
                          str(last) + ' ' + str(rrange))
                    print(str(last_list))
                    print(str(nodes_used))
                    try:
                        opened = 0
                        while not opened:
                            with open('blacklist.txt', 'a+') as file:
                                file.write("\n" + 'BLACKLIST last ' + 
                                    str(last) + ' ' + str(rrange))
                                file.write("\n" + str(last_list))
                                file.write("\n" + str(nodes_used))
                                opened = 1
                    except Exception as e:
                        print (e)
                        print ('blacklist.txt failed, try again...')
                        pass
            # maintain a log of last price, relative range, and statistics type
            last = satoshi(last)
            elapsed = '%.1f' % (time.time() - start)
            print (('%.8f' % last), clock(), 'elapsed: ', elapsed,
                   'nodes: ', len(last_list), 'type: ', ('%.3f' % rrange), msg)
            # update communication file last.txt
            try:
                opened = 0
                while not opened:
                    with open('last.txt', 'w+') as file:
                        file.write(str(last))
                        opened = 1
            except Exception as e:
                print (e)
                print ('last.txt failed, try again...')
                pass
        # no matter what happens just keep attempting to update last price
        except Exception as e:
            print (e)
            pass
        time.sleep(30)
示例#11
0
def dex(  # Public AND Private API Bitshares
        command, amount=ANTISAT, price=None,
        depth=1, expiration=ANTISAT):

    MARKET = Market(BitPAIR, bitshares_instance=BitShares(nodes(), num_retries=0))
    CHAIN = Blockchain(bitshares_instance=BitShares(nodes(), num_retries=0), mode='head')
    #MARKET.bitshares.wallet.unlock(PASS_PHRASE)
    ACCOUNT.refresh()

    if command == 'buy':

        # buy relentlessly until satisfied or currency exhausted
        print(('Bitshares API', command))
        if price is None:
            price = ANTISAT
        print(('buying', amount, 'at', price))
        attempt = 1
        currency = float(ACCOUNT.balance(BitCURRENCY))
        if amount > 0.998 * currency * price:
            amount = 0.998 * currency * price
        if amount > 0:
            while attempt:
                try:
                    details = (MARKET.buy(price, amount, expiration))
                    print (details)
                    attempt = 0
                except:
                    print(("buy attempt %s failed" % attempt))
                    attempt += 1
                    if attempt > 10:
                        print ('buy aborted')
                        return
                    pass
        else:
            print('no currency to buy')

    if command == 'sell':

        # sell relentlessly until satisfied or assets exhausted
        expiration = 86400 * 7
        print(('Bitshares API', command))
        if price is None:
            price = SATOSHI
        print(('selling', amount, 'at', price))
        attempt = 1
        assets = float(ACCOUNT.balance(BitASSET))
        if amount > 0.998 * assets:
            amount = 0.998 * assets
        if amount > 0:
            while attempt:
                try:
                    details = (MARKET.sell(price, amount, expiration))
                    print (details)
                    attempt = 0
                except:
                    print(("sell attempt %s failed" % attempt))
                    attempt += 1
                    if attempt > 10:
                        print ('sell aborted')
                        return
                    pass
        else:
            print('no assets to sell')

    if command == 'cancel':

        # cancel all orders in this MARKET relentlessly until satisfied
        print(('Bitshares API', command))  
        orders = MARKET.accountopenorders()
        print((len(orders), 'open orders to cancel'))
        if len(orders):
            attempt = 1   
            order_list = []      
            for order in orders:
                order_list.append(order['id'])
            while attempt:
                try:
                    details = MARKET.cancel(order_list)
                    print (details)
                    attempt = 0
                except:
                    print((attempt, 'cancel failed', order_list))
                    attempt += 1
                    if attempt > 10:
                        print ('cancel aborted')
                        return
                    pass    

    if command == 'orders':

        servers = nodes()
        orders_list =[]
        satisfied = 0
        while not satisfied: #while len set triplicate
            for n in servers:
                sorders = [str(i) for i in orders_list]
                if (len(sorders) >= 3) and len(set(sorders[-3:])) == 1:
                    orders = orders_list[-1]
                    satisfied = 1
                else:
                    MARKET = Market(BitPAIR, bitshares_instance=BitShares(n, num_retries=0))
                    MARKET.bitshares.wallet.unlock(PASS_PHRASE)
                    ACCOUNT.refresh()

                    # dictionary of open orders in traditional format:
                    # orderNumber, orderType, market, amount, price
                    print(('Bitshares API', command))
                    orders = []
                    for order in MARKET.accountopenorders():
                        orderNumber = order['id']
                        asset = order['base']['symbol']
                        currency = order['quote']['symbol']
                        amount = float(order['base'])
                        price = float(order['price'])
                        orderType = 'buy'
                        if asset == BitASSET:
                            orderType = 'sell'
                            price = 1 / price
                        orders.append({'orderNumber': orderNumber,
                                       'orderType': orderType,
                                       'market': BitPAIR, 'amount': amount,
                                       'price': price})
                    orders_list.append(orders)


        for o in orders:
            print (o)
        if len(orders) == 0:
            print ('no open orders')
        return orders

    if command == 'market_balances':

        # dictionary of currency and assets in this MARKET
        print(('Bitshares API', command))
        currency = float(ACCOUNT.balance(BitCURRENCY))
        assets = float(ACCOUNT.balance(BitASSET))
        balances = {'currency': currency, 'assets': assets}
        print (balances)
        return balances

    if command == 'complete_balances':

        # dictionary of ALL account balances
        print(('Bitshares API', command))
        raw = list(ACCOUNT.balances)
        balances = {}
        for i in range(len(raw)):
            balances[raw[i]['symbol']] = float(raw[i]['amount'])
        print (balances)
        return balances

    if command == 'book':

        try:
            opened = 0
            while not opened:
                with open('book.txt', 'r') as f:
                    book = f.read()
                    opened = 1
        except Exception as e:
            print (e)
            print ('book.txt failed, try again...')
            pass
        return literal(book)

    if command == 'last':

        try:
            opened = 0
            while not opened:
                with open('last.txt', 'r') as f:
                    last = f.read()
                    opened = 1
        except Exception as e:
            print (e)
            print ('last.txt failed, try again...')
            pass
        return literal(last)

    if command == 'account_value':

        # dictionary account value in BTS BTC and USD
        print(('Bitshares API', command))
        raw = list(ACCOUNT.balances)
        balances = {}
        for i in range(len(raw)):
            balances[raw[i]['symbol']] = float(raw[i]['amount'])
        btc_value = 0
        for asset, amount in list(balances.items()):
            market_pair = 'OPEN.BTC:' + asset
            market = Market(market_pair)
            price = float(market.ticker()['latest'])
            try:
                value = amount / price
            except:
                value = 0
            if value < 0.0001:
                value = 0
            else:
                if asset != 'USD':
                    price = 1 / (price + SATOSHI)
                print((('%.4f' % value), 'OPEN.BTC', ('%.2f' % amount),
                       asset, '@', ('%.8f' % price)))
                btc_value += value

        market_pair = 'OPEN.BTC:USD'
        market = Market(market_pair)
        price = float(market.ticker()['latest'])
        usd_value = btc_value * price
        market_pair = 'OPEN.BTC:BTS'
        market = Market(market_pair)
        price = float(market.ticker()['latest'])
        bts_value = btc_value * price
        print((('%.2f' % bts_value), 'BTS',
             ('%.4f' % btc_value), 'OPEN.BTC',
             ('%.2f' % usd_value), 'bitUSD'))
        return bts_value, btc_value, usd_value

    if command == 'blocktime':

        current_block = CHAIN.get_current_block_num()
        blocktime = CHAIN.block_time(current_block)
        blocktimestamp = CHAIN.block_timestamp(current_block) - 18000
        now = time.time()
        latency = now - blocktimestamp
        print(('block               :', current_block))
        # print(('blocktime           :', blocktime))
        # print(('stamp               :', blocktimestamp))
        # print(('ctime(stamp)        :', time.ctime(blocktimestamp)))
        # print(('now                 :', now))
        print(('dex_rate latency    :', ('%.2f' % latency)))
        return current_block, blocktimestamp, latency
def variable_get(varId,type=str):
    if type in (bool, int, float, str):
        return indigo.variables[int(varId)].getValue(type)
    else:
        return literal(indigo.variables[int(varId)].value)
示例#13
0
def watchdog():

    identity = 0  # 1 or 0
    max_latency = 10

    def on_error():
        print('WARNING: the other app is not responding !!!!!')
        bell()
        time.sleep(4)
        watchdog()

    def bell(duration=2000, frequency=432):  # ms / hz = linux alert bell
        # SoX must be installed using 'sudo apt-get install sox' in the terminal
        try:
            os.system('play --no-show-progress --null --channels 1 synth' +
                      ' %s sine %f' % (duration / 1000, frequency))
        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            pass

    def now():
        return int(time.time())

    while 1:
        try:
            try:
                with open('watchdog.txt', 'r') as f:
                    ret = f.read()
                    f.close()
                ret = literal(ret)
                response = int(ret[identity])
                print(ret)
                if identity == 1:
                    msg = str([now(), response])
                if identity == 0:
                    msg = str([response, now()])
                with open('watchdog.txt', 'w+') as f:
                    f.write(msg)
                    f.close()
                latency = now() - response
                if latency > max_latency:
                    on_error()
                break  # exit while loop
            except Exception as e:
                msg = str(type(e).__name__) + str(e.args)
                print(msg)
                with open('watchdog.txt', 'w+') as f:
                    f.write(str([now(), now()]))
                    f.close()
                    break  # exit while loop
        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            try:
                f.close()
            except:
                pass
        finally:
            try:
                f.close()
            except:
                pass
示例#14
0
def bifurcation():  # statistically curate data

    while True:
        try:
            time.sleep(1)

            mavens = race_read(doc='mavens.txt')

            l = len(mavens)

            # initialize lists to sort data from each maven by key
            bidp = []
            askp = []
            bidv = []
            askv = []
            currency_balance = []
            asset_balance = []
            history = []
            last = []
            whitelist = []
            blacklist = []
            blocktime = []
            # initialize the metaNODE dictionary
            metaNODE = {}

            # sort maven data for statistical analysis by key
            for i in range(len(mavens)):
                maven = literal(mavens[i])
                currency_balance.append(maven['currency_balance'])
                asset_balance.append(maven['asset_balance'])
                last.append(maven['last'])
                blocktime.append(maven['blocktime'])
                whitelist.append(maven['whitelist'])
                blacklist.append(maven['blacklist'])
                # stringify lists for statistical mode
                bidp.append(str(maven['bidp']))
                askp.append(str(maven['askp']))
                bidv.append(str(maven['bidv']))
                askv.append(str(maven['askv']))
                history.append(str(maven['market_history']))

            # find the oldest bitshares blocktime in our dataset
            blocktime = min(blocktime)
            # get the mode of the mavens for each metric
            # allow 1 or 2 less than total & most recent for mode
            # accept "no mode" statistics error as possibility
            try:
                currency_balance = mode(currency_balance)
            except:
                try:
                    currency_balance = mode(currency_balance[-(l-1):])
                except:
                    currency_balance = mode(currency_balance[-(l-2):])
            try:
                asset_balance = mode(asset_balance)
            except:
                try:
                    asset_balance = mode(asset_balance[-(l-1):])
                except:
                    asset_balance = mode(asset_balance[-(l-2):])
            try:
                last = mode(last)
            except:
                try:
                    last = mode(last[-(l-1):])
                except:
                    last = mode(last[-(l-2):])
            try:
                bidp = literal(mode(bidp))
            except:
                try:
                    bidp = literal(mode(bidp[-(l-1):]))
                except:
                    bidp = literal(mode(bidp[-(l-2):]))
            try:
                askp = literal(mode(askp))
            except:
                try:
                    askp = literal(mode(askp[-(l-1):]))
                except:
                    askp = literal(mode(askp[-(l-2):]))
            try:
                bidv = literal(mode(bidv))
            except:
                try:
                    bidv = literal(mode(bidv[-(l-1):]))
                except:
                    bidv = literal(mode(bidv[-(l-2):]))
            try:
                askv = literal(mode(askv))
            except:
                try:
                    askv = literal(mode(askv[-(l-1):]))
                except:
                    askv = literal(mode(askv[-(l-2):]))

            try:
                history = literal(mode(history))
            except:
                try:
                    history = literal(mode(history[-(l-1):]))
                except:
                    history = literal(mode(history[-(l-2):]))

            # attempt a full whitelist and blacklist
            wl = []
            for i in whitelist:
                wl += i
            whitelist = list(set(wl))[-WHITE:]
            bl = []
            for i in blacklist:
                bl += i
            blacklist = list(set(bl))[-BLACK:]

            # if you made it this far without statistics error
            # populate the metaNODE with curated data
            metaNODE['bidp'] = bidp
            metaNODE['askp'] = askp
            metaNODE['bidv'] = bidv
            metaNODE['askv'] = askv
            metaNODE['currency_balance'] = currency_balance
            metaNODE['asset_balance'] = asset_balance
            metaNODE['market_history'] = history
            metaNODE['last'] = last
            metaNODE['whitelist'] = whitelist
            metaNODE['blacklist'] = blacklist
            metaNODE['blocktime'] = blocktime
            metaNODE['account_name'] = account_name
            metaNODE['account_id'] = account_id
            metaNODE['asset'] = asset
            metaNODE['asset_id'] = asset_id
            metaNODE['asset_precision'] = asset_precision
            metaNODE['currency'] = currency
            metaNODE['currency_id'] = currency_id
            metaNODE['currency_precision'] = currency_precision

            # solitary process with write access to metaNODE.txt            
            race_write(doc='metaNODE.txt', text=metaNODE)
        
        except Exception as e: # wait a second and try again
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            race_append(doc='metaNODElog.txt', text=msg)
            pass
示例#15
0
def bifurcation():  # statistically curate data

    while True:
        try:
            time.sleep(1)

            mavens = race_read(doc='mavens.txt')

            l = len(mavens)

            # initialize lists to sort data from each maven by key
            bidp = []
            askp = []
            bidv = []
            askv = []
            currency_balance = []
            asset_balance = []
            history = []
            last = []
            whitelist = []
            blacklist = []
            blocktime = []
            orders = []
            # initialize the metaNODE dictionary
            metaNODE = {}

            # sort maven data for statistical analysis by key
            for i in range(len(mavens)):
                maven = literal(mavens[i])
                currency_balance.append(maven['currency_balance'])
                asset_balance.append(maven['asset_balance'])
                last.append(maven['last'])
                blocktime.append(maven['blocktime'])
                whitelist.append(maven['whitelist'])
                blacklist.append(maven['blacklist'])
                # stringify lists for statistical mode
                bidp.append(str(maven['bidp']))
                askp.append(str(maven['askp']))
                bidv.append(str(maven['bidv']))
                askv.append(str(maven['askv']))
                history.append(str(maven['market_history']))
                orders.append(str(maven['orders']))

            # find the oldest bitshares blocktime in our dataset
            blocktime = min(blocktime)
            # get the mode of the mavens for each metric
            # allow 1 or 2 less than total & most recent for mode
            # accept "no mode" statistics error as possibility
            try:
                currency_balance = mode(currency_balance)
            except:
                try:
                    currency_balance = mode(currency_balance[-(l - 1):])
                except:
                    currency_balance = mode(currency_balance[-(l - 2):])
            try:
                asset_balance = mode(asset_balance)
            except:
                try:
                    asset_balance = mode(asset_balance[-(l - 1):])
                except:
                    asset_balance = mode(asset_balance[-(l - 2):])
            try:
                last = mode(last)
            except:
                try:
                    last = mode(last[-(l - 1):])
                except:
                    last = mode(last[-(l - 2):])
            try:
                bidp = literal(mode(bidp))
            except:
                try:
                    bidp = literal(mode(bidp[-(l - 1):]))
                except:
                    bidp = literal(mode(bidp[-(l - 2):]))
            try:
                askp = literal(mode(askp))
            except:
                try:
                    askp = literal(mode(askp[-(l - 1):]))
                except:
                    askp = literal(mode(askp[-(l - 2):]))
            try:
                bidv = literal(mode(bidv))
            except:
                try:
                    bidv = literal(mode(bidv[-(l - 1):]))
                except:
                    bidv = literal(mode(bidv[-(l - 2):]))
            try:
                askv = literal(mode(askv))
            except:
                try:
                    askv = literal(mode(askv[-(l - 1):]))
                except:
                    askv = literal(mode(askv[-(l - 2):]))

            try:
                history = literal(mode(history))
            except:
                try:
                    history = literal(mode(history[-(l - 1):]))
                except:
                    history = literal(mode(history[-(l - 2):]))

            try:
                orders = literal(mode(orders))
            except:
                try:
                    orders = literal(mode(orders[-(l - 1):]))
                except:
                    orders = literal(mode(orders[-(l - 2):]))

            # attempt a full whitelist and blacklist
            wl = []
            for i in whitelist:
                wl += i
            whitelist = list(set(wl))[-WHITE:]
            bl = []
            for i in blacklist:
                bl += i
            blacklist = list(set(bl))[-BLACK:]

            # rebuild orderbook as 4 key dict with lists of floats
            bidp = [float(i) for i in bidp]
            bidv = [float(i) for i in bidv]
            askp = [float(i) for i in askp]
            askv = [float(i) for i in askv]
            book = {'bidp': bidp, 'bidv': bidv, 'askp': askp, 'askv': askv}

            # if you made it this far without statistics error
            # truncate and rewrite the metaNODE with curated data
            metaNODE['book'] = book
            metaNODE['currency_balance'] = float(currency_balance)
            metaNODE['asset_balance'] = float(asset_balance)
            metaNODE['history'] = history  #LIST
            metaNODE['orders'] = orders  #LIST
            metaNODE['last'] = float(last)
            metaNODE['whitelist'] = whitelist  #LIST
            metaNODE['blacklist'] = blacklist  #LIST
            metaNODE['blocktime'] = float(blocktime)
            metaNODE['account_name'] = account_name  #STRING
            metaNODE['account_id'] = account_id  #STRING A.B.C
            metaNODE['asset'] = asset  #STRING SYMBOL
            metaNODE['asset_id'] = asset_id  #STRING A.B.C
            metaNODE['asset_precision'] = int(asset_precision)
            metaNODE['currency'] = currency  #STRING SYMBOL
            metaNODE['currency_id'] = currency_id  #STRING A.B.C
            metaNODE['currency_precision'] = int(currency_precision)

            # solitary process with write access to metaNODE.txt
            race_write(doc='metaNODE.txt', text=metaNODE)
            print('metaNODE.txt updated')

        except Exception as e:  # wait a second and try again
            # common msg is "no mode statistics error"
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            race_append(doc='metaNODElog.txt', text=msg)
            continue  # from top of while loop NOT pass through error
示例#16
0
 def _do_slap(self, command, targets="", definitions={}):
     cmds = literal("'" + command.replace("\'", "\\\'") + "'")
     replacements = self._define_replacements(targets, definitions)
     cmds = self._format_command(cmds, replacements)
     for cmd in cmds.split("\n"):
         self.exec_command(cmd)
示例#17
0
def verify_book():

    tally = {'triple': 0, 'mode': 0, 'median': 0, 'built': 0}
    while True:

        try:
            # fetch list of good nodes from file maintained by nodes.py
            try:
                opened = 0
                while not opened:
                    with open('nodes.txt', 'r') as f:
                        node_list = f.read()
                        opened = 1
            except Exception as e:
                print(e)
                print('nodes.txt failed, try again...')
                pass
            node_list = list(literal(node_list))

            # fetch last price from 5 dex nodes
            start = time.time()
            middles = []
            book_list = []
            nodes_used = []
            test = []
            msg = ''
            for i in range(len(node_list)):
                triplicate = 0
                if (len(book_list) < NODES) and not triplicate:

                    try:
                        m = market(node_list[i])
                        ret = dex_book(m)
                        if FUCK_BOOK:
                            ret = fuck_book(ret)
                        #print(ret)
                        book_list.append(ret)
                        nodes_used.append(node_list[i])
                        test.append(i)

                    except:
                        pass
                    sbooks = [str(i) for i in book_list]

                    #print ((test[:3]))
                    #print ((test[-3:]))
                    if (len(sbooks) >= 3) and len(set(sbooks[-3:])) == 1:
                        book = book_list[-1]
                        asksort = sorted(book['askp'])
                        bidsort = sorted(book['bidp'], reverse=True)
                        if ((asksort == book['askp'])
                                and (bidsort == book['bidp'])
                                and (len(set(asksort)) == len(asksort))
                                and (len(set(bidsort)) == len(bidsort))
                                and (bidsort[0] < asksort[0])):
                            msg += 'triplicate book'
                            triplicate = 1
                            tally['triple'] += 1
                            break
                        else:
                            msg += 'triplicate book error - '

            #book_list = [{'askp': [0.14779781, 0.14779781, 0.14779781, 0.14779781], 'bidv': [530.3, 67.7],
            #    'bidp': [0.14779781, 0.14779781, 0.14779781, 0.14779781], 'askv': [10044.5, 420.0]},]

            if triplicate == 0:
                # check last list and return best last price with message
                try:
                    book = literal(mode([str(i) for i in book_list]))
                    asksort = sorted(book['askp'])
                    bidsort = sorted(book['bidp'], reverse=True)
                    if (asksort != book['askp']):
                        print('asksort')
                    if (bidsort != book['bidp']):
                        print('bidsort')
                    if (len(set(asksort)) != len(asksort)):
                        print('askmatch')
                    if (len(set(bidsort)) != len(bidsort)):
                        print('bidmatch')
                    if (bidsort[0] > asksort[0]):
                        print('mismatched')
                    if ((asksort == book['askp']) and (bidsort == book['bidp'])
                            and (len(set(asksort)) == len(asksort))
                            and (len(set(bidsort)) == len(bidsort))
                            and (bidsort[0] < asksort[0])):
                        msg += 'mode book'
                        tally['mode'] += 1
                    else:
                        raise
                except:

                    book = {
                        i: list(np.median([x[i] for x in book_list], axis=0))
                        for i in ['bidp', 'bidv', 'askp', 'askv']
                    }
                    asksort = sorted(book['askp'])
                    bidsort = sorted(book['bidp'], reverse=True)
                    if (asksort != book['askp']):
                        print('asksort')
                    if (bidsort != book['bidp']):
                        print('bidsort')
                    if (len(set(asksort)) != len(asksort)):
                        print('askmatch')
                    if (len(set(bidsort)) != len(bidsort)):
                        print('bidmatch')
                    if (bidsort[0] > asksort[0]):
                        print('mismatched')
                    if ((asksort == book['askp']) and (bidsort == book['bidp'])
                            and (len(set(asksort)) == len(asksort))
                            and (len(set(bidsort)) == len(bidsort))
                            and (bidsort[0] < asksort[0])):
                        msg += '!!! MEDIAN BOOK !!!'
                        tally['median'] += 1

                    else:
                        print((book['bidp'][:3])[::-1], 'BIDS <> ASKS',
                              book['askp'][:3], 1)
                        msg += '!!! RECONSTRUCTED BOOK !!!                  *****'
                        tally['built'] += 1
                        # assure median comprehension did not reorganize book
                        # prices
                        prices = []
                        prices = prices + book['askp'] + book['bidp']
                        prices = sorted(prices)
                        z = len(
                            prices
                        )  # will except if odd... shouldn't be, so let it fail
                        book['askp'] = prices[int(z / 2):z]
                        book['bidp'] = prices[0:int(z / 2)]
                        book['askp'] = sorted(book['askp'])
                        book['bidp'] = sorted(book['bidp'], reverse=True)
                        print((book['bidp'][:3])[::-1], 'BIDS <> ASKS',
                              book['askp'][:3], 2)
                        if book['bidp'][0] == book['askp'][0]:
                            book['askp'] = [(i + DFLOAT) for i in book['askp']]
                            book['bidp'] = [(i - DFLOAT) for i in book['bidp']]
                        print((book['bidp'][:3])[::-1], 'BIDS <> ASKS',
                              book['askp'][:3], 3)
                        for i in list(range(1, len(book['askp']))):
                            if book['askp'][i] <= book['askp'][i - 1]:
                                book['askp'][i] = max(
                                    (book['askp'][i - 1] + DFLOAT),
                                    book['askp'][i])
                        print((book['bidp'][:3])[::-1], 'BIDS <> ASKS',
                              book['askp'][:3], 4)
                        for i in list(range(1, len(book['bidp']))):
                            if book['bidp'][i] >= book['bidp'][i - 1]:
                                book['bidp'][i] = min(
                                    book['bidp'][i - 1] - DFLOAT,
                                    book['bidp'][i])
                        print((book['bidp'][:3])[::-1], 'BIDS <> ASKS',
                              book['askp'][:3], 5)
                        print(
                            '===================================================================='
                        )

            rrange = sum(book['bidp']) + sum(book['askp'])

            # maintain a log of last price, relative range, and statistics type
            elapsed = '%.1f' % (time.time() - start)

            #print (book['bidp'][::-1], 'BIDS <> ASKS', book['askp'])
            print((book['bidp'][:3])[::-1], 'BIDS <> ASKS', book['askp'][:3])
            try:

                s = sum(tally.values())
                ptally = {k: ('%.2f' % (v / s)) for k, v in tally.items()}
                #print (tally)
                print(clock(), ptally, 'elapsed: ', elapsed, 'nodes: ',
                      len(book_list), 'type: ', ('%.3f' % rrange), msg)
                print(
                    '===================================================================='
                    +
                    '===================================================================='
                )
            except:
                pass
            # update communication file book.txt
            try:
                opened = 0
                while not opened:
                    with open('book.txt', 'w+') as file:
                        file.write(str(book))
                        opened = 1
            except Exception as e:
                print(e)
                print('book.txt failed, try again...')
                pass
        #no matter what happens just keep attempting to update last price
        except Exception as e:
            print(e)
            print('verify_book failed')
            pass
        time.sleep(30)
示例#18
0
def bifurcation():  # statistically curate data

    while True:
        try:
            time.sleep(1)

            mavens = race_read(doc='mavens.txt')

            l = len(mavens)

            bids = []
            asks = []
            currency_balance = []
            asset_balance = []
            history = []
            last = []
            whitelist = []
            blacklist = []
            blocktime = []

            metaNODE = {}

            for i in range(len(mavens)):
                maven = literal(mavens[i])

                currency_balance.append(maven['currency_balance'])
                asset_balance.append(maven['asset_balance'])
                last.append(maven['last'])
                blocktime.append(maven['blocktime'])

                bids.append(str(maven['bids']))
                asks.append(str(maven['asks']))
                history.append(str(maven['market_history']))

                whitelist.append(maven['whitelist'])
                blacklist.append(maven['blacklist'])

            blocktime = min(blocktime)
            # get the mode of the mavens; allow 1 less than total
            # accept "no mode" statistics error as possibility
            try:
                currency_balance = mode(currency_balance)
            except:
                try:
                    currency_balance = mode(currency_balance[-(l - 1):])
                except:
                    currency_balance = mode(currency_balance[-(l - 2):])
            try:
                asset_balance = mode(asset_balance)
            except:
                try:
                    asset_balance = mode(asset_balance[-(l - 1):])
                except:
                    asset_balance = mode(asset_balance[-(l - 2):])
            try:
                last = mode(last)
            except:
                try:
                    last = mode(last[-(l - 1):])
                except:
                    last = mode(last[-(l - 2):])
            try:
                bids = literal(mode(bids))
            except:
                try:
                    bids = literal(mode(bids[-(l - 1):]))
                except:
                    bids = literal(mode(bids[-(l - 2):]))
            try:
                asks = literal(mode(asks))
            except:
                try:
                    asks = literal(mode(asks[-(l - 1):]))
                except:
                    asks = literal(mode(asks[-(l - 2):]))
            try:
                history = literal(mode(history))
            except:
                try:
                    history = literal(mode(history[-(l - 1):]))
                except:
                    history = literal(mode(history[-(l - 2):]))

            # attempt a full whitelist and blacklist
            wl = []
            for i in whitelist:
                wl += i
            whitelist = list(set(wl))[-WHITE:]
            bl = []
            for i in blacklist:
                bl += i
            blacklist = list(set(bl))[-BLACK:]

            metaNODE['bids'] = bids
            metaNODE['asks'] = asks
            metaNODE['currency_balance'] = currency_balance
            metaNODE['asset_balance'] = asset_balance
            metaNODE['market_history'] = history
            metaNODE['last'] = last
            metaNODE['whitelist'] = whitelist
            metaNODE['blacklist'] = blacklist
            metaNODE['blocktime'] = blocktime

            race_write(doc='metaNODE.txt', text=metaNODE)

        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print(msg)
            race_append(doc='metaNODElog.txt', text=msg)
            pass
示例#19
0
def bifurcation():  # statistically curate data

    while 1:
        try:
            time.sleep(1)

            mavens = race_read(doc='mavens.txt')

            bids = []
            asks = []
            currency_balance = []
            asset_balance = []
            history = []
            last = []
            whitelist = []
            blacklist = []
            blocktime = []

            metaNODE = {}

            for i in range(len(mavens)):
                maven = literal(mavens[i])

                currency_balance.append(maven['currency_balance'])
                asset_balance.append(maven['asset_balance'])
                last.append(maven['last'])
                blocktime.append(maven['blocktime'])

                bids.append(str(maven['bids']))
                asks.append(str(maven['asks']))
                history.append(str(maven['market_history']))

                whitelist.append(maven['whitelist'])
                blacklist.append(maven['blacklist'])

            currency_balance = mode(currency_balance)
            asset_balance = mode(asset_balance)
            last = mode(last)
            blocktime = min(blocktime)

            bids = literal(mode(bids))
            asks = literal(mode(asks))
            history = literal(mode(history))

            wl = []
            for i in whitelist:
                wl += i
            whitelist = list(set(wl))[-WHITE:]
            bl = []
            for i in blacklist:
                bl += i
            blacklist = list(set(bl))[-BLACK:]

            metaNODE['bids'] = bids
            metaNODE['asks'] = asks
            metaNODE['currency_balance'] = currency_balance
            metaNODE['asset_balance'] = asset_balance
            metaNODE['market_history'] = history
            metaNODE['last'] = last
            metaNODE['whitelist'] = whitelist
            metaNODE['blacklist'] = blacklist
            metaNODE['blocktime'] = blocktime

            race_write(doc='metaNODE.txt', text=metaNODE)

        except Exception as e:
            msg = str(type(e).__name__) + str(e.args)
            print('bifurcation WARNING', msg)
            pass