'How to store real-time candle information (open, close, high, low prices) with python from the bitmex websocket?

I am able to connect to the bitmex websocket with python, using the code below.

'''

from bitmex_websocket import BitMEXWebsocket
import logging
from time import sleep


# Basic use of websocket.
def run():
    logger = setup_logger()

    # Instantiating the WS will make it connect. Be sure to add your api_key/api_secret.
    ws = BitMEXWebsocket(endpoint="wss://ws.testnet.bitmex.com/realtime", symbol="XBTUSD",
                         api_key=bitmex_api_key_test, api_secret=bitmex_api_secret_test)

    logger.info("Instrument data: %s" % ws.get_instrument())

    # Run forever
    while(ws.ws.sock.connected):
        logger.info("Ticker: %s" % ws.get_ticker())
        if ws.api_key:
            logger.info("Funds: %s" % ws.funds())
        logger.info("Market Depth: %s" % ws.market_depth())
        logger.info("Recent Trades: %s\n\n" % ws.recent_trades())
        sleep(10)


def setup_logger():
    # Prints logger info to terminal
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)  # Change this to DEBUG if you want a lot more info
    ch = logging.StreamHandler()
    # create formatter
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    # add formatter to ch
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger


if __name__ == "__main__":
    run()

'''

It will create a continuous output with new information every 10 sec. Example output of the first second is shown below (apologies for the large body of text).

2022-01-24 14:52:55,684 - bitmex_websocket - INFO - Connecting to wss://ws.testnet.bitmex.com/realtime?subscribe=execution:XBTUSD,instrument:XBTUSD,margin,order:XBTUSD,orderBookL2:XBTUSD,position:XBTUSD,quote:XBTUSD,trade:XBTUSD 2022-01-24 14:52:55,684 - bitmex_websocket - INFO - Connecting to wss://ws.testnet.bitmex.com/realtime?subscribe=execution:XBTUSD,instrument:XBTUSD,margin,order:XBTUSD,orderBookL2:XBTUSD,position:XBTUSD,quote:XBTUSD,trade:XBTUSD 2022-01-24 14:52:55,687 - bitmex_websocket - INFO - Authenticating with API Key. 2022-01-24 14:52:55,687 - bitmex_websocket - INFO - Authenticating with API Key. 2022-01-24 14:52:56,695 - bitmex_websocket - INFO - Connected to WS. 2022-01-24 14:52:56,695 - bitmex_websocket - INFO - Connected to WS. 2022-01-24 14:52:57,701 - bitmex_websocket - INFO - Got all market data. Starting. 2022-01-24 14:52:57,701 - bitmex_websocket - INFO - Got all market data. Starting. 2022-01-24 14:52:57,709 - root - INFO - Instrument data: {'symbol': 'XBTUSD', 'rootSymbol': 'XBT', 'state': 'Open', 'typ': 'FFWCSX', 'listing': '2016-05-04T12:00:00.000Z', 'front': '2016-05-04T12:00:00.000Z', 'expiry': None, 'settle': None, 'listedSettle': None, 'relistInterval': None, 'inverseLeg': '', 'sellLeg': '', 'buyLeg': '', 'optionStrikePcnt': None, 'optionStrikeRound': None, 'optionStrikePrice': None, 'optionMultiplier': None, 'positionCurrency': 'USD', 'underlying': 'XBT', 'quoteCurrency': 'USD', 'underlyingSymbol': 'XBT=', 'reference': 'BMEX', 'referenceSymbol': '.BXBT', 'calcInterval': None, 'publishInterval': None, 'publishTime': None, 'maxOrderQty': 10000000, 'maxPrice': 1000000, 'lotSize': 100, 'tickSize': 0.5, 'multiplier': -100000000, 'settlCurrency': 'XBt', 'underlyingToPositionMultiplier': None, 'underlyingToSettleMultiplier': -100000000, 'quoteToSettleMultiplier': None, 'isQuanto': False, 'isInverse': True, 'initMargin': 0.01, 'maintMargin': 0.0035, 'riskLimit': 20000000000, 'riskStep': 15000000000, 'limit': None, 'capped': False, 'taxed': True, 'deleverage': True, 'makerFee': -0.0001, 'takerFee': 0.0005, 'settlementFee': 0, 'insuranceFee': 0, 'fundingBaseSymbol': '.XBTBON8H', 'fundingQuoteSymbol': '.USDBON8H', 'fundingPremiumSymbol': '.XBTUSDPI8H', 'fundingTimestamp': '2022-01-24T20:00:00.000Z', 'fundingInterval': '2000-01-01T08:00:00.000Z', 'fundingRate': 0.0001, 'indicativeFundingRate': 0.0001, 'rebalanceTimestamp': None, 'rebalanceInterval': None, 'openingTimestamp': '2022-01-24T14:00:00.000Z', 'closingTimestamp': '2022-01-24T15:00:00.000Z', 'sessionInterval': '2000-01-01T01:00:00.000Z', 'prevClosePrice': 33702.68, 'limitDownPrice': None, 'limitUpPrice': None, 'bankruptLimitDownPrice': None, 'bankruptLimitUpPrice': None, 'prevTotalVolume': 147166798830, 'totalVolume': 147166856930, 'volume': 58100, 'volume24h': 3824500, 'prevTotalTurnover': 1953138810607885, 'totalTurnover': 1953138983862673, 'turnover': 173254788, 'turnover24h': 11139447304, 'homeNotional24h': 111.39447304, 'foreignNotional24h': 3824500, 'prevPrice24h': 35196.5, 'vwap': 34332.9957, 'highPrice': 36297, 'lowPrice': 32910, 'lastPrice': 33586, 'lastPriceProtected': 33586, 'lastTickDirection': 'MinusTick', 'lastChangePcnt': -0.0458, 'bidPrice': 33521, 'midPrice': 33544.75, 'askPrice': 33568.5, 'impactBidPrice': 33374.1611, 'impactMidPrice': 33499.5, 'impactAskPrice': 33625.087, 'hasLiquidity': False, 'openInterest': 59224800, 'openValue': 176468583072, 'fairMethod': 'FundingRate', 'fairBasisRate': 0.1095, 'fairBasis': 2.15, 'fairPrice': 33561.15, 'markMethod': 'FairPrice', 'markPrice': 33561.15, 'indicativeTaxRate': 0, 'indicativeSettlePrice': 33559, 'optionUnderlyingPrice': None, 'settledPriceAdjustmentRate': None, 'settledPrice': None, 'timestamp': '2022-01-24T14:52:55.000Z', 'tickLog': 0} 2022-01-24 14:52:57,709 - root - INFO - Instrument data: {'symbol': 'XBTUSD', 'rootSymbol': 'XBT', 'state': 'Open', 'typ': 'FFWCSX', 'listing': '2016-05-04T12:00:00.000Z', 'front': '2016-05-04T12:00:00.000Z', 'expiry': None, 'settle': None, 'listedSettle': None, 'relistInterval': None, 'inverseLeg': '', 'sellLeg': '', 'buyLeg': '', 'optionStrikePcnt': None, 'optionStrikeRound': None, 'optionStrikePrice': None, 'optionMultiplier': None, 'positionCurrency': 'USD', 'underlying': 'XBT', 'quoteCurrency': 'USD', 'underlyingSymbol': 'XBT=', 'reference': 'BMEX', 'referenceSymbol': '.BXBT', 'calcInterval': None, 'publishInterval': None, 'publishTime': None, 'maxOrderQty': 10000000, 'maxPrice': 1000000, 'lotSize': 100, 'tickSize': 0.5, 'multiplier': -100000000, 'settlCurrency': 'XBt', 'underlyingToPositionMultiplier': None, 'underlyingToSettleMultiplier': -100000000, 'quoteToSettleMultiplier': None, 'isQuanto': False, 'isInverse': True, 'initMargin': 0.01, 'maintMargin': 0.0035, 'riskLimit': 20000000000, 'riskStep': 15000000000, 'limit': None, 'capped': False, 'taxed': True, 'deleverage': True, 'makerFee': -0.0001, 'takerFee': 0.0005, 'settlementFee': 0, 'insuranceFee': 0, 'fundingBaseSymbol': '.XBTBON8H', 'fundingQuoteSymbol': '.USDBON8H', 'fundingPremiumSymbol': '.XBTUSDPI8H', 'fundingTimestamp': '2022-01-24T20:00:00.000Z', 'fundingInterval': '2000-01-01T08:00:00.000Z', 'fundingRate': 0.0001, 'indicativeFundingRate': 0.0001, 'rebalanceTimestamp': None, 'rebalanceInterval': None, 'openingTimestamp': '2022-01-24T14:00:00.000Z', 'closingTimestamp': '2022-01-24T15:00:00.000Z', 'sessionInterval': '2000-01-01T01:00:00.000Z', 'prevClosePrice': 33702.68, 'limitDownPrice': None, 'limitUpPrice': None, 'bankruptLimitDownPrice': None, 'bankruptLimitUpPrice': None, 'prevTotalVolume': 147166798830, 'totalVolume': 147166856930, 'volume': 58100, 'volume24h': 3824500, 'prevTotalTurnover': 1953138810607885, 'totalTurnover': 1953138983862673, 'turnover': 173254788, 'turnover24h': 11139447304, 'homeNotional24h': 111.39447304, 'foreignNotional24h': 3824500, 'prevPrice24h': 35196.5, 'vwap': 34332.9957, 'highPrice': 36297, 'lowPrice': 32910, 'lastPrice': 33586, 'lastPriceProtected': 33586, 'lastTickDirection': 'MinusTick', 'lastChangePcnt': -0.0458, 'bidPrice': 33521, 'midPrice': 33544.75, 'askPrice': 33568.5, 'impactBidPrice': 33374.1611, 'impactMidPrice': 33499.5, 'impactAskPrice': 33625.087, 'hasLiquidity': False, 'openInterest': 59224800, 'openValue': 176468583072, 'fairMethod': 'FundingRate', 'fairBasisRate': 0.1095, 'fairBasis': 2.15, 'fairPrice': 33561.15, 'markMethod': 'FairPrice', 'markPrice': 33561.15, 'indicativeTaxRate': 0, 'indicativeSettlePrice': 33559, 'optionUnderlyingPrice': None, 'settledPriceAdjustmentRate': None, 'settledPrice': None, 'timestamp': '2022-01-24T14:52:55.000Z', 'tickLog': 0} 2022-01-24 14:52:57,713 - root - INFO - Ticker: {'last': 33586.0, 'buy': 33521.0, 'sell': 33568.0, 'mid': 33545.0}

However, I would like to use this data to calculate some technical indicators, and create buying and selling signals. How can I store the live websocket data (i.e. in a dataframe that keeps getting updated), containing high, low, open and close candle prices, to perform further calculations?

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source