Skip to main content

Unofficial Python wrapper for Trading REST APIs for Finvasia Shoonya platform

Project description

Shoonya Trading APIs

Unofficial Python Trading APIs for Finvasia Shoonya Platform.

To support this project consider opening account with finvasia.com with this referral link.

Finvasia offers Zero Brokerage trading and is doing good recently with it's new Shoonya platform.
Please check shoonya web platform.
Support this work by opening account with Finvasia

WORK IS STILL IN PROGRESS

USE AT YOUR OWN RISK, DO NOT EXPECT 100% PERFECT DATA

USE ONLY DURING LIVE MARKET

THIS API DOES NOT WORK ON SATURDAY SUNDAY

DataApi MAY WORK ON WEEKENDS WITH FEW EXCEPTIONS

Documentation is incomplete expect bugs and send pull requests

Installation

  • Python version >3.9 is needed for this module to work
  • Git is required for below command to work
  • pip install git+https://gitlab.com/algo2t/shoonyapy.git

Alternate way to install

  • Python version >3.9 is needed for this module to work
  • Download the wheel from releases
  • python -m pip install shoonyapy-0.1.0-py3-none-any.whl

How to setup a good environment

  • Install latest Python version 3.9.x, download it from here
  • Linux comes with python upgrade it to latest version 3.9.x
  • Use scoop.sh for Windows as package manager.
  • Use your favorite editor like VSCodium or VSCode download it from here
  • VScode is available for Windows, Linux and Mac
  • Python extension for vscode is here
  • MagicPython is also good extension
  • Wonderful documentation for working with python and vscode is here
  • Use virtualenv to create a virtual python env so that your system is not affected
  • Steps for virtualenv creation - python -m pip install virtualenv and then virtualenv venv
  • Activate the venv => .\venv\Scripts\activate (this is an example for Windows OS)
  • Install and upgrade modules (venv) PS D:\trading\algo2trade\shoonya> python -m pip install -U pip wheel setuptools pylint rope autopep8

Usage

Creating a config.py example

username='FA12345'
password='FinvAsia@P123'
# Date of Birth format is dd-MM-yyyy e.g 29-06-1984
pan_or_dob='ABCDE1234Z'

Login or Using DataApi and ShoonyaApi

from shoonyapy import ShoonyaApi, DataApi
import logging

import config

from datetime import datetime as dt
from time import sleep
import pandas as pd


logging.basicConfig(level=logging.DEBUG)


begin = dt.now()
print(begin)

data = DataApi()

shoonya = ShoonyaApi(config.username, config.password,
                     config.pan_or_dob, debug=True)

sbin = data.get_instrument_by_exchange_symbol('NSE', 'SBIN')

order_no = shoonya.place_order(sbin, 1, order_type=shoonya.ORDER_TYPE_LIMIT, price=472,
                               product=shoonya.PRODUCT_INTRADAY, txn_type=shoonya.TRANSACTION_TYPE_SELL, is_amo=False)

df = shoonya.orders()
df = df.loc[df['status'].str.contains('Pending'), [
    'order_no', 'status', 'segment', 'product']]
print(df)


end = dt.now()

duration = end - begin
print(end)
print(f"milliseconds taken :: {duration.microseconds/1000}")

API Short Documentation

Modify Orders API is still underdevelopment so do not use it.

List Orders or Order Book

shoonya.orders()

Order History or Order Details

shoonya.order_history(order_no, segment)

List Trades or Trade Book

shoonya.trades()

Trade History or Trade Details

shoonya.trade_history()

Holdings

shoonya.holdings()

Net Position or Positions

shoonya.positions()

Fund Limit or Funds - NOT WORKING

shoonya.funds()

Order Management APIs

Place Bracket Order

inst = data.get_instrument_by_exchange_symbol('MCX', 'GOLDPETAL21AUGFUT')
print(data.ltp(inst).ltp)

order_no = shoonya.place_bracket_order(inst, shoonya.TRANSACTION_TYPE_BUY, \
    shoonya.ORDER_TYPE_MARKET, 1, 0, 0, 2, 1)

inst = data.get_instrument_by_exchange_symbol('NSE', 'SBIN')
order_no = shoonya.place_bracket_order(inst, shoonya.TRANSACTION_TYPE_BUY, \
    shoonya.ORDER_TYPE_LIMIT, quantity=1, price=450, profit=2, stoploss=10)

print(order_no)

Exit Bracket Order

bo_exit = shoonya.exit_bracket_order(order_no, shoonya.SEGMENT_COMMODITY)
print(bo_exit)

Square Off All Positions

shoonya.square_off_all()

Square Off Symbol Positions

inst = data.get_instrument_by_exchange_symbol('NSE', 'SBIN')
shoonya.square_off_symbol(instrument.symbol)

Exit Pending Orders

shoonya.exit_pending_orders()

Place Order

data = DataApi()
inst = data.get_instrument_by_exchange_symbol(exchange, symbol)
print(inst)
order_no = shoonya.place_order(inst, quantity, price, is_amo=True)
print(order_no)

Cancel Order

shoonya.cancel_order(order_no, shoonya.SEGMENT_COMMODITY)

Market Status

shoonya = ShoonyaApi(config.username, config.password,
                     config.pan_or_dob, debug=True)

shoonya.market_status(exchange='MCX')
shoonya.market_status(mkt_type=shoonya.MARKET_TYPE_OL)
shoonya.market_status(mkt_type='')
shoonya.market_status(exchange=shoonya.EXCHANGE_NSE, segment=shoonya.SEGMENT_DERIVATIVE, mkt_type=shoonya.MARKET_TYPE_NL)

DataApi example for Indices Nifty Bank and Nifty 50 no login is required

from shoonyapy import DataApi

from datetime import datetime as dt
from datetime import timedelta as td

from collections import namedtuple

data = DataApi()


Instrument = namedtuple('Instrument', ['exchange', 'segment', 'token', 'symbol',
                                       'name', 'expiry', 'lot_size'])

inst = Instrument('NSE_INDICES','E','26009','Nifty Bank', 'Nifty Bank','0001-01-01T00:00:00', 0)
df = data.history(inst, start_time=dt.today() - td(days=5))
print(df)
inst = Instrument('NSE_INDICES','E','26000','Nifty 50', 'Nifty Bank','0001-01-01T00:00:00', 0)

# Default end_time is today
df = data.history(inst, start_time=dt.today() - td(days=5))
print(df)

df = data.history(inst, start_time=dt.today() - td(days=5), end_time=dt.today() - td(days=3), interval=15)
print(df)

Types of Exchanges

shoonya.EXCHANGE_NSE
shoonya.EXCHANGE_BSE
shoonya.EXCHANGE_MCX

Types of Segments

shoonya.SEGMENT_EQUITY
shoonya.SEGMENT_DERIVATIVE
shoonya.SEGMENT_CURRENCY
shoonya.SEGMENT_COMMODITY

Types of Transactions

shoonya.TRANSACTION_TYPE_BUY
shoonya.TRANSACTION_TYPE_SELL

Types of Products

shoonya.PRODUCT_NORMAL
shoonya.PRODUCT_INTRADAY
shoonya.PRODUCT_MARGIN
shoonya.PRODUCT_BO
shoonya.PRODUCT_CO
shoonya.PRODUCT_CNC
shoonya.PRODUCT_MTF

Types of Markets

shoonya.MARKET_TYPE_NL
shoonya.MARKET_TYPE_OL
shoonya.MARKET_TYPE_AU
shoonya.MARKET_TYPE_SP
shoonya.MARKET_TYPE_A1
shoonya.MARKET_TYPE_A2

Contributing

You can contribute to the project by submitting issues, merge requests, etc. Documentation updates or WiKi Updates can be suggested by users.

Donations

For any donations, please connect with the author.

Authors and acknowledgment

Thanks to people who motivated to create this project.
Thanks Finvasia / Shoonya Team for providing API Documentation

Dependencies

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE Version 3. Free distribution is allowed.

Project status

This project is a hobby project and must not be considered as legally bonded with Finvasia. In case, you want to support this project consider opening account with finvasia.com with this referral link.

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

shoonyapy-0.1.1.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

shoonyapy-0.1.1-py3-none-any.whl (37.5 kB view details)

Uploaded Python 3

File details

Details for the file shoonyapy-0.1.1.tar.gz.

File metadata

  • Download URL: shoonyapy-0.1.1.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.6 Windows/10

File hashes

Hashes for shoonyapy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d38b121935258b780192f76c5a04b7948b725145c6a1f8a814a4cdef836af28b
MD5 8136f38324f14f56f66ffb35ed8321e3
BLAKE2b-256 d232989d7b0cb21b7fb131e0a1d81012ad6fe3c58f5d118ccb9b69c9cc97d448

See more details on using hashes here.

File details

Details for the file shoonyapy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: shoonyapy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 37.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.6 Windows/10

File hashes

Hashes for shoonyapy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8154de0594e9f1b7c3f03ed8f134726d1ab627d7bd366e78d5ee02eafc88a00a
MD5 6816b9ae7e08657890847bd6852afd78
BLAKE2b-256 7e9ce559dee506a316980d7e81e125333bb053dbbf1b8460d779935631c5d33b

See more details on using hashes here.

Supported by

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