Exemple #1
0
def save_to_mongodb(data):
    print('save to mongodb: ', data)
    return
    try:
        # 把 下面的实时数据写入 Mongodb中
        # "amount": 24小时成交量,
        # "open": 前推24小时成交价,
        # "close": 当前成交价,
        # "high": 近24小时最高价,
        # "low": 近24小时最低价,
        # "count": 近24小时累积成交数,
        # "vol": 近24小时累积成交额, 即
        # sum(每一笔成交价 * 该笔的成交量)
        # sym ts, amount, open, close, high, low, count, vol, amount
        ybdd = {}
        symbol = Symbol.convert_to_standard_symbol(
            Platform.PLATFORM_HUOBI, json_result["ch"].split('.')[1])
        tdata = json_result['tick']
        amount = tdata['amount']
        open = tdata['open']
        price = tdata['close']  # "close": 当前成交价 也就是最新价
        high = tdata['high']
        low = tdata['low']
        vol = tdata['vol']
        # 涨跌幅 最新成交价 - 24小时前成交价 / 24小时前成交价
        ff = (price - open) / open * 100
        delta = ('%.2f' % ff)
        ybdd['Change'] = '{0}{1}'.format(delta, '%')
        ybdd['sym'] = symbol
        ybdd['amount'] = amount
        ybdd['Price'] = price
        ybdd['High'] = high
        ybdd['Low'] = low
        ybdd['Volume'] = vol
        ybdd['api'] = 'ticker'
        ybdd['exchange'] = 'huobi'
        detail_coll.update({
            'sym': ybdd['sym'],
            'exchange': ybdd['exchange']
        }, {
            '$set': {
                'Change': ybdd['Change'],
                'Volume': ybdd['Volume'],
                'amount': ybdd['amount'],
                'Price': ybdd['Price'],
                'High': ybdd['High'],
                'Low': ybdd['Low'],
                'api': 'ticker'
            }
        }, True)
    except Exception as e:
        print(str(e))
Exemple #2
0
def save_to_mongodb(data):
    try:
        # 把 下面的实时数据写入 Mongodb中
        # sym ts, depth, sell_price, sell_amt, buy_price, buy_amt
        # symbol
        # 货币对
        symbol = Symbol.convert_to_standard_symbol(Platform.PLATFORM_HUOBI, json_result["ch"].split('.')[1])
        ts = data['ts']
        bids = json_result['tick']['bids']
        asks = json_result['tick']['asks']
        ybdd = {}
        ybdd['sym'] = symbol
        small_len = len(bids)
        if small_len > len(asks):
            small_len = len(asks)
        for i in range(small_len):
            depth = i + 1
            sell_price = asks[i][0]
            sell_amt = asks[i][1]
            buy_price = bids[i][0]
            buy_amt = bids[i][1]
        if len(bids) > small_len:
            for j in range(small_len, len(bids)):
                depth = j + 1
                buy_price = bids[j][0]
                buy_amt = bids[j][1]
        if len(asks) > small_len:
            for k in range(small_len, len(asks)):
                depth = k + 1
                sell_price = asks[k][0]
                sell_amt = asks[k][1]
                # line = [symbol, ts, depth, sell_price, sell_amt, '', '']
                # write_lines.append(line)
        ybdd['depth'] = depth
        ybdd['ts'] = ts
        ybdd['buy_price'] = buy_price
        ybdd['buy_amt'] = buy_amt
        ybdd['sell_price'] = sell_price
        ybdd['sell_amt'] = sell_amt
        ybdd['api'] = 'depth'
        ybdd['exchange'] = 'huobi'
        depth_coll.insert(ybdd)
        # depth_coll.update({'sym': ybdd['sym'], 'exchange': ybdd['exchange'], 'depth': ybdd['depth']},
        #                   {'$set': {'buy_price': ybdd['buy_price'], 'buy_amt': ybdd['buy_amt'],
        #                             'sell_price': ybdd['sell_price'], 'sell_amt': ybdd['sell_amt'],
        #                             'sym': ybdd['sym'], 'exchange': ybdd['exchange'], 'api': 'ticker'}}, True)
    except Exception as e:
        print(str(e))
Exemple #3
0
def save_to_csv(json_result):
    try:
        data_folder = 'D:\\huobi_download'
        today_date = time.strftime("%Y%m%d")
        file_path = os.path.join(data_folder, today_date, "huobi", "ticker")

        if not os.path.exists(file_path):
            os.makedirs(file_path)
            csv_name = file_path + '/' + "ticker" + '_' + today_date + '.csv'
            with open(csv_name, "a", newline='') as csvfile:
                csv_writer = csv.writer(csvfile)
                csv_writer.writerow([
                    'symbol', 'ts', 'latest_price', 'latest_amount',
                    'max_buy1_price', 'max_buy1_amt', 'min_sell1_price',
                    'min_sell1_amt', 'pre_24h_price', 'pre_24h_price_max',
                    'pre_24h_price_min', 'pre_24h_bt_finish_amt',
                    'pre_24h_usd_finish_amt'
                ])
        file_name = file_path + '/' + "ticker" + '_' + today_date + '.txt'
        # print (file_name)
        with open(file_name, 'a') as fw:
            fw.write(str(json_result) + '\n')

        csv_name = file_path + '/' + "ticker" + '_' + today_date + '.csv'
        # 转化为标准币对形式保存在csv
        symbol = Symbol.convert_to_standard_symbol(
            Platform.PLATFORM_HUOBI, json_result["ch"].split('.')[1])
        print(symbol)
        ts = json_result['ts']
        with open(csv_name, "a", newline='') as csvfile:
            csv_writer = csv.writer(csvfile)
            csv_writer.writerow([
                symbol, ts, '', '', '', '', '', '',
                json_result['tick']['open'], json_result['tick']['high'],
                json_result['tick']['low'], json_result['tick']['amount'],
                json_result['tick']['vol']
            ])

    except Exception as e:
        print(str(e))
def save_to_csv(json_result):
    try:
        #print("save_to_csv start time: ")
        #print(time.strftime("%Y%m%d %H:%M:%S"))
        #json_result = eval(json_result)
        data_folder = '/yanjiuyuan/data'
        today_date = time.strftime("%Y%m%d")
        file_path = os.path.join(data_folder, today_date, "huobi", "trader")
        if not os.path.exists(file_path):
            os.makedirs(file_path)
            csv_name = file_path + '/' + "trader" + '_' + today_date + '.csv'
            with open(csv_name, "a", newline='') as csvfile:
                csv_writer = csv.writer(csvfile)
                csv_writer.writerow(
                    ['symbol', 'id', 'ts', 'direction', 'amount', 'price'])

        file_name = file_path + '/' + "trader" + '_' + today_date + '.txt'
        # print (file_name)
        with open(file_name, 'a') as fw:
            fw.write(result + '\n')
        csv_name = file_path + '/' + "trader" + '_' + today_date + '.csv'
        # 转化为标准币对形式保存在csv
        symbol = Symbol.convert_to_standard_symbol(
            Platform.PLATFORM_HUOBI, json_result["ch"].split('.')[1])
        # print(symbol)
        tick_data = json_result['tick']['data']
        write_lines = []
        for trade in tick_data:
            write_lines.append([
                symbol, trade['id'], trade['ts'], trade['direction'],
                trade['amount'], trade['price']
            ])

        with open(csv_name, "a", newline='') as csvfile:
            csv_writer = csv.writer(csvfile)
            for line in write_lines:
                csv_writer.writerow(line)
    except Exception as e:
        print(str(e))
Exemple #5
0
 def sync_kline(self, symbol, period, size=2000):
     """
     :param symbol
     :param period: 可选值:{1min, 5min, 15min, 30min, 60min, 1day, 1mon, 1week, 1year }
     :param size: 可选值: [1,2000]
     :return:
     """
     params = {'symbol': symbol, 'period': period, 'size': size}
     url = MARKET_URL + '/market/history/kline'
     try:
         ret = http_get_request(url, params)
         if ret["status"] == "ok":
             json_ret = ret["data"]
             for line in json_ret:
                 sym = Symbol.convert_to_standard_symbol(
                     Platform.PLATFORM_HUOBI, symbol)
                 # 把 下面的实时数据写入 Mongodb中
                 # 'high' 最高价
                 # 'low' 最低价
                 # open 开盘价
                 # close 收盘价
                 # count
                 # base_vol 基准货币成交量
                 # quote_vol 计价货币成交量
                 # 货币对
                 ybdd = {}
                 ybdd['sym'] = sym
                 ybdd['ts'] = line["id"] * 1000
                 # 间隔时间
                 ybdd['interval'] = period
                 # 开盘价格
                 ybdd['open'] = line['open']
                 # 最高价格
                 ybdd['high'] = line['high']
                 # 最低价
                 ybdd['low'] = line['low']
                 # close
                 ybdd['close'] = line['close']
                 # count
                 ybdd['count'] = line['count']
                 # quote_vol 计价货币成交量
                 ybdd['quote_vol'] = round(line['vol'], 2)
                 ybdd['exchange'] = 'huobi'
                 ybdd['api'] = 'kline'
                 inter_p = Symbol.convert_to_standard_interval(
                     Platform.PLATFORM_HUOBI, period)
                 coll = sdb[mdb[inter_p]]
                 coll.update(
                     {
                         'sym': ybdd['sym'],
                         'ts': ybdd['ts'],
                         'interval': ybdd['interval']
                     }, {
                         '$set': {
                             'Open': ybdd['open'],
                             'High': ybdd['high'],
                             'Low': ybdd['low'],
                             'Close': ybdd['close'],
                             'Count': ybdd['count'],
                             'Quote_vol': ybdd['quote_vol'],
                             'exchange': ybdd['exchange'],
                             'api': ybdd['api']
                         }
                     }, True)
     except Exception as e:
         print(str(e))
def save_to_csv(json_result):
    try:
        data_folder = '/yanjiuyuan/data'
        today_date = time.strftime("%Y%m%d")
        file_path = os.path.join(data_folder, today_date, "huobi", "depth")
        if not os.path.exists(file_path):
            os.makedirs(file_path)
            csv_name = file_path + '/' + "depth" + '_' + today_date + '.csv'
            with open(csv_name, "a", newline='') as csvfile:
                csv_writer = csv.writer(csvfile)
                csv_writer.writerow([
                    'symbol', 'ts', 'depth', 'sell_price', 'sell_amt',
                    'buy_price', 'buy_amt'
                ])

        file_name = file_path + '/' + "depth" + '_' + today_date + '.txt'
        # print (file_name)
        with open(file_name, 'a') as fw:
            fw.write(result + '\n')
        csv_name = file_path + '/' + "depth" + '_' + today_date + '.csv'
        # 转化为标准币对形式保存在csv
        symbol = Symbol.convert_to_standard_symbol(
            Platform.PLATFORM_HUOBI, json_result["ch"].split('.')[1])
        ts = json_result['ts']
        write_lines = []
        bids = json_result['tick']['bids']
        # print (len(bids))
        asks = json_result['tick']['asks']
        # print(len(asks))
        small_len = len(bids)
        if small_len > len(asks):
            small_len = len(asks)
        for i in range(small_len):
            depth = i + 1
            sell_price = asks[i][0]
            sell_amt = asks[i][1]
            buy_price = bids[i][0]
            buy_amt = bids[i][1]
            line = [
                symbol, ts, depth, sell_price, sell_amt, buy_price, buy_amt
            ]
            write_lines.append(line)
        if len(bids) > small_len:
            for j in range(small_len, len(bids)):
                depth = j + 1
                buy_price = bids[j][0]
                buy_amt = bids[j][1]
                line = [symbol, ts, depth, '', '', buy_price, buy_amt]
                write_lines.append(line)
        if len(asks) > small_len:
            for k in range(small_len, len(asks)):
                depth = k + 1
                sell_price = asks[k][0]
                sell_amt = asks[k][1]
                line = [symbol, ts, depth, sell_price, sell_amt, '', '']
                write_lines.append(line)

        with open(csv_name, 'a', newline='') as csvfile:
            csv_writer = csv.writer(csvfile)
            for write_line in write_lines:
                csv_writer.writerow(write_line)

    except Exception as e:
        print(str(e))
def save_to_csv(symbol):
    # 今天日期
    today = datetime.date.today()
    #print(today)
    # 昨天时间
    yesterday = today - datetime.timedelta(days=1)
    #print(yesterday.strftime("%Y%m%d"))

    # 昨天开始时间戳
    yesterday_start_time = int(
        time.mktime(time.strptime(str(yesterday), '%Y-%m-%d')))
    #print(yesterday_start_time)
    # 昨天结束时间戳
    yesterday_end_time = int(time.mktime(time.strptime(str(today),
                                                       '%Y-%m-%d'))) - 1
    #print(yesterday_end_time)

    data_folder = '/yanjiuyuan/code/huobi/kline_data'

    yesterday_date = yesterday.strftime("%Y%m%d")
    file_path = os.path.join(data_folder, yesterday_date, "huobi", "kline")
    if not os.path.exists(file_path):
        os.makedirs(file_path)
        csv_name = file_path + '/' + "kline" + '_' + yesterday_date + '.csv'
        with open(csv_name, "a", newline='') as csvfile:
            csv_writer = csv.writer(csvfile)
            csv_writer.writerow([
                'symbol', 'ts', 'tm_intv', 'id', 'open', 'close', 'low',
                'high', 'amount', 'vol', 'count'
            ])

    r_lest = []
    try:
        result = get_kline(symbol)
        result = get_kline(symbol)
        result = get_kline(symbol)
        result = get_kline(symbol)

        if result["status"] == "ok":
            json_result = result["data"]

            for item in json_result:
                if item["id"] >= yesterday_start_time and item[
                        "id"] <= yesterday_end_time:
                    #print(item)
                    r_lest.append((item))

            #
    except Exception as e:
        print(str(e))

    print(len(r_lest))
    csv_name = file_path + '/' + "kline" + '_' + yesterday_date + '.csv'
    #print(csv_name)
    with open(csv_name, 'a', newline='') as csvfile:
        csv_writer = csv.writer(csvfile)
        for line in r_lest:
            writ_line = [
                Symbol.convert_to_standard_symbol(Platform.PLATFORM_HUOBI,
                                                  symbol), line["id"] * 1000,
                '1m', line['id'] * 1000, line['open'], line['close'],
                line['low'], line['high'], line['amount'], line['vol'],
                line['count']
            ]
            csv_writer.writerow(writ_line)