Upload api_helper.py
Browse files- api_helper.py +74 -0
api_helper.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from NorenRestApiPy.NorenApi import NorenApi
|
| 2 |
+
from threading import Timer
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import time
|
| 5 |
+
import concurrent.futures
|
| 6 |
+
|
| 7 |
+
api = None
|
| 8 |
+
class Order:
|
| 9 |
+
def __init__(self, buy_or_sell:str = None, product_type:str = None,
|
| 10 |
+
exchange: str = None, tradingsymbol:str =None,
|
| 11 |
+
price_type: str = None, quantity: int = None,
|
| 12 |
+
price: float = None,trigger_price:float = None, discloseqty: int = 0,
|
| 13 |
+
retention:str = 'DAY', remarks: str = "tag",
|
| 14 |
+
order_id:str = None):
|
| 15 |
+
self.buy_or_sell=buy_or_sell
|
| 16 |
+
self.product_type=product_type
|
| 17 |
+
self.exchange=exchange
|
| 18 |
+
self.tradingsymbol=tradingsymbol
|
| 19 |
+
self.quantity=quantity
|
| 20 |
+
self.discloseqty=discloseqty
|
| 21 |
+
self.price_type=price_type
|
| 22 |
+
self.price=price
|
| 23 |
+
self.trigger_price=trigger_price
|
| 24 |
+
self.retention=retention
|
| 25 |
+
self.remarks=remarks
|
| 26 |
+
self.order_id=None
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
#print(ret)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def get_time(time_string):
|
| 35 |
+
data = time.strptime(time_string,'%d-%m-%Y %H:%M:%S')
|
| 36 |
+
|
| 37 |
+
return time.mktime(data)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class ShoonyaApiPy(NorenApi):
|
| 41 |
+
def __init__(self):
|
| 42 |
+
NorenApi.__init__(self, host='https://api.shoonya.com/NorenWClientTP/', websocket='wss://api.shoonya.com/NorenWSTP/')
|
| 43 |
+
global api
|
| 44 |
+
api = self
|
| 45 |
+
|
| 46 |
+
def place_basket(self, orders):
|
| 47 |
+
|
| 48 |
+
resp_err = 0
|
| 49 |
+
resp_ok = 0
|
| 50 |
+
result = []
|
| 51 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
| 52 |
+
|
| 53 |
+
future_to_url = {executor.submit(self.place_order, order): order for order in orders}
|
| 54 |
+
for future in concurrent.futures.as_completed(future_to_url):
|
| 55 |
+
url = future_to_url[future]
|
| 56 |
+
try:
|
| 57 |
+
result.append(future.result())
|
| 58 |
+
except Exception as exc:
|
| 59 |
+
print(exc)
|
| 60 |
+
resp_err = resp_err + 1
|
| 61 |
+
else:
|
| 62 |
+
resp_ok = resp_ok + 1
|
| 63 |
+
|
| 64 |
+
return result
|
| 65 |
+
|
| 66 |
+
def placeOrder(self,order: Order):
|
| 67 |
+
ret = NorenApi.place_order(self, buy_or_sell=order.buy_or_sell, product_type=order.product_type,
|
| 68 |
+
exchange=order.exchange, tradingsymbol=order.tradingsymbol,
|
| 69 |
+
quantity=order.quantity, discloseqty=order.discloseqty, price_type=order.price_type,
|
| 70 |
+
price=order.price, trigger_price=order.trigger_price,
|
| 71 |
+
retention=order.retention, remarks=order.remarks)
|
| 72 |
+
#print(ret)
|
| 73 |
+
|
| 74 |
+
return ret
|