示例#1
0
文件: live.py 项目: prateek3211/nsepy
def get_quote(symbol, series='EQ', instrument=None, expiry=None, option_type=None, strike=None):
    """
    1. Underlying security (stock symbol or index name)
    2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
    3. expiry (ddMMMyyyy)
    4. type (CE/PE for options, - for futures
    5. strike (strike price upto two decimal places
    """

    if instrument:
        #expiry_str = "%02d%s%d"%(expiry.day, months[expiry.month][0:3].upper(), expiry.year)
        quote_derivative_url.session.headers.update({'Referer': eq_quote_referer.format(symbol)})
        res = quote_derivative_url(symbol, instrument, expiry, option_type, strike)
    else:
        quote_eq_url.session.headers.update({'Referer': eq_quote_referer.format(symbol)})
        res = quote_eq_url(symbol, series)

    d =  json.loads(res.text)['data'][0]
    res = {}
    for k in d.keys():
        v = d[k]
        try:
            v_ = None
            if v.find('.') > 0:
                v_ = float(v.strip().replace(',', ''))
            else:
                v_ = int(v.strip().replace(',', ''))
        except:
            v_ = v
        res[k] = v_
    return res
示例#2
0
def get_quote(symbol, series='EQ', instrument=None, expiry=None, option_type=None, strike=None):
    """
    1. Underlying security (stock symbol or index name)
    2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
    3. expiry (ddMMMyyyy)
    4. type (CE/PE for options, - for futures
    5. strike (strike price upto two decimal places
    """

    if instrument:
        expiry_str = "%02d%s%d"%(expiry.day, months[expiry.month][0:3].upper(), expiry.year)
        res = quote_derivative_url(symbol, instrument, expiry_str, option_type, strike)

    res = quote_eq_url(symbol, series)

    return json.loads(res.text)['data'][0]
示例#3
0
def get_quote(symbol,
              series='EQ',
              instrument=None,
              expiry=None,
              option_type=None,
              strike=None):
    """
    1. Underlying security (stock symbol or index name)
    2. instrument (FUTSTK, OPTSTK, FUTIDX, OPTIDX)
    3. expiry (ddMMMyyyy)
    4. type (CE/PE for options, - for futures
    5. strike (strike price upto two decimal places
    """
    global d
    if instrument:
        expiry_str = "%02d%s%d" % (
            expiry.day, months[expiry.month][0:3].upper(), expiry.year)
        quote_derivative_url.session.headers.update(
            {'Referer': eq_quote_referer.format(symbol)})
        strike_str = "{:.2f}".format(strike) if strike else ""
        res = quote_derivative_url(symbol, instrument, expiry_str, option_type,
                                   strike_str)
    else:
        quote_eq_url.session.headers.update(
            {'Referer': eq_quote_referer.format(symbol)})
        res = quote_eq_url(symbol, series)

    html_soup = BeautifulSoup(res.text, 'lxml')
    hresponseDiv = html_soup.find("div", {"id": "responseDiv"})
    try:
        d = json.loads(hresponseDiv.get_text())
    except:
        print("Error >> ", hresponseDiv.get_text())
    #d = json.loads(res.text)['data'][0]
    res = {}
    for k in d['data'][0].keys():
        v = d['data'][0][k]
        try:
            v_ = None
            if v.find('.') > 0:
                v_ = float(v.strip().replace(',', ''))
            else:
                v_ = int(v.strip().replace(',', ''))
        except:
            v_ = v
        res[k] = v_
    return res
示例#4
0
 def test_quote_eq_url(self):
     resp = quote_eq_url('SBIN', 'EQ')
     html_soup = BeautifulSoup(resp.text, 'lxml')
     hresponseDiv = html_soup.find("div", {"id": "responseDiv"})
     d = json.loads(hresponseDiv.get_text())
     self.assertEqual(d['data'][0]['symbol'], 'SBIN')
示例#5
0
 def test_quote_eq_url(self):
     resp = quote_eq_url('SBIN', 'EQ')
     d = json.loads(resp.content)
     self.assertEqual(d['data'][0]['symbol'], 'SBIN')
示例#6
0
 def test_quote_eq_url(self):
     resp = quote_eq_url('SBIN', 'EQ')
     d = json.loads(resp.content)
     self.assertEqual(d['data'][0]['symbol'], 'SBIN')