forked from angel-one/smartapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.py
More file actions
186 lines (159 loc) · 4.44 KB
/
sample.py
File metadata and controls
186 lines (159 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# 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","Your totp")
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=obj.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=obj.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"
}
obj.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))
##Estimate Charges
# params = {
# "orders": [
# {
# "product_type": "DELIVERY",
# "transaction_type": "BUY",
# "quantity": "10",
# "price": "800",
# "exchange": "NSE",
# "symbol_name": "745AS33",
# "token": "17117"
# },
# # {
# # "product_type": "DELIVERY",
# # "transaction_type": "BUY",
# # "quantity": "10",
# # "price": "800",
# # "exchange": "BSE",
# # "symbol_name": "PIICL151223",
# # "token": "726131"
# # }
# ]
# }
# estimateCharges = obj.estimateCharges(params)
# print(estimateCharges);
# params = {
# "isin":"INE528G01035",
# "quantity":"1"
# }
# verifyDis = obj.verifyDis(params)
# print(verifyDis);
# params = {
# "dpId":"33200",
# "ReqId":"2351614738654050",
# "boid":"1203320018563571",
# "pan":"JZTPS2255C"
# }
# generateTPIN = obj.generateTPIN(params)
# print(generateTPIN);
# params = {
# "ReqId":"2351614738654050"
# }
# getTranStatus = obj.getTranStatus(params)
# print(getTranStatus);
# params = {
# "name":"TCS",
# "expirydate":"25JAN2024"
# }
# optionGreek = obj.optionGreek(params)
# print(optionGreek);
# params = {
# "datatype":"PercOIGainers",
# "expirytype":"NEAR"
# }
# gainersLosers = obj.gainersLosers(params)
# print(gainersLosers);
# putCallRatio = obj.putCallRatio()
# print(putCallRatio);
# params = {
# "expirytype":"NEAR",
# "datatype":"Long Built Up"
# }
# OIBuildup = obj.oIBuildup(params)
# print(OIBuildup);
## WebSocket
from SmartApi.webSocket 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()