Upstox Python
Upstox Python
MIT License
38 stars 43 forks
Star Watch
master Go to file
View code
README.md
Upstox Python library provides an easy to use wrapper over the HTTPs APIs.
The HTTP calls have been converted to methods and JSON responses are
wrapped into Python-compatible objects.
Installation
This module is installed via pip:
Prerequisites
Python 2.x or 3.x
• future
• websocket_client
• requests
Overview
There are two classes in the library: Session, and Upstox. The Session class is
used to retrieve an access token from the server. An access token is valid for
24 hours. With an access token, you can instantiate an Upstox object. Ideally
you only need to create a Session object once every day. After you have the
access token, you can store it separately for re-use.
REST Documentation
The original REST API that this SDK is based on is available online. Upstox API
REST documentation
Sample Program
An interactive Python program to test your connectivity and show
functionality is available on Gist.
s = Session ('your_api_key')
s.set_redirect_uri ('your_redirect_uri')
s.set_api_secret ('your_api_secret')
3. Get the login URL so you can login with your Upstox UCC ID and
password.
print (s.get_login_url())
## this will return a URL such as https://api.upstox.com/index/dialog/authorize?apiKey={
4. Login to the URL and set the code returned by the login response in your
Session object
s.set_code ('your_code_from_login_response')
access_token = s.retrieve_access_token()
print ('Received access_token: %s' % access_token)
Establish a session
1. Once you have your access_token , you can create an Upstox object with
your access_token and api_key .
Search for a single instrument by it's token number (generally useful only for
BSE Equities):
Instrument object
All instruments have the fields mentioned above. Wherever a field is not
applicable for an instrument (for example, equity instruments don't have strike
prices), that value will be None
Quote update
Once you have master contracts loaded, you can easily subscribe to quote
updates.
You can either subscribe for a full quote update or an LTP quote update. Using
the LiveFeedType object, you can specify whether you want the full feed
( LiveFeedType.Full ) or just the LTP feed ( LiveFeedType.LTP )
u.set_on_quote_update(event_handler_quote_update)
u.start_websocket(True)
Place an order
Place limit, market, SL, SL-M, AMO, OCO/BO, CO orders
print (u.get_profile())
u.get_master_contract('nse_eq') # get contracts for NSE EQ
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, #transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), #instrument
1, # quantity
OrderType.Market, # order_type
ProductType.Delivery, # product_type
0.0, # price
None, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None )# trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Market, # order_type
ProductType.Intraday, # product_type
0.0, # price
None, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None )# trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Market, # order_type
ProductType.CoverOrder, # product_type
0.0, # price
7.5, # trigger_price Here the trigger_price is taken as stop loss (provid
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Limit, # order_type
ProductType.Delivery, # product_type
8.0, # price
None, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%5%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Limit, # order_type
ProductType.Intraday, # product_type
8.0, # price
None, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%6%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Limit, # order_type
ProductType.CoverOrder, # product_type
8.0, # price
8.0, # trigger_price Here the trigger_price is taken as stop loss (pro
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks 20 * 0.05
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%7%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.Limit, # order_type
ProductType.OneCancelsOther, # product_type
8.0, # price
None, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
1.0, # stop_loss
1.0, # square_off
20) # trailing_ticks 20 * 0.05
)
###############################
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.StopLossMarket, # order_type
ProductType.Delivery, # product_type
0.0, # price
8.0, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
)
###################################
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.StopLossLimit, # order_type
ProductType.Delivery, # product_type
8.0, # price
8.0, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%11%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.StopLossLimit, # order_type
ProductType.Intraday, # product_type
8.0, # price
8.0, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
None, # stop_loss
None, # square_off
None) # trailing_ticks
)
print ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
print(
u.place_order(TransactionType.Buy, # transaction_type
u.get_instrument_by_symbol('NSE_EQ', 'UNITECH'), # instrument
1, # quantity
OrderType.StopLossLimit, # order_type
ProductType.OneCancelsOther, # product_type
8.0, # price
8.0, # trigger_price
0, # disclosed_quantity
DurationType.DAY, # duration
1.0, # stop_loss
1.0, # square_off
20) # trailing_ticks 20 * 0.05
)
Cancel an order
Order properties as objects
Order properties such as TransactionType, OrderType, and others have been
safely classified as objects so you don't have to write them out as strings
TransactionType
Transaction types indicate whether you want to buy or sell. Valid transaction
types are of the following:
• TransactionType.Buy - buy
• TransactionType.Sell - sell
OrderType
Order type specifies the type of order you want to send. Valid order types
include:
ProductType
Product types indicate the complexity of the order you want to place. Valid
product types are:
DurationType
Duration types specify how long your order will stay on the market. Valid
duration types include:
• Quote update
• Trade update
• Order update
Upstox uses the Websocket library to provide real-time updates for these
events
1. Before starting the websocket, attach event handlers to the events you
want to be notified for
u.set_on_order_update (my_generic_event_handler)
u.set_on_quote_update (my_generic_event_handler)
u.start_websocket (False)
To run it in the background, please ensure that your program doesn't exit.
Here's an example of
u.start_websocket (True)
condition = threading.Condition()
condition.acquire()
condition.wait()
Releases 2
1.5.1 Latest
Jun 30, 2017
+ 1 release
Packages
No packages published
Contributors 2
AlokGaira
svishi Shrini
Languages
Python 100.0%