-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathexample_market.py
More file actions
156 lines (117 loc) · 4.41 KB
/
example_market.py
File metadata and controls
156 lines (117 loc) · 4.41 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
from api_helper import ShoonyaApiPy, get_time
import datetime
import logging
import time
import yaml
import pandas as pd
#sample
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: {0}".format(time.strftime('%d-%m-%Y %H:%M:%S')) + str(message))
def open_callback():
global socket_opened
socket_opened = True
print('app is connected')
api.subscribe('NSE|11630')
#api.subscribe(['NSE|22', 'BSE|522032'])
#end of callbacks
def get_time(time_string):
data = time.strptime(time_string,'%d-%m-%Y %H:%M:%S')
return time.mktime(data)
#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('f => find symbol')
print('m => get quotes')
print('p => contract info n properties')
print('v => get 1 min market data')
print('t => get today 1 min market data')
print('d => get daily data')
print('o => get option chain')
print('s => start_websocket')
print('q => quit')
prompt1=input('what shall we do? ').lower()
if prompt1 == 'v':
start_time = "17-01-2022 00:00:00"
end_time = time.time()
start_secs = get_time(start_time)
ret = api.get_time_price_series(exchange='NSE', token='22', starttime=1642265814, endtime=1642438794, interval=240)
df = pd.DataFrame.from_dict(ret)
print(df)
elif prompt1 == 't':
ret = api.get_time_price_series(exchange='NFO', token='71321')
df = pd.DataFrame.from_dict(ret)
print(df)
elif prompt1 == 'f':
exch = 'MCX'
query = 'CRUDEOIL FEB'
ret = api.searchscrip(exchange=exch, searchtext=query)
print(ret)
if ret != None:
symbols = ret['values']
for symbol in symbols:
print('{0} token is {1}'.format(symbol['tsym'], symbol['token']))
elif prompt1 == 'd':
exch = 'NSE'
tsym = 'RELIANCE-EQ'
ret = api.get_daily_price_series(exchange=exch, tradingsymbol=tsym, startdate=0)
print(ret)
elif prompt1 == 'p':
exch = 'NSE'
token = '22'
ret = api.get_security_info(exchange=exch, token=token)
print(ret)
elif prompt1 == 'm':
exch = 'NSE'
token = '22'
ret = api.get_quotes(exchange=exch, token=token)
print(ret)
elif prompt1 == 'o':
exch = 'MCX'
tsym = 'CRUDEOIL18FEB22'
chain = api.get_option_chain(exchange=exch, tradingsymbol=tsym, strikeprice=4150, count=2)
chainscrips = []
for scrip in chain['values']:
scripdata = api.get_quotes(exchange=scrip['exch'], token=scrip['token'])
chainscrips.append(scripdata)
print(chainscrips)
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:
ret = api.logout()
print(ret)
print('Fin') #an answer that wouldn't be yes or no
break