Skip to main content

Python SDK for 5paisa APIs natively written in VB.NET

Project description

5paisa Python SDK

Python SDK for 5paisa APIs natively written in VB .NET

PyPI GitHub Workflow Status (branch)

5paisa logo

Documentation

Read the docs hosted here

Features

  • Order placement, modification and cancellation
  • Fetching user info including holdings, positions, margin and order book.
  • Fetching live market streaming.
  • Placing, modifying and deleting Bracket Order.
  • Fetching order status and trade information.
  • Getting live data streaming using websockets.

Installation

pip install py5paisa

Usage

Configuring API keys

Get your API keys from here

Note:- We have deprecated the existing method which involved the use of login credentials. Kindly go through this updated documentation of using Access token for API Access.

Scrip codes reference:

Note : Use these Links for getting scrip codes

Scrip Master - Downaload ScripMaster [here](https://openapi.5paisa.com/VendorsAPI/Service1.svc/ScripMaster/segment/All)

[API Documentation](https://xstream.5paisa.com/dev-docs/docFundamentals/scrip-master)

#Fetch Scrip Codes

scrips = client.get_scrips()

#Query Script Data Inputs sequence- exchange, exchangetype, symbol, strike, type, expiry

#Strike to be 0 for cash stocks , Actual Strike for Derivatives 

#type to be XX for Cash stocks and Futures, EQ for indices, CE/PE for Options

#Fetch Scrip Data for Cash
record = client.query_scrips("N","C","ITC","0","XX","")
#Fetch Scrip Data for Options
record = client.query_scrips("N","D","NIFTY","22300","CE","2024-04-25")
#Fetch Scrip Data for Futures
record = client.query_scrips("N","C","INFY","0","XX","")

AUTHENTICATION USING OAUTH

from py5paisa import FivePaisaClient
cred={
    "APP_NAME":"YOUR APP_NAME",
    "APP_SOURCE":"YOUR APP_SOURCE",
    "USER_ID":"YOUR USER_ID",
    "PASSWORD":"YOUR PASSWORD",
    "USER_KEY":"YOUR USERKEY",
    "ENCRYPTION_KEY":"YOUR ENCRYPTION_KEY"
    }

#This function will automatically take care of generating and sending access token for all your API's

client = FivePaisaClient(cred=cred)

# OAUTH Approach
# First get a token by logging in to -> https://dev-openapi.5paisa.com/WebVendorLogin/VLogin/Index?VendorKey=<Your Vendor Key>&ResponseURL=<Redirect URL>
# VendorKey is UesrKey for individuals user
# for e.g. you can use ResponseURL as https://www.5paisa.com/technology/developer-apis
# Pass the token received in the response url after successful login to get an access token (this also sets the token for all the APIs you use)-

# Please note that you need to copy the request token from URL and paste in this code and start the code within 30s.

client.get_oauth_session('Your Response Token')

After successful authentication, you should get a `Logged in!!` message in console

#Function to fetch access token after successful login
print(client.get_access_token())

#Login with Access Token
client.set_access_token('accessToken','clientCode')

Market Feed

#NOTE : ScripData and ScripCode you can find from new Scripmaster as mentioned above


req_list_ = [{"Exch": "N", "ExchType": "C", "ScripData": "ITC_EQ"}]
              {"Exch": "N", "ExchType": "C", "ScripCode": "2885"}]

print(client.fetch_market_feed_scrip(req_list_))

Market Status

print(client.get_market_status())

Fetching user info

# Fetches holdings
client.holdings()

# Fetches margin
client.margin()

# Fetches positions
client.positions()

# Fetches the order book of the client
client.order_book()

# Fetches Trade book
client.get_tradebook()

Position Conversion

# Convert positions
# client.position_convertion(<Exchange>,<Exchange Type>,<Scrip Name>,<Buy/Sell>,<Qty>,<From Delivery/Intraday>,<From Delivery/Intraday>)
client.position_convertion("N","C","BPCL_EQ","B",5,"D","I")

Placing an order

# Note: This is an indicative order.

from py5paisa.order import Order, OrderType, Exchange

#You can pass scripdata either you can pass scripcode also.
# please use price = 0 for market Order
#use IsIntraday= true for intraday orders

#Using Scrip Data :-

#Using Scrip Code :-
client.place_order(OrderType='B',Exchange='N',ExchangeType='C', ScripCode = 1660, Qty=1, Price=260)
#Sample For SL order (for order to be treated as SL order just pass StopLossPrice)
client.place_order(OrderType='B',Exchange='N',ExchangeType='C', ScripCode = 1660, Qty=1, Price=350, IsIntraday=False, StopLossPrice=345)
#Derivative Order
client.place_order(OrderType='B',Exchange='N',ExchangeType='D', ScripCode = 57633, Qty=50, Price=1.5)

Please refer below documentation link for paramaters to be passed in cleint.place_order function
https://www.5paisa.com/developerapi/order-request-place-order

Placing offline orders (After Market Orders)

By default all orders are normal orders, pass AHPlaced=Y to place offline orders.

client.place_order(OrderType='B',Exchange='N',ExchangeType='C', ScripCode = 1660, Qty=1, Price=325, AHPlaced="Y")

Modifying an order

client.modify_order(ExchOrderID="1100000017861430", Qty=2,Price=261)

Cancelling an order

client.cancel_order(exch_order_id="1100000017795041")
cancel_bulk=[
            {
                "ExchOrderID": "<Exchange Order ID 1>"
            },
            {
                "ExchOrderID": "<Exchange Order ID 2>"
            },
client.cancel_bulk_order(cancel_bulk)

Order Margin Calculation

client.Order_margin( Exch= "N", ExchType = "C", OrderRequestorCode = "51959929", ScripCode = "1660", PlaceModifyCancel = "P",  TransactionType = "B", AtMarket = "Y", LimitRate = 0, Volume = 5, OldTradedQty = 0, ProductType = "D", ExchOrderId = "0", CoverPositions ="N")

SquareOffAll Orders

client.squareoff_all()

Bracket Order

For placing Braket order

client.bo_order(OrderType='B',Exchange='N',ExchangeType='C', ScripCode = 1660, Qty=1, LimitPrice=330,TargetPrice=345,StopLossPrice=320,LimitPriceForSL=319,TrailingSL=1.5)

For placing Cover order

client.cover_order(OrderType='B',Exchange='N',ExchangeType='C', ScripCode = 1660, Qty=1, LimitPrice=330,StopLossPrice=320,TrailingSL=1.5)

Note:For placing Bracket order in FNO segment pass ExchType='D'

For Modifying Bracket/Cover Order only for Initial order (entry)

client.modify_bo_order(ExchOrderID="1100000017861430",LimitPrice=330)
client.modify_cover_order(ExchOrderID="1100000017861430",LimitPrice=330)

#Note : For cover order just pass LimitPriceProfitOrder equal to Zero.

For Modifying LimitPriceProfitOrder

client.modify_bo_order(ExchOrderID="1100000017861430",TargetPrice=330)
client.modify_cover_order(ExchOrderID="1100000017861430",TargetPrice=330)

For Modifying TriggerPriceForSL

client.modify_bo_order(ExchOrderID="1100000017861430",LimitPriceForSL=330)
client.modify_bo_order(ExchOrderID="1100000017861430",LimitPriceForSL=330)

#Note : You have pass atmarket=true while modifying stoploss price, Pass ExchorderId for the particular leg to modify.

Basket Orders

# Create a new Basket
client.create_basket("<New Basket Name>")

# Rename existing basket
client.rename_basket("<Modified Basket Name>",<Exisiting Basket ID>)

# Clone existing basket
client.clone_basket(<Exisiting Basket ID>)

# Delete bulk baskets
delete_basket_list=[{"BasketID":"<Exisiting Basket ID>"},{"BasketID":"<Exisiting Basket ID>"}]
client.delete_basket(delete_basket_list)


# Get list of all baskets (Open/Closed)
client.get_basket()

basket_list= [
            {
                "BasketID": "<Exisiting Basket ID>"
            },
            {
                "BasketID": "<Exisiting Basket ID>"
            }
        ]
order_to_basket=Basket_order("N","C",23000,"BUY",1,"1660","I")
client.add_basket_order(order_to_basket,basket_list)

# Get orders in basket
client.get_order_in_basket(<Exisiting Basket ID>)

Fetching Order Status and Trade Information

from py5paisa.order import  Exchange

req_list= [
        {
            "Exch": "N",
            "ExchType": "C",
            "ScripCode": 20374,
            "ExchOrderID": "1000000015310807"
        }]

# Fetches the trade details
client.fetch_trade_info(req_list)

req_list_= [

        {
            "Exch": "N",
            "RemoteOrderID": "90980441"
        }]
# Fetches the order status
client.fetch_order_status(req_list_)

# Fetch Trade History

print(client.get_trade_history("PASS EXCHANGE ORDER ID"))

Live Market Feed Streaming - Websocket

#NOTE : Webscoket only works with ScripCode

req_list=[
            { "Exch":"N","ExchType":"C","ScripCode":1660},
            ]

req_data=client.Request_Feed('mf','s',req_list)
def on_message(ws, message):
    print(message)


client.connect(req_data)

client.receive_data(on_message)

Note: Use the following abbreviations :

Market Feed=mf

Market Depth (upto 5)=md

Indices (Spot Feed) =i

Open Interest=oi

Subscribe= s

Unsubscribe=u

Live Market Depth Streaming (Depth 20)

a={
                "method":"subscribe",
                "operation":"20depth",
                "instruments":["NC2885"]
            }
print(client.socket_20_depth(a))
def on_message(ws, message):
    print(message)
client.receive_data(on_message)

Note:- Instruments in payload above is a list(array) in format as <exchange><exchange type><scrip code>

Level 5 Market Depth

print(client.fetch_market_depth_by_scrip(Exchange="N",ExchangeType="C",ScripCode="1660"))
print(client.fetch_market_depth_by_scrip(Exchange="N",ExchangeType="C",ScripData="RELIANCE_EQ"))

Full Market Snapshot

a=[{"Exchange":"N","ExchangeType":"C","ScripCode":"2885"},
   {"Exchange":"N","ExchangeType":"C","ScripData":"ITC_EQ"},
   ]
print(client.fetch_market_snapshot(a))

Option Chain

client.get_expiry("N","NIFTY")
# Returns list of all active expiries

# client.get_option_chain("N","NIFTY",<Pass expiry timestamp from get_expiry response>)
client.get_option_chain("N","NIFTY",1647507600000)

Historical Data

#historical_data(<Exchange>,<Exchange Type>,<Scrip Code>,<Time Frame>,<From Data>,<To Date>)

df=client.historical_data('N','C',1660,'15m','2021-05-25','2021-06-16')
print(df)

# Note : TimeFrame Should be from this list ['1m','5m','10m','15m','30m','60m','1d']

Bulk Order Placement

bulk_order=[{
        "Exchange":"N", "ExchangeType":"C", "ScripCode":0, "ScripData":"ITC_EQ", "Price": "440", "OrderType": "Buy", "Qty": 1, "DisQty": "0", "StopLossPrice": "0", "IsIntraday": True, "iOrderValidity": "0", "RemoteOrderID":"50000091_220620"
    },{
        "Exchange":"N", "ExchangeType":"C", "ScripCode":0, "ScripData":"IDEA_EQ", "Price": "15", "OrderType": "Buy", "Qty": 1, "DisQty": "0", "StopLossPrice": "0", "IsIntraday": True, "iOrderValidity": "0", "RemoteOrderID":"50000091_220620"
    }
]
client.place_order_bulk(OrderList=bulk_order)

Strategy Execution

List Of Strategies Available

  • Short Straddle
  • Short Strangle
  • Long Straddle
  • Long Strangle
  • Iron Fly(Butterfly)
  • Iron Condor
  • Call Calendar Spread
  • Put Calendar Spread
  • Call Ladder
  • Put Ladder
  • Ladder
#Import strategy package
from py5paisa.strategy import *

Note: These single-commands are capable of trading multiple legs of pre-defined strategies. Like :- Short/Long Straddles and Strangles, Iron Fly and Iron Condor (many more to come) Please use these at your own risk.

#Create an Object:-
cred={
    "APP_NAME":"YOUR APP_NAME",
    "APP_SOURCE":YOUR APP_SOURCE,
    "USER_ID":"YOUR USER_ID",
    "PASSWORD":"YOUR PASSWORD",
    "USER_KEY":"YOUR USERKEY",
    "ENCRYPTION_KEY":"YOUR ENCRYPTION_KEY"
    }
--Old approach
strategy=strategies(user="random_email@xyz.com", passw="password", dob="YYYYMMDD",cred=cred)
--New Approach
strategy=strategies(cred=cred,request_token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjUwMDUyNzcwIiwicm9sZSI6ImdpUUlvYXR5R2NYQUR3eFYwNXVXSGlPVzJRT1dOTGNzIiwibmJmIjoxNjY3ODMwODczLCJleHAiOjE2Njc4MzA5MDMsImlhdCI6MTY2NzgzMDg3M30.iP_FZtFy-nj6QeRd0sEhaKS-jr-wu-pCwtcdYCGPeO4")

Use the following to execute the strategy (note:- they are executed at market price only)

#short_straddle(<symbol>,<strike price>,<qty>,<expiry>,<Order Type>)
strategy.short_straddle("banknifty",'37000','50','20210610','I',tag='<Your strategy Name>')

#Using tag is optional
#short_strangle(<symbol>,<List of sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.short_strangle("banknifty",['35300','37000'],'50','20210610','D')
#long_straddle(<symbol>,<strike price>,<qty>,<expiry>,<Order Type>)
strategy.long_straddle("banknifty",'37000','50','20210610','I',tag='<Your strategy Name>')

#Using tag is optional
#long_strangle(<symbol>,<List of sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.long_strangle("banknifty",['35300','37000'],'50','20210610','D')
#iron_condor(<symbol>,<List of buy strike prices>,<List of sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.iron_condor("NIFTY",["15000","15200"],["15100","15150"],"75","20210603","I")
#iron_fly(<symbol>,<List of buy strike prices>,<Sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.iron_fly("NIFTY",["15000","15200"],"15100","75","20210610","I",tag='<Your strategy Name>')

#Using tag is optional
#call_calendar(<symbol>,<List of sell strike price>,<qty>,<list of expiry(first one will be bought and the second sold based on expiry)>,<Order Type>)
strategy.call_calendar("nifty",'15600','75',['20210603','20210610'],'I')
#put_calendar(<symbol>,<List of sell strike price>,<qty>,<list of expiry(first one will be bought and the second sold based on expiry)>,<Order Type>)
strategy.put_calendar("nifty",'15600','75',['20210603','20210610'],'I')
#call_ladder(<symbol>,<Buy strike prices>,<List of Sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.call_ladder("NIFTY","15100",["15300","15400"],"75","20210610","I")
#put_ladder(<symbol>,<Buy strike prices>,<List of Sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.put_ladder("NIFTY","15000",["14800","14500"],"75","20210610","I",tag='<Your strategy Name>')

#Using tag is optional
#ladder(<symbol>,<List of Buy strike prices>,<List of Sell strike price>,<qty>,<expiry>,<Order Type>)
strategy.ladder("sbin",["400","420"],["350","370","450","500"],"1500","20210729","D")
Squareoff a strategy Using tags
strategy.squareoff('tag')

# Use the same tag as used while executing the strategies

REPORTS

TAX Report
a=client.tax_report("2024-01-01",'2024-06-26')
print(a)

# to fetch Tax report
Ledger Report
a=client.fetch_ledger("2024-01-01",'2024-06-26')
print(a)

# to fetch Ledger report

TODO

  • Write tests.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

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

py5paisa-0.7.21.2.tar.gz (77.5 kB view details)

Uploaded Source

Built Distribution

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

py5paisa-0.7.21.2-py2.py3-none-any.whl (25.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file py5paisa-0.7.21.2.tar.gz.

File metadata

  • Download URL: py5paisa-0.7.21.2.tar.gz
  • Upload date:
  • Size: 77.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for py5paisa-0.7.21.2.tar.gz
Algorithm Hash digest
SHA256 39e765a6caaf4c7f7719a1fdd6cf295e3fb29cb523a59b935a100434e5bba331
MD5 c912573a760b458de47d42a8d199f120
BLAKE2b-256 43747f3e0f7abcb06ac11325c271011c961db7133a7c8baeca2c03cca2fa12f6

See more details on using hashes here.

File details

Details for the file py5paisa-0.7.21.2-py2.py3-none-any.whl.

File metadata

  • Download URL: py5paisa-0.7.21.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.18

File hashes

Hashes for py5paisa-0.7.21.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6635abc85092bf8b74c2aeef9685f47e40624e2e166b03348350c37f5f88e18e
MD5 139045a8ffa41f7e382ddaa89e16d08b
BLAKE2b-256 ff3750b08c1aa04d6a69e6003da586339f48b64b6090012856699d9e8afb8e0d

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