Пример #1
0
def autorelay_download_save(conn, table, bin_size, ohlcv_len, append):
    cur = conn.cursor()
    startd = time.time()
    bitmex = BitMex(threading=True)
    sql = str("select ROWID, * from '%s_%s' order by ROWID desc limit 1" % (table, bin_size))
    cur.execute(sql)
    item_list = cur.fetchall()
    end_time = datetime.now(timezone.utc)
    start_time = end_time - ohlcv_len * delta(bin_size)

    # 데이터가 있으면 릴레이 다운로드 자동으로 기간설정
    if item_list:
        lasttime = item_list[0][1]
        # print(lasttime)
        # print(lasttime[0:16])
        last_time = datetime.strptime(lasttime[0:16], '%Y-%m-%d %H:%M')
        start_time = last_time + timedelta(minutes=1)
    else:
        # 데이터가 없으면,
        print('No data and will start from ', start_time)
        pass

    # print('start_time:', start_time)
    # print('end_time:', end_time)

    # relay download 디폴트 기간 설정만큼 다운로드 한다.
    df = bitmex.fetch_ohlcv(bin_size, start_time, end_time)
    print('download_df_ohlcv time: ', time.time() - startd)

    # insert to database
    df.to_sql(table+'_'+bin_size, conn, if_exists=append)
    show_table(conn, table, bin_size, 1)
Пример #2
0
    def test_fetch_ohlcv_11m(self):
        ohlcv_len = 100
        bin_size = '11m'
        bitmex = BitMex(threading=False)

        end_time = datetime.now(timezone.utc)
        start_time = end_time - ohlcv_len * delta(bin_size)
        d1 = bitmex.fetch_ohlcv(bin_size, start_time, end_time)
        print(f"{d1}")
Пример #3
0
def test_fetch_ohlcv_1m(mins):
    '''
    source => DataFrame Type
    ------------------------------------------------------------------
                               open    high     low   close   volume
    2019-06-15 14:29:00+00:00  8669.5  8670.0  8667.0  8667.0  1454667
    2019-06-15 14:30:00+00:00  8667.0  8667.5  8667.0  8667.5   424940
    :return:
    '''
    # bitmex = BitMex(threading=False)
    bitmex = BitMex(threading=True)
    end_time = datetime.now(timezone.utc)
    start_time = end_time - 1 * timedelta(minutes=mins)
    source = bitmex.fetch_ohlcv('1m', start_time, end_time)
    return source
Пример #4
0
def download_df_ohlcv(bin_size, ohlcv_len, **kwargs):
    '''
    df => DataFrame Type
    ------------------------------------------------------------------
                               open    high     low   close   volume
    2019-06-15 14:29:00+00:00  8669.5  8670.0  8667.0  8667.0  1454667
    2019-06-15 14:30:00+00:00  8667.0  8667.5  8667.0  8667.5   424940
    :return:
    '''
    print('download data from server')
    start = time.time()
    # bitmex = BitMex(threading=False)
    bitmex = BitMex(threading=True)
    end_time = datetime.now(timezone.utc)
    start_time = end_time - ohlcv_len * delta(bin_size)
    df = bitmex.fetch_ohlcv(bin_size, start_time, end_time)
    print('download_df_ohlcv time:', time.time() - start)
    return df
Пример #5
0
 def test_fetch_ohlc_2h(self):
     bitmex = BitMex(threading=False)
     end_time = datetime.now(timezone.utc)
     start_time = end_time - 5 * timedelta(hours=2)
     source = bitmex.fetch_ohlcv('2h', start_time, end_time)
     assert len(source) > 1
Пример #6
0
 def test_fetch_ohlcv_5m(self):
     bitmex = BitMex(threading=False)
     end_time = datetime.now(timezone.utc)
     start_time = end_time - 5 * timedelta(minutes=5)
     source = bitmex.fetch_ohlcv('5m', start_time, end_time)
     assert len(source) > 1