Angel Broking openApi integration
Project description
SMARTAPI-PYTHON
SMARTAPI-PYTHON is a Python library for dealing AMX,that is a set of REST-like HTTP APIs that expose many capabilities required to build stock market investment and trading platforms. It lets you execute orders in real time.
Installation
Use the package manager pip to install smartapi-python.
pip install smartapi-python
Usage
# package import statement
from smartapi import SmartConnect #or from smartapi.smartConnect import SmartConnect
#import smartapi.smartExceptions(for smartExceptions)
#create object of call
obj=SmartConnect(api_key="your api key")
#login api call
data = obj.generateSession("Your Client ID","Your Password")
refreshToken= data['data']['refreshToken']
#fetch the feedtoken
feedToken=obj.getfeedToken()
#fetch User Profile
userProfile= obj.getProfile(refreshToken)
#place order
try:
orderparams = {
"variety": "NORMAL",
"tradingsymbol": "SBIN-EQ",
"symboltoken": "3045",
"transactiontype": "BUY",
"exchange": "NSE",
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "19500",
"squareoff": "0",
"stoploss": "0",
"quantity": "1"
}
orderId=obj.placeOrder(orderparams)
print("The order id is: {}".format(orderId))
except Exception as e:
print("Order placement failed: {}".format(e.message))
#gtt rule creation
try:
gttCreateParams={
"tradingsymbol" : "SBIN-EQ",
"symboltoken" : "3045",
"exchange" : "NSE",
"producttype" : "MARGIN",
"transactiontype" : "BUY",
"price" : 100000,
"qty" : 10,
"disclosedqty": 10,
"triggerprice" : 200000,
"timeperiod" : 365
}
rule_id=gtt.gttCreateRule(gttCreateParams)
print("The GTT rule id is: {}".format(rule_id))
except Exception as e:
print("GTT Rule creation failed: {}".format(e.message))
#gtt rule list
try:
status=["FORALL"] #should be a list
page=1
count=10
lists=smartApi.gttLists(status,page,count)
except Exception as e:
print("GTT Rule List failed: {}".format(e.message))
#Historic api
try:
historicParam={
"exchange": "NSE",
"symboltoken": "3045",
"interval": "ONE_MINUTE",
"fromdate": "2021-02-08 09:00",
"todate": "2021-02-08 09:16"
}
smartApi.getCandleData(historicParam)
except Exception as e:
print("Historic Api failed: {}".format(e.message))
#logout
try:
logout=obj.terminateSession('Your Client Id')
print("Logout Successfull")
except Exception as e:
print("Logout failed: {}".format(e.message))
## WebSocket
from smartapi import WebSocket
FEED_TOKEN= "your feed token"
CLIENT_CODE="your client Id"
token="channel you want the information of" #"nse_cm|2885&nse_cm|1594&nse_cm|11536"
task="task" #"mw"|"sfi"|"dp"
ss = WebSocket(FEED_TOKEN, CLIENT_CODE)
def on_tick(ws, tick):
print("Ticks: {}".format(tick))
def on_connect(ws, response):
ws.websocket_connection() # Websocket connection
ws.send_request(token,task)
def on_close(ws, code, reason):
ws.stop()
# Assign the callbacks.
ss.on_ticks = on_tick
ss.on_connect = on_connect
ss.on_close = on_close
ss.connect( )
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for smartapi_python-1.2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8b3e37ed6f332f2f82832a684d6fb4454f7b675c933087ea848122c14eca5b7 |
|
MD5 | e8a25a2bd499a509b5c7c8cca1b211ff |
|
BLAKE2b-256 | cd860f66d06a96afc4c6bb3ce3cd653cf38edae5118a3150506e06b1d8f59a70 |