def __init__(self, instrument, size, spread): # Define variables self.instrument = instrument self.comision = 0 self.initial_size = size self.buy_size = size self.sell_size = size self.spread = spread self.tick = 0.001 self.my_order = dict() self.last_md = None self.state = States.WAITING_MARKET_DATA # Initialize the environment pyRofex.initialize(user="******", password="******", account="XXXXXXX", environment=pyRofex.Environment.REMARKET) # Initialize Websocket Connection with the handler pyRofex.init_websocket_connection( market_data_handler=self.market_data_handler, order_report_handler=self.order_report_handler) # Subscribes for Market Data pyRofex.market_data_subscription(tickers=[self.instrument], entries=[ pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.OFFERS ]) # Subscribes to receive order report for the default account pyRofex.order_report_subscription()
def subscribe_instruments(self, args): """ Subscribes to MarketData for specified products. (Each given Ticker HAS to be a str inside a one element list). :param args: List of tickers to subscribe to MarketData. :type args: list of list """ entries = [ pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.OFFERS, pyRofex.MarketDataEntry.LAST ] try: for instrument in args: self.subscribed_instruments.append(instrument) if logging.getLevelName('DEBUG') > 1: logging.debug( f'ROFEXClient: Subscribing to {instrument} MarketData') message = f'Subscribing to {instrument[0]} MarketData' print(message) pyRofex.market_data_subscription(tickers=instrument, entries=entries) except Exception as e: if logging.getLevelName('DEBUG') > 1: logging.debug(f'ROFEXClient ERROR: En exception occurred {e}') e = str(e).upper() error_msg = f"\033[0;30;47mERROR: {e} Check log file " \ "for detailed error message.\033[1;37;40m" print(error_msg) print(dash_line)
def open_connection(): pyRofex.init_websocket_connection(market_data_handler=market_data_handler, error_handler=error_handler, exception_handler=exception_handler) instruments = [args.ticker] entries = [pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.LAST] print("Consultando símbolo") pyRofex.market_data_subscription(tickers=instruments, entries=entries) pyRofex.market_data_subscription(tickers=["InvalidInstrument"], entries=entries)
def order_report_handler(message): print("Order Report Message Received: {0}".format(message)) def error_handler(message): print("Error Message Received: {0}".format(message)) def exception_handler(e): print("Exception Occurred: {0}".format(e.message)) # Initiate Websocket Connection pyRofex.init_websocket_connection(market_data_handler=market_data_handler, error_handler=error_handler, exception_handler=exception_handler) # Instruments list to subscribe instruments = ["RFX20Sep20", "I.RFX20"] # Uses the MarketDataEntry enum to define the entries we want to subscribe to entries = [ pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.OFFERS, pyRofex.MarketDataEntry.LAST ] # Subscribes to receive market data messages ** pyRofex.market_data_subscription(tickers=instruments, entries=entries) # Subscribes to receive order report messages (default account will be used) ** pyRofex.order_report_subscription()
plt.pause(0.2) # Defines the handlers that will process the messages def market_data_handler(message): global prices print("Market Data Message Received: {0}".format(message)) last = None if not message["marketData"]["LA"] else message["marketData"]["LA"]["price"] prices.loc[datetime.fromtimestamp(message["timestamp"]/1000)] = [ message["marketData"]["BI"][0]["price"], message["marketData"]["OF"][0]["price"], last ] # Initialize Websocket Connection with the handlers pyRofex.init_websocket_connection(market_data_handler=market_data_handler) # Subscribes to receive market data messages pyRofex.market_data_subscription( tickers=[instrument], entries=[ pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.OFFERS, pyRofex.MarketDataEntry.LAST] ) while True: update_plot() time.sleep(0.5)
def error_handler(message): print("Error Message Received: {0}".format(message)) def exception_handler(e): print("Exception Occurred: {0}".format(e.message)) # 3-Initialize Websocket Connection with the handlers pyRofex.init_websocket_connection(market_data_handler=market_data_handler, error_handler=error_handler, exception_handler=exception_handler) # 4-Subscribes to receive market data messages instruments = ["DONov19", "DODic19"] # Instruments list to subscribe entries = [ pyRofex.MarketDataEntry.BIDS, pyRofex.MarketDataEntry.OFFERS, pyRofex.MarketDataEntry.LAST ] pyRofex.market_data_subscription(tickers=instruments, entries=entries) # Subscribes to an Invalid Instrument (Error Message Handler should be call) pyRofex.market_data_subscription(tickers=["InvalidInstrument"], entries=entries) # Wait 5 sec then close the connection time.sleep(5) pyRofex.close_websocket_connection()