matbarofex / pyRofex

Python library to connect with Matba Rofex's Rest and Websocket APIs. Market Data and Order Routing are supported.
MIT License
78 stars 54 forks source link

Data suscription #7

Closed lfaccioli closed 3 years ago

lfaccioli commented 3 years ago

Hola, buenas tardes.

Queria consultar, como se podria manipular data si yo me suscribo a dos instrumentos. Es decir, si quiero comparar el bid de un instrumento, con el offer del otro por decir de alguna forma. Como se podria armar ?

Aprovecho para agradecer esta gran herramienta.

Un saludo

fzanuso commented 3 years ago

Hola,

Dependiendo como consultes la marketdata, si es por la api rest o websocket.

Por rest tendrias que hacer ambas consultas por seperado y comparar la respuesta:

md1 = pyRofex.get_market_data("DONov20", [pyRofex.MarketDataEntry.BIDS])

md2 = pyRofex.get_market_data("DODic20", [pyRofex.MarketDataEntry.BIDS])

md1_bid = md1["marketData"]["BI"][0]["price"]
md2_bid = md2["marketData"]["BI"][0]["price"]

print("bid DONov20 is: %s\nbid DODic20 is: %s" % (md1_bid, md2_bid))

if md1_bid > md2_bid:
    print("bid DONov20 is higher than bid of DODic20")
elif md1_bid < md2_bid:
    print("bid DONov20 is lower than bid of DODic20")
else:
    print("bid DONov20 is equal than bid of DODic20") 

Por websocket tendrias que ir almacenando los precios que recibis para compararlos:

instr_list = ["DONov20", "DODic20"]

prices = dict()
for inst in instr_list:
    prices[inst] = None

# Subscribes to receive market data messages **
pyRofex.market_data_subscription(tickers=instr_list, entries=[pyRofex.MarketDataEntry.BIDS])

# Defines the handlers that will process the messages
def market_data_handler(message):
    global prices
    symbol = message["instrumentId"]["symbol"]
    if message["instrumentId"]["symbol"] in prices:
        prices[symbol] = message["marketData"]["BI"][0]["price"]

    if prices["DONov20"] and prices["DODic20"]:
        if prices["DONov20"] > prices["DODic20"]:
            print("bid DONov20 is higher than bid of DODic20")
        elif prices["DONov20"] < prices["DODic20"]:
            print("bid DONov20 is lower than bid of DODic20")
        else:
            print("bid DONov20 is equal than bid of DODic20")

# Initialize Websocket Connection with the handlers
pyRofex.init_websocket_connection(market_data_handler=market_data_handler)

Nose si algo asi te sirve, pero para el caso de precios lo mejor es ir escuchando las actualizaciones por websocket, ir almacenando los precios en alguna estructura de datos (en el ejemplo uso un diccionario, pero tranquilamente podria ser un DataFrame), para luego hacer las comparaciones entre los datos que recibas.

Saluds!