forked from L30705/smartapi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
141 lines (110 loc) · 2.8 KB
/
test.py
File metadata and controls
141 lines (110 loc) · 2.8 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
from smartapi import SmartConnect
#---------for smartExceptions---------
#import smartapi.smartExceptions
#or
#from smartapi import smartExceptions
smartApi =SmartConnect(api_key="Your Api Key")
login = smartApi.generateSession('Your Client Id', 'Your Password')
refreshToken = login['data']['refreshToken']
feedToken = smartApi.getfeedToken()
smartApi.getProfile(refreshToken)
smartApi.generateToken(refreshToken)
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)
modifyparams = {
"variety": "NORMAL",
"orderid": orderid,
"ordertype": "LIMIT",
"producttype": "INTRADAY",
"duration": "DAY",
"price": "19500",
"quantity": "1",
"tradingsymbol":"SBIN-EQ",
"symboltoken":"3045",
"exchange":"NSE"
}
smartApi.modifyOrder(modifyparams)
smartApi.cancelOrder(orderid, "NORMAL")
smartApi.orderBook()
smartApi.tradeBook()
smartApi.rmsLimit()
smartApi.position()
smartApi.holding()
exchange = "NSE"
tradingsymbol = "SBIN-EQ"
symboltoken = 3045
smartApi.ltpData("NSE", "SBIN-EQ", "3045")
params={
"exchange": "NSE",
"oldproducttype":"DELIVERY",
"newproducttype": "MARGIN",
"tradingsymbol": "SBIN-EQ",
"transactiontype":"BUY",
"quantity":1,
"type":"DAY"
}
smartApi.convertPosition(params)
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)
gttModifyParams={
"id": rule_id,
"symboltoken":"3045",
"exchange":"NSE",
"price":19500,
"quantity":10,
"triggerprice":200000,
"disclosedqty":10,
"timeperiod":365
}
modified_id=smartApi.gttModifyRule(gttModifyParams)
cancelParams={
"id": rule_id,
"symboltoken":"3045",
"exchange":"NSE"
}
cancelled_id=smartApi.gttCancelRule(cancelParams)
smartApi.gttDetails(rule_id)
smartApi.gttLists('List of status',<page>,<count>)
smartApi.terminateSession('Your Client Id')
## Websocket Programming
from smartapi import WebSocket
FEED_TOKEN=feedToken
CLIENT_CODE="Your Client Id"
token=None
task=None
ss = WebSocket(FEED_TOKEN, CLIENT_CODE)
def on_tick(ws, tick):
print("Ticks: {}".format(tick))
def on_connect(ws, response):
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( )