Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
angel-one
GitHub Repository: angel-one/smartapi-python
Path: blob/main/example/sample.py
410 views
1
# package import statement
2
from SmartApi import SmartConnect #or from smartapi.smartConnect import SmartConnect
3
4
#import smartapi.smartExceptions(for smartExceptions)
5
6
#create object of call
7
obj=SmartConnect(api_key="your api key")
8
9
#login api call
10
11
data = obj.generateSession("Your Client ID","Your Password","Your totp")
12
13
refreshToken= data['data']['refreshToken']
14
15
#fetch the feedtoken
16
feedToken=obj.getfeedToken()
17
18
#fetch User Profile
19
userProfile= obj.getProfile(refreshToken)
20
#place order
21
try:
22
orderparams = {
23
"variety": "NORMAL",
24
"tradingsymbol": "SBIN-EQ",
25
"symboltoken": "3045",
26
"transactiontype": "BUY",
27
"exchange": "NSE",
28
"ordertype": "LIMIT",
29
"producttype": "INTRADAY",
30
"duration": "DAY",
31
"price": "19500",
32
"squareoff": "0",
33
"stoploss": "0",
34
"quantity": "1"
35
}
36
orderId=obj.placeOrder(orderparams)
37
print("The order id is: {}".format(orderId))
38
except Exception as e:
39
print("Order placement failed: {}".format(e.message))
40
#gtt rule creation
41
try:
42
gttCreateParams={
43
"tradingsymbol" : "SBIN-EQ",
44
"symboltoken" : "3045",
45
"exchange" : "NSE",
46
"producttype" : "MARGIN",
47
"transactiontype" : "BUY",
48
"price" : 100000,
49
"qty" : 10,
50
"disclosedqty": 10,
51
"triggerprice" : 200000,
52
"timeperiod" : 365
53
}
54
rule_id=obj.gttCreateRule(gttCreateParams)
55
print("The GTT rule id is: {}".format(rule_id))
56
except Exception as e:
57
print("GTT Rule creation failed: {}".format(e.message))
58
59
#gtt rule list
60
try:
61
status=["FORALL"] #should be a list
62
page=1
63
count=10
64
lists=obj.gttLists(status,page,count)
65
except Exception as e:
66
print("GTT Rule List failed: {}".format(e.message))
67
68
#Historic api
69
try:
70
historicParam={
71
"exchange": "NSE",
72
"symboltoken": "3045",
73
"interval": "ONE_MINUTE",
74
"fromdate": "2021-02-08 09:00",
75
"todate": "2021-02-08 09:16"
76
}
77
obj.getCandleData(historicParam)
78
except Exception as e:
79
print("Historic Api failed: {}".format(e.message))
80
#logout
81
try:
82
logout=obj.terminateSession('Your Client Id')
83
print("Logout Successfull")
84
except Exception as e:
85
print("Logout failed: {}".format(e.message))
86
87
##Estimate Charges
88
# params = {
89
# "orders": [
90
# {
91
# "product_type": "DELIVERY",
92
# "transaction_type": "BUY",
93
# "quantity": "10",
94
# "price": "800",
95
# "exchange": "NSE",
96
# "symbol_name": "745AS33",
97
# "token": "17117"
98
# },
99
# # {
100
# # "product_type": "DELIVERY",
101
# # "transaction_type": "BUY",
102
# # "quantity": "10",
103
# # "price": "800",
104
# # "exchange": "BSE",
105
# # "symbol_name": "PIICL151223",
106
# # "token": "726131"
107
# # }
108
# ]
109
# }
110
# estimateCharges = obj.estimateCharges(params)
111
# print(estimateCharges);
112
113
# params = {
114
# "isin":"INE528G01035",
115
# "quantity":"1"
116
# }
117
# verifyDis = obj.verifyDis(params)
118
# print(verifyDis);
119
120
# params = {
121
# "dpId":"33200",
122
# "ReqId":"2351614738654050",
123
# "boid":"1203320018563571",
124
# "pan":"JZTPS2255C"
125
# }
126
# generateTPIN = obj.generateTPIN(params)
127
# print(generateTPIN);
128
129
# params = {
130
# "ReqId":"2351614738654050"
131
# }
132
# getTranStatus = obj.getTranStatus(params)
133
# print(getTranStatus);
134
135
# params = {
136
# "name":"TCS",
137
# "expirydate":"25JAN2024"
138
# }
139
# optionGreek = obj.optionGreek(params)
140
# print(optionGreek);
141
142
# params = {
143
# "datatype":"PercOIGainers",
144
# "expirytype":"NEAR"
145
# }
146
# gainersLosers = obj.gainersLosers(params)
147
# print(gainersLosers);
148
149
# putCallRatio = obj.putCallRatio()
150
# print(putCallRatio);
151
152
# params = {
153
# "expirytype":"NEAR",
154
# "datatype":"Long Built Up"
155
# }
156
# OIBuildup = obj.oIBuildup(params)
157
# print(OIBuildup);
158
159
## WebSocket
160
161
from SmartApi.webSocket import WebSocket
162
163
FEED_TOKEN= "your feed token"
164
CLIENT_CODE="your client Id"
165
token="channel you want the information of" #"nse_cm|2885&nse_cm|1594&nse_cm|11536"
166
task="task" #"mw"|"sfi"|"dp"
167
ss = WebSocket(FEED_TOKEN, CLIENT_CODE)
168
169
def on_tick(ws, tick):
170
print("Ticks: {}".format(tick))
171
172
def on_connect(ws, response):
173
ws.websocket_connection() # Websocket connection
174
ws.send_request(token,task)
175
176
def on_close(ws, code, reason):
177
ws.stop()
178
179
# Assign the callbacks.
180
ss.on_ticks = on_tick
181
ss.on_connect = on_connect
182
ss.on_close = on_close
183
184
ss.connect()
185
186
187
188