0

I need to be able to extract historical candlestick data (such as Open, Close, High, Low, and Volume) of a candlestick in differing intervals (1m, 3m, 5m, 1H, etc.) at a specified time (timestamps) from Phemex.

Other exchanges, such as Binance or FTX, seem to provide REST Websocket API for this, yet I can't seem to find one for Phemex. Mind helping me resolve this issue? Thank you so much.

Steps I have taken, yet found no resolution:

  1. Went to https://phemex.com/user-guides/api-overview
  2. Went to https://github.com/phemex/phemex-api-docs/blob/master/Public-Contract-API-en.md
  3. None of the items listed in 'Market Data API List' seem to do the task
gdont
  • 1
  • 1

1 Answers1

0

This code will get the candels and save them to a csv file. Hope this helps:)

exchange = ccxt.phemex({
    'options': { 'defaultType': 'swap' },
    'enableRateLimit': True
})
# Load the markets
markets = exchange.load_markets()

curent_time = int(time.time()*1000)
one_min = 60000     

def get_all_candels(symbol,start_time,stop_time):
    counter = 0
    candel_counter = 0
    data_set = []
    t = 0
    while t < stop_time:
        if data_set == []:
            block = exchange.fetch_ohlcv(symbol,'1m',start_time)
            for candle in block:
                if candle == []:
                    break
                data_set.append(candle)
            last_time_in_block = block[-1][0]
            counter += 1
            candel_counter += len(block)
            print(f'{counter} - {block[0]} - {candel_counter} - {last_time_in_block}')

        if data_set != []:
            t = last_time_in_block + one_min
            block = exchange.fetch_ohlcv(symbol,'1m',t)
            if block == []:
                break
            for candle in block:
                if candle == []:
                    break
                data_set.append(candle)
            last_time_in_block = block[-1][0]
            candel_counter += len(block)
            counter += 1
            print(f'{counter} - {block[0]} - {candel_counter} - {last_time_in_block}')

            time.sleep(1)
    return data_set
    
data_set = get_all_candels('BTCUSD',1574726400000,curent_time)

print(np.shape(data_set))

with open('raw.csv', 'w', newline='') as csv_file:
    column_names = ['time', 'open', 'high', 'low', 'close', 'volume']

    csv_writer = csv.DictWriter(csv_file,fieldnames=column_names)

    csv_writer.writeheader()

    for candel in data_set:
        csv_writer.writerow({
            'time':candel[0],
            'open':candel[1],
            'high':candel[2],
            'low':candel[3],
            'close':candel[4],
            'volume':candel[5]
        })