Skip to main content

Python connector for ROFEX's Rest and Websocket APIs. For multiples comitentes

Project description

Based on https://github.com/matbarofex/pyRofex by Franco Zanuso

Overview

pyGarufa is a python library that allows interactions with ROFEX’s Rest and Websocket APIs.

The library is designed to avoid developers hours of research and coding needed to connect with ROFEX APIs, so they could focused on the important part of their software.

Although, we recommend to take a look at the official API documentation to get familiarize with the API Responses and functionality.

Warning

pyGarufa will only work with the websocket-client version between 0.54.0 and 0.57.0

Installing

pyGarufa is available at the Python Package Index repository. Install and update using pip:

pip install -U pyGarufa

API Credentials

In order to use the library you need to have the correct authentication credentials for the environment.

To get new credentials:

Dependencies

Features

This sections describe the functionality and components of the library.

Available methods

Initialization

Before you start using the library, you need to initialize the environment that you want to connect with.

Functions
  • initialize: Initialize the specified environment. Set the default user, password and account for the environment.

  • set_default_environment: Set default environment. The default environment that is going to be used when no environment is specified.

Rest

The library provides functions to make requests to the REST API and return the corresponding response.

Functions
  • get_segments: gets a list of valid segments.

  • get_instruments: gets data of instruments by passing arguments for specific info.

  • get_all_instruments: gets a list of all available instruments.

  • get_detailed_instruments: gets a detailed list of all available instruments.

  • get_instrument_details: gets the details of a single instrument.

  • get_market_data: gets market data information for an instrument.

  • get_trade_history: gets a list of historic trades for an instrument.

  • send_order: sends a new order to the Market.

  • cancel_order: cancels an order.

  • get_order_status: gets the status of the specified order.

  • get_all_orders_status: gets the status of all the orders associated with an account.

  • get_account_position: gets the status of the account positions.

  • get_detailed_position: gets the status of detailed account asset positions by asset type.

  • get_account_report: gets the summary of associated account.

All functions return a dict of the JSON response.

Websocket

The library allows users to start a connection to the websocket API. The connection will be monitored by a separated thread, so its very important to set the proper handlers to process new incoming messages.

Functions
  • init_websocket_connection: configure the Websocket Client with the handlers and then start a Websocket connection with API.

  • close_websocket_connection: close the connection with the API.

  • market_data_subscription: sends a Market Data Subscription Message through the connection.

  • order_report_subscription: sends an Order Report Subscription Message through the connection.

  • add_websocket_market_data_handler **: adds a new Market Data handler to the Websocket Client. This handler is going to be call when a new Market Data Message is received.

  • remove_websocket_market_data_handler **: removes a Market Data handler from the handlers list in the Websocket Client.

  • add_websocket_order_report_handler **: adds a new Order Report handler to the Websocket Client. This handler is going to be call when a new Order Report Message is received.

  • remove_websocket_order_report_handler **: removes an Order Report handler from the handlers list in the Websocket Client.

  • add_websocket_error_handler **: adds a new Error handler to the Websocket Client. This handler is going to be call when a new Error Message is received.

  • remove_websocket_error_handler **: removes an Error handler from the handlers list in the Websocket Client.

  • set_websocket_exception_handler: sets an exception handler to the Websocket Client. This handler is going to be called when an Exception occurred in the client.

  • send_order_via_websocket: sends a new order to the Market.

  • cancel_order_via_websocket: cancels an order.

** handlers are pythons functions that will be call whenever the specific event occurred.

Enumerations

The library also provides some enumerations to help developers avoid errors and improve readability. Next, you have the list of available enums:

  • Environment: Identifies the environment to use. (REMARKET: Demo environment; LIVE: Production environment)

  • CFICode: Identifies the type of instrument.

  • MarketDataEntry: Identifies market data entries for an instrument.

  • Market: Market ID associated to the instruments.

  • MarketSegment: Market Segment ID associated to the instruments.

  • OrderType: Identifies the different order types.

  • Side: Identifies the side of an order.

  • TimeInForce: Time modifier of the order that defines the time the order will be active.

How to use it

Once the library is install, we import and initialize it.

The initialization sets the user, password and account to the environment specified. Then, try to authenticate with the given user/password.

If the authentication fails, an ApiException is raised.

Finally, sets the environment as the default one. (you can change it with the set_default_environment function)

import pyGarufa

# Set the the parameter for the REMARKET environment
pyGarufa.initialize(user="sampleUser",
                   password="samplePassword",
                   account="sampleAccount",
                   environment=pyGarufa.Environment.REMARKET)

# In case you have a previously generated and active token, you will be able to do the following
pyGarufa.initialize(user="sampleUser",
                   password="samplePassword",
                   account="sampleAccount",
                   environment=pyGarufa.Environment.REMARKET,
                   active_token="activeToken")

Rest

# Makes a request to the Rest API and get the last price
# Use the MarketDataEntry enum to specify the data
pyGarufa.get_market_data(ticker="DLR/DIC23",
                        entries=[pyGarufa.MarketDataEntry.LAST])

# Gets all segments
pyGarufa.get_segments()

# Gets available instruments list
pyGarufa.get_all_instruments()

# Alternative option to get all available instruments list
pyGarufa.get_instruments('all')

# Alternative option to get instruments by given cfi code:
pyGarufa.get_instruments('by_cfi',
                        cfi_code=[pyGarufa.CFICode.STOCK,
                                  pyGarufa.CFICode.BOND,
                                  pyGarufa.CFICode.CEDEAR])

# Alternative option to get instruments by given segments
pyGarufa.get_instruments('by_segments',
                        market=pyGarufa.Market.ROFEX,
                        market_segment=[pyGarufa.MarketSegment.DDF,
                                        pyGarufa.MarketSegment.MERV])

# Gets detailed instruments list
pyGarufa.get_detailed_instruments()

# Alternative option to get detailed instruments list
pyGarufa.get_instruments('details')

# Get all order report for the configured account
pyGarufa.get_all_orders_status()

# Gets historic trades
pyGarufa.get_trade_history(ticker="DLR/JUN23",
                          start_date="2018-12-01",
                          end_date="2019-01-10")

# Sends a Limit order to the market
order = pyGarufa.send_order(ticker="DLR/DIC23",
                           side=pyGarufa.Side.BUY,
                           size=10,
                           price=55.8,
                           order_type=pyGarufa.OrderType.LIMIT)

# Gets the last order status for the previous order
pyGarufa.get_order_status(order["order"]["clientId"])

# Cancels the previous order
cancel_order = pyGarufa.cancel_order(order["order"]["clientId"])

# Checks the order status of the cancellation order
pyGarufa.get_order_status(cancel_order["order"]["clientId"])

Websocket

# First we define the handlers that will process the messages and exceptions.
def market_data_handler(message):
    print("Market Data Message Received: {0}".format(message))
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
pyGarufa.init_websocket_connection(market_data_handler=market_data_handler,
                                  order_report_handler=order_report_handler,
                                  error_handler=error_handler,
                                  exception_handler=exception_handler)

# Instruments list to subscribe
instruments = ["DLR/DIC23", "DLR/ENE24"]
# Uses the MarketDataEntry enum to define the entries we want to subscribe to
entries = [pyGarufa.MarketDataEntry.BIDS,
           pyGarufa.MarketDataEntry.OFFERS,
           pyGarufa.MarketDataEntry.LAST]

# Subscribes to receive market data messages **
pyGarufa.market_data_subscription(tickers=instruments,
                                 entries=entries)

# Subscribes to receive order report messages (default account will be used) **
pyGarufa.order_report_subscription()

** Every time a new message is received, the correct handler will be call.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyGarufa-0.0.3.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyGarufa-0.0.3-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file pyGarufa-0.0.3.tar.gz.

File metadata

  • Download URL: pyGarufa-0.0.3.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.9

File hashes

Hashes for pyGarufa-0.0.3.tar.gz
Algorithm Hash digest
SHA256 1f602134ec0d652d7eca81549c804385d529ddbd217a618f028201ba76eee23d
MD5 4c6d14d786b1cc4827785fea65777f40
BLAKE2b-256 383516c408161f5faa42dfd195e5be90571c7c9e56c9906f0c97335a31c76e63

See more details on using hashes here.

File details

Details for the file pyGarufa-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pyGarufa-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.9

File hashes

Hashes for pyGarufa-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 626503cb966444afbc8df129038a7e701a255d064d272d608a299fa7e6adbf05
MD5 7313e493fa7e54253e108a0874835d89
BLAKE2b-256 fa18aae85941c97192c4fc30323e727d82bb4181923b732df1d3dc4fa96bbd58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page