Ejemplo n.º 1
0
 def _store_db(self, price):
     logger.info('store price - %0.2f' % (price))
     sql = 'INSERT INTO t_price_history (price, updatetime) VALUES (%0.2f, %d);' % (price, int(time.time()))
     ret, rec = self._db.execute(sql)
     if not ret:
         logger.error('store price error - %0.2f' % (price))
     pass
Ejemplo n.º 2
0
 def _getDataFromURL(self, index):
     try:
         textmod = {'market': index}
         textmod = urllib.urlencode(textmod)
         url = '%s%s%s' % (self._url, '?', textmod)
         req = urllib2.Request(url)
         response = urllib2.urlopen(req, timeout=self._request_timeout)
         res = response.read()
         data = json.loads(res)
         buy_value = data['ticker']['buy']  # buy value
         high_value = data['ticker']['high']  #24h high value
         last_value = data['ticker']['last']  #last value
         low_value = data['ticker']['low']  #24h low value
         sell_value = data['ticker']['sell']  #sell value
         vol_value = data['ticker']['vol']  #24Сʱ�ɽ���
         json_body = [{
             "measurement": "ZB",
             "tags": {
                 "coin": index,
                 "index": index
             },
             "fields": {
                 "buy": float(buy_value),
                 "high": float(high_value),
                 "last": float(last_value),
                 "low": float(low_value),
                 "sell": float(sell_value),
                 "vol": float(vol_value)
             }
         }]
         self.client.write_points(json_body)
     except urllib2.HTTPError, e:
         logger.error('HTTP Error: %d\t%s\t%s\t%s' %
                      (e.code, e.reason, e.geturl(), e.read()))
 def _wget(self):
     ret = False
     data = None
     try:
         textmod = ''
         #textmod = urllib.urlencode(textmod)
         req = urllib2.Request(url='%s%s' % (self._url, textmod))
         response = urllib2.urlopen(req, timeout=10)
         res = response.read()
         data = json.loads(res)
         query = 'select last(value) from Exchange where index=\'JPY\''
         result = self.client.query(query)
         exchange_value = result.raw['series'][0]['values'][0][1]
         for index in self.ticker_index:
             value = data['ask']
             json_body = [{
                 "measurement": "Bitflyer",
                 "tags": {
                     "coin": index,
                     "index": index
                 },
                 "fields": {
                     "buy": float(value) / exchange_value,
                     "buy_jpy": float(value)
                 }
             }]
             self.client.write_points(json_body)
         ret = True
     except urllib2.HTTPError, e:
         logger.error('HTTP Error: %d\t%s\t%s\t%s' %
                      (e.code, e.reason, e.geturl(), e.read()))
Ejemplo n.º 4
0
 def _store_db(self, price):
     logger.info('store price - %0.2f' % (price))
     sql = 'INSERT INTO t_price_history (price, updatetime) VALUES (%0.2f, %d);' % (
         price, int(time.time()))
     ret, rec = self._db.execute(sql)
     if not ret:
         logger.error('store price error - %0.2f' % (price))
     pass
Ejemplo n.º 5
0
 def _do_sync(self):
     try:
         info_dict = self._bc.get_account_info()
         self._profile = info_dict['profile']
         self._balance = info_dict['balance']
         self._frozen = info_dict['frozen']
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         return False
Ejemplo n.º 6
0
 def _wget(self):
     ret = False
     data = None
     req = urllib2.Request(self._url)
     try:
         response = urllib2.urlopen(req, timeout=60)
         data = response.read()
         ret = True
     except urllib2.HTTPError, e:
         logger.error('HTTP Error: %d\t%s\t%s\t%s' % (e.code, e.reason, e.geturl(), e.read()))
Ejemplo n.º 7
0
    def _load_recent_price(self):
        ret = False
        recs = []

        sql = 'SELECT price, updatetime FROM t_price_history ORDER BY id desc LIMIT %d;' % (config.STOP_LOSS_SAMPLE_SPACE)
        ret, recs = self._db.execute(sql)
        if not ret:
            logger.error('load recent price fail')
            return ret, recs
        return ret, recs
 def _do_sync(self):
     try:
         info_dict = self._bc.get_account_info()
         self._profile = info_dict['profile']
         self._balance = info_dict['balance']
         self._frozen = info_dict['frozen']
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         return False
 def _sync(self):
     for cnt in range(1, self._retry+1):
         logger.info('%s sync account - %d' % (self._TITLE, cnt))
         if self._do_sync():
             logger.info('%s sync account - ok (btc %s, cny %s)' % (self._TITLE, self._balance['btc']['amount'], self._balance['cny']['amount']))
             break
         time.sleep(self._retry_interval)
         pass
     else:
         logger.error('%s sync account - fail' % (self._TITLE))
Ejemplo n.º 10
0
 def _wget(self):
     ret = False
     data = None
     req = urllib2.Request(self._url)
     try:
         response = urllib2.urlopen(req, timeout=60)
         data = response.read()
         ret = True
     except urllib2.HTTPError, e:
         logger.error("HTTP Error: %d\t%s\t%s\t%s" % (e.code, e.reason, e.geturl(), e.read()))
Ejemplo n.º 11
0
 def run(self):
     while True:
         try:
             self.update_btcchina()
         except Exception, e:
             logger.error(traceback.format_exc())
             logger.error(str(e))
         logger.info('sleep %d sec --------------------------------------' % (config.PRICE_UPDATE_INTERVAL))
         time.sleep(config.PRICE_UPDATE_INTERVAL)
         pass
Ejemplo n.º 12
0
    def _load_recent_price(self):
        ret = False
        recs = []

        sql = 'SELECT price, updatetime FROM t_price_history ORDER BY id desc LIMIT %d;' % (
            config.STOP_LOSS_SAMPLE_SPACE)
        ret, recs = self._db.execute(sql)
        if not ret:
            logger.error('load recent price fail')
            return ret, recs
        return ret, recs
Ejemplo n.º 13
0
 def run(self):
     while True:
         try:
             self.update_btcchina()
         except Exception, e:
             logger.error(traceback.format_exc())
             logger.error(str(e))
         logger.info('sleep %d sec --------------------------------------' %
                     (config.PRICE_UPDATE_INTERVAL))
         time.sleep(config.PRICE_UPDATE_INTERVAL)
         pass
Ejemplo n.º 14
0
 def _sync(self):
     for cnt in range(1, self._retry + 1):
         logger.info('%s sync account - %d' % (self._TITLE, cnt))
         if self._do_sync():
             logger.info('%s sync account - ok (btc %s, cny %s)' %
                         (self._TITLE, self._balance['btc']['amount'],
                          self._balance['cny']['amount']))
             break
         time.sleep(self._retry_interval)
         pass
     else:
         logger.error('%s sync account - fail' % (self._TITLE))
Ejemplo n.º 15
0
    def send(self, msg):
        ret = False
        if not self._check_interval():
            logger.info('sms send operation too frequently, cancel - "%s"' % msg)
            return ret

        url = self._gateway_url % (",".join(self._tel_list), msg)
        ret, data = self._wget(url)
        logger.info('request %s - "%s"' % (ret, data))
        if ret:
            json_data = json.loads(data)
            if json_data["ret"] == "0":
                logger.info('sms send ok - "%s"' % (msg))
            else:
                ret = False
                logger.error('sms send fail - "%s"' % (msg))
        return ret
Ejemplo n.º 16
0
    def _do_sell(self, price, amount):
        try:
            # set user id
            result = self._bc.get_account_info(post_data={'id': self._id})

            # TODO FOR TEST
            #raise NameError
            #result = True
            result = self._bc.sell(price, amount)
            logger.info('sell call - "%s"' % (str(result)))

            if not result:
                return False
        except Exception, e:
            logger.error(traceback.format_exc())
            logger.error(str(e))
            return False
Ejemplo n.º 17
0
    def _do_sell(self, price, amount):
        try:
            # set user id
            result = self._bc.get_account_info(post_data={'id':self._id})

            # TODO FOR TEST
            #raise NameError
            #result = True
            result = self._bc.sell(price, amount)
            logger.info('sell call - "%s"' % (str(result)))

            if not result:
                return False
        except Exception, e:
            logger.error(traceback.format_exc())
            logger.error(str(e))
            return False
Ejemplo n.º 18
0
    def update_btcchina(self):
        #raise NameError('Exception Raise')
        logger.info('task start - agents total %d' % (len(self._agents)))
        for agent in self._agents:
            logger.info('agent "%s"' % (agent.name))

            # get price
            logger.info('1. get price')
            ret, price = agent.query()
            if not ret:
                logger.error('query failed, skip "%s"' % (agent.name))
                continue

            # store
            logger.info('2. store db')
            self._store_db(price)

        pass
Ejemplo n.º 19
0
    def update_btcchina(self):
        #raise NameError('Exception Raise')
        logger.info('task start - agents total %d' % (len(self._agents)))
        for agent in self._agents:
            logger.info('agent "%s"' % (agent.name))

            # get price
            logger.info('1. get price')
            ret, price = agent.query()
            if not ret:
                logger.error('query failed, skip "%s"' % (agent.name))
                continue

            # store
            logger.info('2. store db')
            self._store_db(price)

        pass
Ejemplo n.º 20
0
    def send(self, msg):
        ret = False
        if not self._check_interval():
            logger.info('sms send operation too frequently, cancel - "%s"' %
                        msg)
            return ret

        url = self._gateway_url % (','.join(self._tel_list), msg)
        ret, data = self._wget(url)
        logger.info('request %s - "%s"' % (ret, data))
        if ret:
            json_data = json.loads(data)
            if json_data['ret'] == '0':
                logger.info('sms send ok - "%s"' % (msg))
            else:
                ret = False
                logger.error('sms send fail - "%s"' % (msg))
        return ret
Ejemplo n.º 21
0
    def query_history(self, history_length, file_path):
        price_list = []

        ret, data = self.execute('select price from t_price_history order by updatetime desc limit %d' % (history_length))
        if not ret:
            logger.error('query history fail')
            return ret

        for item in data:
            price_list.append(str(int(item[0])))

        price_list.reverse()

        fp = open(file_path, 'w')
        fp.write('\r\n'.join(price_list))
        fp.close()

        logger.info('query history dump latest %d price data to file "%s"' % (history_length, file_path))

        return ret
        pass
Ejemplo n.º 22
0
    def query_history(self, history_length, file_path):
        price_list = []

        ret, data = self.execute(
            'select price from t_price_history order by updatetime desc limit %d'
            % (history_length))
        if not ret:
            logger.error('query history fail')
            return ret

        for item in data:
            price_list.append(str(int(item[0])))

        price_list.reverse()

        fp = open(file_path, 'w')
        fp.write('\r\n'.join(price_list))
        fp.close()

        logger.info('query history dump latest %d price data to file "%s"' %
                    (history_length, file_path))

        return ret
        pass
class BitflyerPrice(object):
    '''
    '''
    def __init__(self):
        self._url = config.PRICE_INTERFACE['bitflyer']
        self.client = InfluxDBClient('localhost', 8086, 'root', ',', 'grafana')
        self._price = 0.0
        self._name = 'http://www.bitflyer.jp'
        self.ticker_index = {'BTC'}

    @property
    def name(self):
        return self._name

    def _wget(self):
        ret = False
        data = None
        try:
            textmod = ''
            #textmod = urllib.urlencode(textmod)
            req = urllib2.Request(url='%s%s' % (self._url, textmod))
            response = urllib2.urlopen(req, timeout=10)
            res = response.read()
            data = json.loads(res)
            query = 'select last(value) from Exchange where index=\'JPY\''
            result = self.client.query(query)
            exchange_value = result.raw['series'][0]['values'][0][1]
            for index in self.ticker_index:
                value = data['ask']
                json_body = [{
                    "measurement": "Bitflyer",
                    "tags": {
                        "coin": index,
                        "index": index
                    },
                    "fields": {
                        "buy": float(value) / exchange_value,
                        "buy_jpy": float(value)
                    }
                }]
                self.client.write_points(json_body)
            ret = True
        except urllib2.HTTPError, e:
            logger.error('HTTP Error: %d\t%s\t%s\t%s' %
                         (e.code, e.reason, e.geturl(), e.read()))
        except urllib2.URLError, e:
            logger.error('URL Error: %s' % (e.reason))
Ejemplo n.º 24
0
 def _create_db(self):
     ret = False
     try:
         conn = sqlite3.connect(self._db_file)
         logger.info('db file - "%s"' % (self._db_file))
         c = conn.cursor()
         for k in self._sql_create_tables:
             c.execute(self._sql_create_tables[k])
             conn.commit()
             logger.info('create table "%s"' % (k))
         conn.close()
         ret = True
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         logger.error('create table fail')
         return ret
Ejemplo n.º 25
0
 def _create_db(self):
     ret = False
     try:
         conn = sqlite3.connect(self._db_file)
         logger.info('db file - "%s"' % (self._db_file))
         c = conn.cursor()
         for k in self._sql_create_tables:
             c.execute(self._sql_create_tables[k])
             conn.commit()
             logger.info('create table "%s"' % (k))
         conn.close()
         ret = True
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         logger.error('create table fail')
         return ret
Ejemplo n.º 26
0
class SMSGateway(object):
    '''
    '''
    def __init__(self):
        self._gateway_url = config.SMS_GATEWAY_URL1
        self._tel_list = config.SMS_TEL_LIST
        self._send_interval = config.MIN_INTERVAL_SMS
        self._last_send = 0
        pass

    def _wget(self, url):
        ret = False
        data = None
        req = urllib2.Request(url)
        try:
            response = urllib2.urlopen(req, timeout=20)
            data = response.read()
            ret = True
        except urllib2.HTTPError, e:
            logger.error('HTTP Error: %d\t%s\t%s\t%s' %
                         (e.code, e.reason, e.geturl(), e.read()))
        except urllib2.URLError, e:
            logger.error('URL Error: %s' % (e.reason))
Ejemplo n.º 27
0
 def execute(self, sql):
     ret = False
     rec_list = []
     try:
         conn = sqlite3.connect(self._db_file)
         c = conn.cursor()
         c.execute(sql)
         conn.commit()
         logger.info('execute - "%s"' % (sql))
         rec_list = c.fetchall()
         logger.info('retrive - %d records - "%s"' % (len(rec_list), str(rec_list)))
         conn.close()
         ret = True
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         logger.error('execute sql fail - "%s"' % (sql))
         return ret, rec_list
Ejemplo n.º 28
0
class BTCChinaPrice(object):
    '''
    '''
    def __init__(self):
        self._url = config.PRICE_INTERFACE['btcchina']
        self._price = 0.0
        self._name = 'http://www.btcchina.com'

    @property
    def name(self):
        return self._name

    def _wget(self):
        ret = False
        data = None
        req = urllib2.Request(self._url)
        try:
            response = urllib2.urlopen(req, timeout=60)
            data = response.read()
            ret = True
        except urllib2.HTTPError, e:
            logger.error('HTTP Error: %d\t%s\t%s\t%s' % (e.code, e.reason, e.geturl(), e.read()))
        except urllib2.URLError, e:
            logger.error('URL Error: %s' % (e.reason))
Ejemplo n.º 29
0
 def execute(self, sql):
     ret = False
     rec_list = []
     try:
         conn = sqlite3.connect(self._db_file)
         c = conn.cursor()
         c.execute(sql)
         conn.commit()
         logger.info('execute - "%s"' % (sql))
         rec_list = c.fetchall()
         logger.info('retrive - %d records - "%s"' %
                     (len(rec_list), str(rec_list)))
         conn.close()
         ret = True
     except Exception, e:
         logger.error(traceback.format_exc())
         logger.error(str(e))
         logger.error('execute sql fail - "%s"' % (sql))
         return ret, rec_list
Ejemplo n.º 30
0
class ZBPrice(object):
    '''
    '''
    def __init__(self):
        self._url = config.PRICE_INTERFACE['zb']
        self._request_timeout = int(config.REQUEST_TIMEOUT)
        self.client = InfluxDBClient('localhost', 8086, 'root', ',', 'grafana')
        self._price = 0.0
        self._name = 'http://www.zb.com'
        self.ticker_index = {
            'btc_usdt',
            'bcc_usdt',  #'ubtc_usdt',
            'ltc_usdt',
            'eth_usdt',
            'etc_usdt',  #'bts_usdt','eos_usdt','qtum_usdt','hsr_usdt','xrp_usdt'
            #,'bcd_usdt','dash_usdt',#
            'btc_qc',  #bcc_qc,ubtc_qc,
            'ltc_qc',
            'eth_qc',
            'etc_qc',  #bts_qc,
            'eos_qc',
            'qtum_qc',  #hsr_qc,
            'xrp_qc',  #bcd_qc,
            'dash_qc'
        }

    @property
    def name(self):
        return self._name

    def _getDataFromURL(self, index):
        try:
            textmod = {'market': index}
            textmod = urllib.urlencode(textmod)
            url = '%s%s%s' % (self._url, '?', textmod)
            req = urllib2.Request(url)
            response = urllib2.urlopen(req, timeout=self._request_timeout)
            res = response.read()
            data = json.loads(res)
            buy_value = data['ticker']['buy']  # buy value
            high_value = data['ticker']['high']  #24h high value
            last_value = data['ticker']['last']  #last value
            low_value = data['ticker']['low']  #24h low value
            sell_value = data['ticker']['sell']  #sell value
            vol_value = data['ticker']['vol']  #24Сʱ�ɽ���
            json_body = [{
                "measurement": "ZB",
                "tags": {
                    "coin": index,
                    "index": index
                },
                "fields": {
                    "buy": float(buy_value),
                    "high": float(high_value),
                    "last": float(last_value),
                    "low": float(low_value),
                    "sell": float(sell_value),
                    "vol": float(vol_value)
                }
            }]
            self.client.write_points(json_body)
        except urllib2.HTTPError, e:
            logger.error('HTTP Error: %d\t%s\t%s\t%s' %
                         (e.code, e.reason, e.geturl(), e.read()))
        except urllib2.URLError, e:
            logger.error('URL Error: %s ' % (e.reason))