-
Notifications
You must be signed in to change notification settings - Fork 134
/
example_orders.py
139 lines (112 loc) · 4.06 KB
/
example_orders.py
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
from api_helper import ShoonyaApiPy, get_time
import datetime
import logging
import time
import yaml
import pandas as pd
logging.basicConfig(level=logging.DEBUG)
#flag to tell us if the websocket is open
socket_opened = False
#application callbacks
def event_handler_order_update(message):
print("order event: " + str(message))
def event_handler_quote_update(message):
#e Exchange
#tk Token
#lp LTP
#pc Percentage change
#v volume
#o Open price
#h High price
#l Low price
#c Close price
#ap Average trade price
print("quote event: " + str(message))
def open_callback():
global socket_opened
socket_opened = True
print('app is connected')
#api.subscribe_orders()
api.subscribe('NSE|22')
#api.subscribe(['NSE|22', 'BSE|522032'])
#end of callbacks
#start of our program
api = ShoonyaApiPy()
#use following if yaml isnt used
#user = <uid>
#pwd = <password>
#factor2 = <2nd factor>
#vc = <vendor code>
#apikey = <secret key>
#imei = <imei>
#ret = api.login(userid = user, password = pwd, twoFA=factor2, vendor_code=vc, api_secret=apikey, imei=imei)
#yaml for parameters
with open('cred.yml') as f:
cred = yaml.load(f, Loader=yaml.FullLoader)
print(cred)
ret = api.login(userid = cred['user'], password = cred['pwd'], twoFA=cred['factor2'], vendor_code=cred['vc'], api_secret=cred['apikey'], imei=cred['imei'])
if ret != None:
while True:
print('p => place order')
print('m => modify order')
print('c => cancel order')
print('y => order history')
print('o => get order book')
print('h => get holdings')
print('l => get limits')
print('k => get positions')
print('d => get daily mtm')
print('s => start_websocket')
print('q => quit')
prompt1=input('what shall we do? ').lower()
if prompt1 == 'p':
ret = api.place_order(buy_or_sell='B', product_type='C',
exchange='NSE', tradingsymbol='INFY-EQ',
quantity=1, discloseqty=0,price_type='LMT', price=1500.00, trigger_price=None,
retention='DAY', remarks='my_order_001')
print(ret)
elif prompt1 == 'm':
orderno=input('Enter orderno:').lower()
ret = api.modify_order(exchange='NSE', tradingsymbol='INFY-EQ', orderno=orderno,
newquantity=2, newprice_type='LMT', newprice=1505.00)
print(ret)
elif prompt1 == 'c':
orderno=input('Enter orderno:').lower()
ret = api.cancel_order(orderno=orderno)
print(ret)
elif prompt1 == 'y':
orderno=input('Enter orderno:').lower()
ret = api.single_order_history(orderno=orderno)
print(ret)
elif prompt1 == 'o':
ret = api.get_order_book()
print(ret)
elif prompt1 == 'h':
ret = api.get_holdings()
print(ret)
elif prompt1 == 'l':
ret = api.get_limits()
print(ret)
elif prompt1 == 'k':
ret = api.get_positions()
print(ret)
elif prompt1 == 'd':
#contributed by Aromal P Nair
while True:
ret = api.get_positions()
mtm = 0
pnl = 0
for i in ret:
mtm += float(i['urmtom'])
pnl += float(i['rpnl'])
day_m2m = mtm + pnl
print(day_m2m)
elif prompt1 == 's':
if socket_opened == True:
print('websocket already opened')
continue
ret = api.start_websocket(order_update_callback=event_handler_order_update, subscribe_callback=event_handler_quote_update, socket_open_callback=open_callback)
print(ret)
else:
print('Fin') #an answer that wouldn't be yes or no
break