# package import statement1from SmartApi import SmartConnect #or from smartapi.smartConnect import SmartConnect23#import smartapi.smartExceptions(for smartExceptions)45#create object of call6obj=SmartConnect(api_key="your api key")78#login api call910data = obj.generateSession("Your Client ID","Your Password","Your totp")1112refreshToken= data['data']['refreshToken']1314#fetch the feedtoken15feedToken=obj.getfeedToken()1617#fetch User Profile18userProfile= obj.getProfile(refreshToken)19#place order20try:21orderparams = {22"variety": "NORMAL",23"tradingsymbol": "SBIN-EQ",24"symboltoken": "3045",25"transactiontype": "BUY",26"exchange": "NSE",27"ordertype": "LIMIT",28"producttype": "INTRADAY",29"duration": "DAY",30"price": "19500",31"squareoff": "0",32"stoploss": "0",33"quantity": "1"34}35orderId=obj.placeOrder(orderparams)36print("The order id is: {}".format(orderId))37except Exception as e:38print("Order placement failed: {}".format(e.message))39#gtt rule creation40try:41gttCreateParams={42"tradingsymbol" : "SBIN-EQ",43"symboltoken" : "3045",44"exchange" : "NSE",45"producttype" : "MARGIN",46"transactiontype" : "BUY",47"price" : 100000,48"qty" : 10,49"disclosedqty": 10,50"triggerprice" : 200000,51"timeperiod" : 36552}53rule_id=obj.gttCreateRule(gttCreateParams)54print("The GTT rule id is: {}".format(rule_id))55except Exception as e:56print("GTT Rule creation failed: {}".format(e.message))5758#gtt rule list59try:60status=["FORALL"] #should be a list61page=162count=1063lists=obj.gttLists(status,page,count)64except Exception as e:65print("GTT Rule List failed: {}".format(e.message))6667#Historic api68try:69historicParam={70"exchange": "NSE",71"symboltoken": "3045",72"interval": "ONE_MINUTE",73"fromdate": "2021-02-08 09:00",74"todate": "2021-02-08 09:16"75}76obj.getCandleData(historicParam)77except Exception as e:78print("Historic Api failed: {}".format(e.message))79#logout80try:81logout=obj.terminateSession('Your Client Id')82print("Logout Successfull")83except Exception as e:84print("Logout failed: {}".format(e.message))8586##Estimate Charges87# params = {88# "orders": [89# {90# "product_type": "DELIVERY",91# "transaction_type": "BUY",92# "quantity": "10",93# "price": "800",94# "exchange": "NSE",95# "symbol_name": "745AS33",96# "token": "17117"97# },98# # {99# # "product_type": "DELIVERY",100# # "transaction_type": "BUY",101# # "quantity": "10",102# # "price": "800",103# # "exchange": "BSE",104# # "symbol_name": "PIICL151223",105# # "token": "726131"106# # }107# ]108# }109# estimateCharges = obj.estimateCharges(params)110# print(estimateCharges);111112# params = {113# "isin":"INE528G01035",114# "quantity":"1"115# }116# verifyDis = obj.verifyDis(params)117# print(verifyDis);118119# params = {120# "dpId":"33200",121# "ReqId":"2351614738654050",122# "boid":"1203320018563571",123# "pan":"JZTPS2255C"124# }125# generateTPIN = obj.generateTPIN(params)126# print(generateTPIN);127128# params = {129# "ReqId":"2351614738654050"130# }131# getTranStatus = obj.getTranStatus(params)132# print(getTranStatus);133134# params = {135# "name":"TCS",136# "expirydate":"25JAN2024"137# }138# optionGreek = obj.optionGreek(params)139# print(optionGreek);140141# params = {142# "datatype":"PercOIGainers",143# "expirytype":"NEAR"144# }145# gainersLosers = obj.gainersLosers(params)146# print(gainersLosers);147148# putCallRatio = obj.putCallRatio()149# print(putCallRatio);150151# params = {152# "expirytype":"NEAR",153# "datatype":"Long Built Up"154# }155# OIBuildup = obj.oIBuildup(params)156# print(OIBuildup);157158## WebSocket159160from SmartApi.webSocket import WebSocket161162FEED_TOKEN= "your feed token"163CLIENT_CODE="your client Id"164token="channel you want the information of" #"nse_cm|2885&nse_cm|1594&nse_cm|11536"165task="task" #"mw"|"sfi"|"dp"166ss = WebSocket(FEED_TOKEN, CLIENT_CODE)167168def on_tick(ws, tick):169print("Ticks: {}".format(tick))170171def on_connect(ws, response):172ws.websocket_connection() # Websocket connection173ws.send_request(token,task)174175def on_close(ws, code, reason):176ws.stop()177178# Assign the callbacks.179ss.on_ticks = on_tick180ss.on_connect = on_connect181ss.on_close = on_close182183ss.connect()184185186187188