nacho-herrera / pyCocos

Python library to connect with Cocos Capital Rest APIs. Market Data and Order Routing are supported.
MIT License
32 stars 8 forks source link
api cocos cocoscapital finance python rest-api

Welcome to pyCocos' Documentation

Overview

pyCocos is a Python library that enables interaction with Cocos Capital REST APIs. It is designed to save developers hours of research and coding required to connect to Cocos Capital REST APIs.

Disclaimer

pyCocos is not owned by Cocos Capital, and the authors are not responsible for the use of this library.

Installation

To install pyCocos, you can use the following command:

pip install pycocos

API Credentials

To use this library, you need to have the correct authentication credentials.

Dependencies

The library has the following dependency:

requests>=2.31.0
simplejson>=3.19.1
pyotp>=2.9.0

Features

Available Methods

Initialization

Before using the library, you need to initialize it with a valid email and password. The library includes the working webpage API key token. If that token is updated, you can find it using the browser dev-tools and pass it on initialization with the api_key parameter.

2nd Factor Authentication

As of 4/13/2024, Cocos Capital has implemented second-factor authentication (2FA) for all users, offering options such as SMS, email, or TOTP (Time-Based One-Time Password) app authentication. This library incorporates the PyOTP module for automated code generation. To utilize this feature, the TOTP secret key must be provided as the totp_secret_key parameter during initialization. If the parameter is not supplied, the library will prompt for the code interactively.

For those with an activated TOTP app, you can retrieve your secret key using this method. If your Cocos Capital account lacks Google Authenticator activated, it's advisable to scan the QR code first with a QR reader, note the URI, and then add it to the Google Authenticator app. The QR code typically contains a text resembling: otpauth://totp/app.cocos.capital:?algorithm=SHA1&digits=6&issuer=app.cocos.capital&period=30&secret=. The within the URL is the TOTP secret key, which needs to be passed as the aforementioned parameter.

The PyOTP implementation was graciously developed by @El_Raulo on Telegram.

REST

The library provides functions to make requests to the REST API and retrieve the corresponding responses.

Functions

All functions return a dictionary representing the JSON response.

Websocket

Cocos Capital uses SocketIO for real-time quote updates. These methods are not available in the library at the moment.

Enumerations

The library also provides enumerations to help developers avoid errors and improve readability.

Usage

Once the library has been installed, you can import and initialize it. The initialization sets the email and password. It then attempts to authenticate with the provided credentials. If the authentication fails, an ApiException is thrown.

from pycocos import Cocos

app = Cocos(email="sample@email.com", password="S4mp13.p4ssW0rd", api_key="OPTIONAL", topt_secret_key="OPTIONAL")

api_key and topt_secret_key are optional parameters, default values are None.

REST

# Get the available portfolio with the current market valuation
app.my_portfolio()

# Get the available funds
app.funds_available()

# Send a withdrawal order of 1000 pesos
app.withdraw_funds(currency=app.currencies.PESOS, 
                   amount="1000", 
                   cbu_cvu="0123456789012345678912")

# Get the long ticker for AL30 with T+1 settlement
long_ticker = app.long_ticker(ticker="AL30", 
                              settlement=app.settlements.T1, 
                              currency=app.currencies.PESOS)

# Send a buy order for 200 AL30 bonds with T+2 settlement at $9000. By default, all orders are *LIMIT* orders.
order = app.submit_buy_order(long_ticker=long_ticker, 
                             quantity="200", 
                             price="9000")

# Cancel an order by order_id
app.cancel_order(order_number=order['Orden'])

# Get the quoteboard for "Acciones panel Lideres", T+1 settlement, traded in Pesos
app.instrument_list_snapshot(instrument_type=app.instrument_types.ACCIONES, 
                             instrument_subtype=app.instrument_subtypes.LIDERES, 
                             settlement=app.settlements.T1, 
                             currency=app.currencies.PESOS, 
                             segment=app.segments.DEFAULT)

For more information you can check this article.

Official API Documentation

There is no official API documentation for this library. The library was created by webscraping the app.

Acknowledgements

This library was created with the support of the Scrappers Argentinos and Inversiones y Algoritmos Telegram Groups. The updated version that includes OTP validation is based on the development and testing of @El_Raulo, @mjcolom, and @sebivaq. Special thanks to them.