Skip to content

Commit

Permalink
version 0.0.2
Browse files Browse the repository at this point in the history
working distribution
  • Loading branch information
kambalatech committed Aug 21, 2021
1 parent a711c97 commit 131b56d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 27 deletions.
40 changes: 40 additions & 0 deletions NorenRestApiPy/NorenApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProductType(enum.Enum):
Delivery = 'C'
Intraday = 'I'
Normal = 'M'
CF = 'M'

class PriceType(enum.Enum):
Market = 'MKT'
Expand All @@ -39,6 +40,14 @@ def reportmsg(msg):
#print(msg)
logger.debug(msg)

def reporterror(msg):
#print(msg)
logger.error(msg)

def reportinfo(msg):
#print(msg)
logger.info(msg)

class NorenApi:
__service_config = {
'host': 'http://wsapihost/',
Expand All @@ -48,6 +57,7 @@ class NorenApi:
'modifyorder': '/ModifyOrder',
'cancelorder': 'CancelOrder',
'orderbook': '/OrderBook',
'searchscrip': '/SearchScrip',
'TPSeries' : '/TPSeries',
},
'websocket_endpoint': 'wss://wsendpoint/'
Expand Down Expand Up @@ -371,6 +381,36 @@ def get_order_book(self):

return resDict

def searchscrip(self, exchange, searchtext):
config = NorenApi.__service_config

#prepare the uri
url = f"{config['host']}{config['routes']['searchscrip']}"
reportmsg(url)

if searchtext == None:
reporterror('search text cannot be null')
return None

values = {}
values["uid"] = self.__username
values["exch"] = exchange
values["stext"] = searchtext

payload = 'jData=' + json.dumps(values) + f'&jKey={self.__susertoken}'

reportmsg(payload)

res = requests.post(url, data=payload)
reportmsg(res.text)

resDict = json.loads(res.text)

if resDict['stat'] != 'Ok':
return None

return resDict

def get_time_price_series(self, exchange, token, starttime=None, endtime=None):
config = NorenApi.__service_config

Expand Down
75 changes: 49 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NorenAPI
# NorenApi

Api used to connect to StarUAT
Api used to connect to NorenOMS
****

## Build
Expand All @@ -16,8 +16,9 @@ to build this package and install it on your server please use
class ```NorenApi```
- [login](#md-login)
- [place_order](#md-place_order)
- [modify_order](#md-place_order)
- [cancel_order](#md-place_order)
- [modify_order](#md-modify_order)
- [cancel_order](#md-cancel_order)
- [searchscrip](#md-searchscrip)
- [start_websocket](#md-start_websocket)
- [subscribe](#md-subscribe)
- [unsubscribe](#md-unsubscribe)
Expand Down Expand Up @@ -72,6 +73,30 @@ cancel an order
| --- | --- | --- | ---|
| orderno | ```string``` | False | orderno with status open |

#### <a name="md-searchscrip"></a> searchscrip(exchange, searchtext):
search for scrip or contract and its properties

| Param | Type | Optional |Description |
| --- | --- | --- | ---|
| exchange | ```string``` | True | Exchange NSE / NFO / BSE / CDS |
| searchtext | ```string``` | True | Search Text ie partial or complete text ex: INFY-EQ, INF.. |

the response is as follows,

| Param | Type | Optional |Description |
| --- | --- | --- | ---|
| stat | ```string``` | True | ok or Not_ok |
| values | ```string``` | True | properties of the scrip |
| emsg | ```string``` | False | Error Message |

| Param | Type | Optional |Description |
| --- | --- | --- | ---|
| exch | ```string``` | True | Exchange NSE / NFO / BSE / CDS |
| tsym | ```string``` | True | Trading Symbol is the readable Unique id of contract/scrip |
| token | ```string``` | True | Unique Code of contract/scrip |
| pp | ```string``` | True | price precision, in case of cds its 4 ie 100.1234 |
| ti | ```string``` | True | tick size minimum increments of paise for price |
| ls | ```string``` | True | Lot Size |

#### <a name="md-start_websocket"></a> start_websocket()
starts the websocket
Expand Down Expand Up @@ -101,34 +126,32 @@ send a list of instruments to stop watch


```python
from NorenRestApiPy.NorenApi import NorenApi

socket_opened = False

####callbacks of client application

def event_handler_quote_update(message):
print(message)

def open_callback():
global socket_opened
socket_opened = True
print('app is connected')
api.subscribe('CDS|100')
api.subscribe(['NSE|22', 'BSE|522032'])
from NorenRestApiPy.NorenApi import PriceType, BuyorSell, ProductType
from api_helper import NorenApiPy, get_time
import logging
import hashlib

######################################################################
host = 'http://your.webapi.api'
endpoint = 'wss://websocket.endpoint.api'
logging.basicConfig(level=logging.DEBUG)

api = StarApi(host, endpoint)
#start of our program
api = NorenApiPy()

ret = api.login(userid = <user>, password = <pwd>, twoFA=<factor2>, vendor_code=<vc>, api_secret=<apikey>, imei=<imei>)
#credentials
user = '< user id>'
u_pwd = '< password >'
factor2 = 'second factor'
vc = 'vendor code'
api_secret = 'secret key'
imei = 'uniq identifier'

u_app_key='{0}|{1}'.format(user,api_secret)

#Convert to SHA 256 for password and app key
pwd = hashlib.sha256(u_pwd.encode('utf-8')).hexdigest()
app_key=hashlib.sha256(u_app_key.encode('utf-8')).hexdigest()

if ret == None:
api.start_websocket(subscribe_callback=event_handler_quote_update, socket_open_callback=open_callback)
ret = api.login(userid=user, password=pwd, twoFA=factor2, vendor_code=vc, api_secret=app_key, imei=imei)
print(ret)
```

****
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name="NorenRestApiPy",
version="0.0.1",
version="0.0.2",
author="KumarAnand",
author_email="kumar.anand@kambala.co.in",
description="A package for NorenOMS",
Expand Down

0 comments on commit 131b56d

Please sign in to comment.