Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented feature to get historic price trend of a stock over certain fixed time intervals. #24

Merged
merged 10 commits into from
Mar 7, 2021
11 changes: 10 additions & 1 deletion bsedata/bse.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

"""

from . import losers, gainers, quote, indices
from . import losers, gainers, quote, indices, periodTrend
import requests
import json

Expand Down Expand Up @@ -101,6 +101,15 @@ def verifyScripCode(self, code):
except KeyError:
return None

def getPeriodTrend(self,scripCode,timePeriod):
"""
Get historic price and volume trends of a stock over certain fixed period of time
:param scripCode: a stock code
:param timePeriod: the period of time. It can take the following values: '1M', '3M', '6M' and '12M'
:returns: List of dictionaries with date,price,vol data
"""
return periodTrend.getPeriodTrend(scripCode,timePeriod)

def __str__(self):
return 'Driver Class for Bombay Stock Exchange (BSE)'

Expand Down
51 changes: 51 additions & 0 deletions bsedata/periodTrend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""

MIT License

Copyright (c) 2019 Paul Antony

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

"""



import json
import requests

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36 Edg/83.0.478.45'
}

def getPeriodTrend(scripCode,timePeriod):

assert timePeriod in ['1M','3M','6M','12M'], "timePeriod should be one of the following options '1M', '3M', '6M' and '12M'"

baseurl = '''https://api.bseindia.com/BseIndiaAPI/api/StockReachGraph/w?'''
URL = baseurl + '''scripcode={}&flag={}&fromdate=&todate=&seriesid='''.format(scripCode,timePeriod)
res = requests.get(URL, headers=headers)

# extracting the data from the response
data = json.loads(res.content.decode('utf-8'))
data = json.loads(data['Data'])

# formating the data
res = [{'date' : x['dttm'], "value" : float(x['vale1']),"vol" : int(x['vole'])} for x in data]

return res
38 changes: 38 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,44 @@ Getting top losers
# 'scripCode': '534809',
# 'securityID': 'PCJEWELLER'}]


Getting Period Trend data
-------------------

Get historic price trend of a stock over certain fixed period of time. This data can be used to visualize the trends in the stock price

.. note::

The avalable period options are '1D','5D','1M','3M','6M','12M'.

.. code-block:: Python
his = b.getPeriodTrend('534976','6M')
pprint(q)
# Output:
# [{'date': 'Tue Jan 12 2021 00:00:00', 'value': '2453.10', 'vol': '791'},
# {'date': 'Wed Jan 13 2021 00:00:00', 'value': '2422.15', 'vol': '771'},
# {'date': 'Thu Jan 14 2021 00:00:00', 'value': '2417.45', 'vol': '423'},
# {'date': 'Fri Jan 15 2021 00:00:00', 'value': '2404.45', 'vol': '228'},
# {'date': 'Mon Jan 18 2021 00:00:00', 'value': '2415.25', 'vol': '241'},
# {'date': 'Tue Jan 19 2021 00:00:00', 'value': '2437.10', 'vol': '589'},
# {'date': 'Wed Jan 20 2021 00:00:00', 'value': '2410.20', 'vol': '809'},
# {'date': 'Thu Jan 21 2021 00:00:00', 'value': '2473.40', 'vol': '4657'},
# {'date': 'Fri Jan 22 2021 00:00:00', 'value': '2495.10', 'vol': '1671'},
# {'date': 'Mon Jan 25 2021 00:00:00', 'value': '2496.25', 'vol': '636'},
# {'date': 'Wed Jan 27 2021 00:00:00', 'value': '2399.90', 'vol': '1063'},
# {'date': 'Thu Jan 28 2021 00:00:00', 'value': '2417.30', 'vol': '3635'},
# {'date': 'Fri Jan 29 2021 00:00:00', 'value': '2453.15', 'vol': '129'},
# {'date': 'Mon Feb 01 2021 00:00:00', 'value': '2457.35', 'vol': '785'},
# {'date': 'Tue Feb 02 2021 00:00:00', 'value': '2451.95', 'vol': '176'},
# {'date': 'Wed Feb 03 2021 00:00:00', 'value': '2458.30', 'vol': '1689'},
# {'date': 'Thu Feb 04 2021 00:00:00', 'value': '2493.30', 'vol': '1433'},
# {'date': 'Fri Feb 05 2021 00:00:00', 'value': '2478.40', 'vol': '1066'},
# {'date': 'Mon Feb 08 2021 00:00:00', 'value': '2472.95', 'vol': '1037'},
# {'date': 'Tue Feb 09 2021 00:00:00', 'value': '2446.55', 'vol': '1013'},
# {'date': 'Wed Feb 10 2021 00:00:00', 'value': '2485.20', 'vol': '313'},
# {'date': 'Thu Feb 11 2021 00:00:00', 'value': '2982.20', 'vol': '16605'},
# {'date': 'Fri Feb 12 2021 00:00:00', 'value': '2881.35', 'vol': '8452'}]

Getting indices
---------------

Expand Down
19 changes: 18 additions & 1 deletion test_bsedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,21 @@ def test_verifyCode_valid():
assert b.verifyScripCode('534976') == 'V-mart Retail Ltd.'

def test_verifyCode_invalid():
assert b.verifyScripCode('534980') == None
assert b.verifyScripCode('534980') == None

@pytest.mark.parametrize("timePeriod", ['1M', '3M', '6M', '12M'])
def test_getPeriodTrend(timePeriod):
timePeriodResults = {
'1M': [20, 30],
'3M': [60, 90],
'6M': [120, 180],
'12M': [240, 360]
}
assert timePeriodResults[timePeriod][0] <= len(b.getPeriodTrend(
'534976', timePeriod)) <= timePeriodResults[timePeriod][1]

def test_getPeriodTrend_invalid_period():
with pytest.raises(AssertionError) as err_info:
b.getPeriodTrend('534976', '2M')

assert err_info.value.args[0] == "timePeriod should be one of the following options '1M', '3M', '6M' and '12M'"