Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

108 Commits

Repository files navigation

SMARTAPI-PYTHON

SMARTAPI-PYTHON is a Python library for interacting with Angel's Trading platform ,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 -r requirements_dev.txt # for downloading the other required packages
pip install smartapi-python
pip install websocket-client

Usage

# package import statementfromSmartApiimportSmartConnect#or from SmartApi.smartConnect import SmartConnectimportpyotpapi_key='Your Api Key'clientId='Your Client Id'pwd='Your Pin'smartApi=SmartConnect(api_key)
token="Your QR code value"totp=pyotp.TOTP(token).now()
correlation_id="abc123"# login api calldata=smartApi.generateSession(clientId, pwd, totp)
# print(data)authToken=data['data']['jwtToken']
refreshToken=data['data']['refreshToken']
# fetch the feedtokenfeedToken=smartApi.getfeedToken()
# fetch User Profileres=smartApi.getProfile(refreshToken)
smartApi.generateToken(refreshToken)
res=res['data']['exchanges']
#place ordertry:
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=smartApi.placeOrder(orderparams)
print("The order id is: {}".format(orderId))
exceptExceptionase:
print("Order placement failed: {}".format(e.message))
#gtt rule creationtry:
gttCreateParams={
"tradingsymbol" : "SBIN-EQ",
"symboltoken" : "3045",
"exchange" : "NSE", "producttype" : "MARGIN",
"transactiontype" : "BUY",
"price" : 100000,
"qty" : 10,
"disclosedqty": 10,
"triggerprice" : 200000,
"timeperiod" : 365
}
rule_id=smartApi.gttCreateRule(gttCreateParams)
print("The GTT rule id is: {}".format(rule_id))
exceptExceptionase:
print("GTT Rule creation failed: {}".format(e.message))
#gtt rule listtry:
status=["FORALL"] #should be a listpage=1count=10lists=smartApi.gttLists(status,page,count)
exceptExceptionase:
print("GTT Rule List failed: {}".format(e.message))
#Historic apitry:
historicParam={
"exchange": "NSE",
"symboltoken": "3045",
"interval": "ONE_MINUTE",
"fromdate": "2021-02-08 09:00", "todate": "2021-02-08 09:16"
}
smartApi.getCandleData(historicParam)
exceptExceptionase:
print("Historic Api failed: {}".format(e.message))
#logouttry:
logout=smartApi.terminateSession('Your Client Id')
print("Logout Successfull")
exceptExceptionase:
print("Logout failed: {}".format(e.message))

Getting started with SmartAPI Websocket's

fromSmartApiimportSmartWebSocket# feed_token=092017047FEED_TOKEN="YOUR_FEED_TOKEN"CLIENT_CODE="YOUR_CLIENT_CODE"# token="mcx_fo|224395"token="EXCHANGE|TOKEN_SYMBOL"#SAMPLE: nse_cm|2885&nse_cm|1594&nse_cm|11536&nse_cm|3045# token="mcx_fo|226745&mcx_fo|220822&mcx_fo|227182&mcx_fo|221599"task="mw"# mw|sfi|dpss=SmartWebSocket(FEED_TOKEN, CLIENT_CODE)
defon_message(ws, message):
print("Ticks: {}".format(message))
defon_open(ws):
print("on open")
ss.subscribe(task,token)
defon_error(ws, error):
print(error)
defon_close(ws):
print("Close")
# Assign the callbacks.ss._on_open=on_openss._on_message=on_messagess._on_error=on_errorss._on_close=on_closess.connect()
####### Websocket sample code ended here ############## Websocket V2 sample code #######fromSmartApi.smartWebSocketV2importSmartWebSocketV2fromlogzeroimportloggerAUTH_TOKEN="Your Auth_Token"API_KEY="Your Api_Key"CLIENT_CODE="Your Client Code"FEED_TOKEN="Your Feed_Token"correlation_id="abc123"action=1mode=1token_list= [
{
"exchangeType": 1,
"tokens": ["26009"]
}
]
sws=SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN)
defon_data(wsapp, message):
logger.info("Ticks: {}".format(message))
# close_connection()defon_open(wsapp):
logger.info("on open")
sws.subscribe(correlation_id, mode, token_list)
defon_error(wsapp, error):
logger.error(error)
defon_close(wsapp):
logger.info("Close")
defclose_connection():
sws.close_connection()
# Assign the callbacks.sws.on_open=on_opensws.on_data=on_datasws.on_error=on_errorsws.on_close=on_closesws.connect()

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages