A Python SDK for Connecting to mStock Trading API and Streaming API
Project description
TradingAPI SDK - Python Client for accessing TradingAPI and Streaming Market Data
Description
TradingAPI is a complete SDK that allows you to build a trading and investment platform which includes executing orders in real time, streaming live trade and order data (Using Websockets) and more.
Installation
[!IMPORTANT] You can install the python client with below command. For requirements.txt file refer here.
pip install -r requirements.txt
pip install --upgrade mStock-TradingApi-A
It is recommended to update 'pip' and setuptools to the latest version if you are facing any issues during installation
pip install -U pip setuptools
[!NOTE] The package can be used with the name tradingapi_a
API Usage
import logging
from tradingapi_a.mconnect import *
logging.basicConfig(level=logging.DEBUG)
#Object for MConnect API
mconnect_obj=MConnect()
#Login
login_response=mconnect_obj.login("<user_id>","<password>")
#Generate access token by calling generate session
gen_response=mconnect_obj.generate_session("<API_Key>","<request_token_here>","<checksum>")
#Place Order
try:
porder_resp=mconnect_obj.place_order(_tradingsymbol="SBICARD",_exchange="NSE",_transaction_type="BUY",_order_type="MARKET",_quantity="10",_product="CNC",_validity="DAY",_price="0",_trigger_price="0")
logging.info("Order placed. ID is: {}".format(porder_resp["data"]["order_id"]))
except Exception as e:
logging.info("Order placement failed: {}".format(e.message))
#Fetch all orders
mconnect_obj.get_order_book()
#Fetch all holdings
mconnect_obj.get_holdings()
#Get Net position for logged in user
mconnect_obj.get_net_position()
#Cancel All orders
mconnect_obj.cancel_all()
#Get fund Summary
mconnect_obj.get_fund_summary()
Websocket Usage
from tradingapi_a.mticker import *
import logging
logging.basicConfig(level=logging.DEBUG)
#Testing Web Socket or MTicker
m_ticker=MTicker("<API_KEY>","<ACCESS_TOKEN>","<WEB_SOCKET_URL>")
#Defining Callbacks
def on_ticks(ws, ticks):
# Callback to receive ticks.
logging.info("Ticks: {}".format(ticks))
def on_order_update(ws,data):
#Callback to receive Order Updates
logging.info("On Order Updates Packet received : {}".format(data))
def on_trade_update(ws,data):
#Callback to receive Trade Updates
logging.info("On Trade Updates Packet received : {}".format(data))
def on_connect(ws, response):
# Callback on successful connect.
m_ticker.send_login_after_connect()
# Subscribe to a list of instrument_tokens .
ws.subscribe([5633])
# Set tick in `full` mode.
ws.set_mode(m_ticker.MODE_FULL, [5633])
def on_close(ws, code, reason):
# On connection close stop the event loop.
# Reconnection will not happen after executing `ws.stop()`
ws.stop()
# Assign the callbacks.
m_ticker.on_ticks = on_ticks
m_ticker.on_connect = on_connect
m_ticker.on_close = on_close
m_ticker.on_order_update=on_order_update
m_ticker.on_trade_update=on_trade_update
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
m_ticker.connect()
logging.info('Now Closing Web socket connection')
m_ticker.close()
logging.info('Testing complete')
Running Unit Tests
This requires having pytest library pre installed. You can install the same via pip:
pip install pytest
Navigate to the unit directory and run the connect_test.py file using pytest
cd unit
pytest connect_test.py
Support
For issues, please open an issue on GitHub.
Contributing
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a new branch (feature-xyz)
- Commit your changes
- Push the branch and create a pull request
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mstock_tradingapi_a-0.1.1.tar.gz.
File metadata
- Download URL: mstock_tradingapi_a-0.1.1.tar.gz
- Upload date:
- Size: 14.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
58b45b229d729228470e3f08826ce57fc39817c3ca129712a6faccc69c8e3f18
|
|
| MD5 |
67af5da4f7fbe81ac3abf949d059eacf
|
|
| BLAKE2b-256 |
975734423cfa2d6c0ef81729a07284216759b02101a0b5b4c580015a5f8d8aa4
|
File details
Details for the file mstock_tradingapi_a-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mstock_tradingapi_a-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ea505044f7f106db1f4462974e597b27527bbd8caa9cb742953de5ce9f0d6f9
|
|
| MD5 |
071744850dec8107d254f3d8ece945e2
|
|
| BLAKE2b-256 |
15b3df36b1a59d9c1de74950c8a681f442b910681697a6b3a0bd6f8c2f62d753
|