Skip to main content

Stock market data analysis

Project description

Finmetry

This is the project on stock market data analysis using modern data anlysis tools, AI and ML.

In depth tests on various stock market tools are conducted.

The current version is

0.1.7

Importing and initiating

%load_ext autoreload

%autoreload 2
import finmetry as fm

import pandas as pd
pd.set_option('display.max_rows', 5)
pd.set_option('display.max_columns', 20)
import numpy as np
import datetime as dtm

The API has Stock class which helps in managing the data related to any security. Clients are classes of various data providing external libraries such as py5paisa, yfinance etc. finmetry API has the class based of the python API of such external libraries for ease of use with Stock class. This version of finmetry is mainly developed around client py5paisa, as it provieds enough data for in-depth market analysis.

Initializing Stock

Stock class API provides handy tools for managing various data provided by clients. The Stock instance is provided to various clients which will then download the data for those stocks. There are multiple clients which provide variety of data on security. Further information on these can be found on specific module or through help of specific methods. The information about intializing Client5paisa can be found here.

The Stock class consists of various methods to get historical data and managing the local storage of data. If the store_local is False then no local storage is done. It is advisable to keep a folder for market data which can be provided to Stock class for proper handling. Also, if the historical data is already downloaded then this helps in directly accesing offline data instead of downloading it again.

from finmetry import Stock

market_foldpath = r"D:\Finmetry\local_data"

s1 = Stock(
    symbol="RELIANCE",
    exchange="N",
    exchange_type="C",
    store_local=True,
    local_data_foldpath=market_foldpath,
)

As store_local = True, Folder with name RELIANCE is created. Now all the historical and other types of data will be stored in this folder. If there is already some historical data downloaded then API would directly access that data.

Intitalizing Client5paisa

Client5paisa is the class derived from py5paisa.FivePaisaClient. This is done to ease the data handling process.

First, we will initialize Client5paisa which will be used for data downloading for various stocks. For this the user needs 5paisa account and the API keys. User can refer to py5paisa for further details on getting API credentials. User need, API credentials (cred), email-id (email), password (password) of 5paisa account and date of birth (dob). Here, the credentials are already loaded on variables and not shown for security reasons.

c_5p = fm.client_5paisa.Client5paisa(email=email, password=password, dob=dob, cred=cred)
 19:32:23 | Logged in!!

ScripMaster

py5paisa provides the .csv file containing symbols and other information of majority of the Indian securities. The file is around 40Mb (while writing this and may vary in future) which may take about few seconds to download depending upon the internet speed. It is advisable to save it offline and use that .csv again for faster intialization. The local file should be updated often for any changes in .csv file, if any.

Downloading the file

scrip_master = fm.client_5paisa.ScripMaster()

Save the file locally

scrip_master_filepath = r"D:\Finmetry\local_data\scrip_master.csv"
scrip_master.save(scrip_master_filepath)

Loading from local file

scrip_master_filepath = r"D:\Finmetry\local_data\scrip_master.csv"
scrip_master = fm.client_5paisa.ScripMaster(filepath=scrip_master_filepath)
scrip_master.data
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Exch ExchType Scripcode Name Series Expiry ... Multiplier Underlyer Root TickSize CO BO Allowed Symbol
0 B C 199117 SETFN50INAV EQ 1980-01-01 00:00:00 ... 1 NaN SETFN50INAV 0.01 N SETFN50INAV
1 B C 199118 MOHEALTINAV EQ 1980-01-01 00:00:00 ... 1 NaN MOHEALTINAV 0.01 N MOHEALTINAV
... ... ... ... ... ... ... ... ... ... ... ... ... ...
202732 M D 256029 NATURALGAS 23 JUN 2023 CE 130.00 ... 2023-06-23 23:59:59 ... 1 23 Jun 2023 NATURALGAS 0.05 Y NATURALGAS 23 JUN 2023 CE 130.00 ...
202733 M D 256030 NATURALGAS 23 JUN 2023 PE 130.00 ... 2023-06-23 23:59:59 ... 1 23 Jun 2023 NATURALGAS 0.05 Y NATURALGAS 23 JUN 2023 PE 130.00 ...

202734 rows × 20 columns

User can repeat the process of downloading and saving to update the local file. This scrip_master will be used for generating various inputs for data fatching methods. The Exch, ExchType, Scripcode are some of the parameters required to get the market data of various securities. Hence this scrip_master should be provided to client_5paisa instance (i.e, c_5p).

Provide scrip_master to Client5paisa

c_5p.scrip_master = scrip_master

User can also provide the scrip_master instance during initialization of Client5paisa class.

c_5p = fm.client_5paisa.Client5paisa(
    email=email, password=password, dob=dob, cred=cred, scrip_master=scrip_master
)

Historical data

Historical data for list of stocks can be downloaded using c_5p.download_historical_data. If Stock.store_local is True then the data will be stored locally with separate file for each months data.

syms = ["RELIANCE", "ITC", "TCS"]
sl1 = []
for sym in syms:
    s1 = Stock(
        symbol=sym.upper(),
        exchange="N",
        exchange_type="C",
        store_local=True,
        local_data_foldpath=market_foldpath,
    )
    sl1.append(s1)
c_5p.download_historical_data(
    stocklist=sl1, interval="1m", start="2019-04-1", end="2019-04-30"
)

Accessing local data

Stock class has the method to access the data stored locally.

s1 = sl1[0]
data = s1.load_historical_data(interval="1m", start="2019-04-1", end="2019-04-30")
data
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Open High Low Close Volume
Datetime
2019-04-01 09:15:00 1371.00 1373.15 1369.50 1370.10 73889
2019-04-01 09:16:00 1370.30 1370.75 1368.90 1369.00 45357
... ... ... ... ... ...
2019-04-30 15:29:00 1392.15 1393.00 1391.00 1393.00 33141
2019-04-30 15:30:00 1392.45 1392.45 1392.45 1392.45 25

7137 rows × 5 columns

Live Market Data

Various live market data is provided by py5paisa, like Market Depth and Market Feed.

Market Depth

d1 = c_5p.get_market_depth(sl1)
d1
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
AverageTradePrice BuyQuantity Close Exchange ExchangeType High LastQuantity LastTradeTime LastTradedPrice Low ... Open OpenInterest ScripCode SellQuantity TotalBuyQuantity TotalSellQuantity UpperCircuitLimit Volume Datetime Symbol
0 2342.62 0 2352 N C 2359 1 /Date(1681986517000)/ 2346.05 2332.1 ... 2354.1 0 2885 0 0 783 2587.20 3233882 2023-04-20 19:37:00.123152 RELIANCE
1 400.07 0 398.75 N C 402.65 5 /Date(1681986598000)/ 400.30 397.7 ... 400 0 1660 0 279 0 438.60 6667781 2023-04-20 19:37:00.123152 ITC
2 3094.25 0 3089.6 N C 3113 11 /Date(1681986560000)/ 3104.80 3078 ... 3090 0 11536 0 0 14 3398.55 2419999 2023-04-20 19:37:00.123152 TCS

3 rows × 23 columns

Market Feed

c_5p.get_market_feed(sl1)
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Chg ChgPcnt Exch ExchType High LastRate Low PClose Symbol TickDt Time Token TotalQty Datetime
0 -5.95 -0.252976 N C 2359.00 2346.05 2332.1 2352.00 RELIANCE /Date(1681986517000+0530)/ 35917 2885 3233882 2023-04-20 19:37:03.343834
1 1.55 0.388715 N C 402.65 400.30 397.7 398.75 ITC /Date(1681986598000+0530)/ 35998 1660 6667781 2023-04-20 19:37:03.343834
2 15.20 0.491973 N C 3113.00 3104.80 3078.0 3089.60 TCS /Date(1681986560000+0530)/ 35960 11536 2419999 2023-04-20 19:37:03.343834

Market Data

For feed_type='md', user gets the data for Buyers and seller for 5 depth. Here, Last traded price is not there.

d1 = c_5p.get_market_data(sl1,feed_type='md')
d1
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Exch ExchType Token TBidQ TOffQ Details TimeStamp Time
0 N C 2885 0 783 [{'Quantity': 0, 'Price': 0, 'NumberOfOrders':... 0 /Date(1681986590946)/
1 N C 1660 279 0 [{'Quantity': 279, 'Price': 400.3, 'NumberOfOr... 0 /Date(1681986598666)/
2 N C 11536 0 14 [{'Quantity': 0, 'Price': 0, 'NumberOfOrders':... 0 /Date(1681986591595)/

for feed_type='mf', user gets the data for nearest bid and offer and the last traded price.

d1 = c_5p.get_market_data(sl1,feed_type='mf')
d1
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Exch ExchType Token LastRate LastQty TotalQty High Low OpenRate PClose AvgRate Time BidQty BidRate OffQty OffRate TBidQ TOffQ TickDt ChgPcnt
0 N C 2885 2346.05 1 3233882 2359.00 2332.1 2354.1 2352.00 2342.62 35918 0 0.0 783 2346.05 0 783 /Date(1681986518000)/ 0
1 N C 1660 400.30 5 6667781 402.65 397.7 400.0 398.75 400.07 35999 279 400.3 0 0.00 279 0 /Date(1681986599000)/ 0
2 N C 11536 3104.80 11 2419999 3113.00 3078.0 3090.0 3089.60 3094.25 35961 0 0.0 14 3104.80 0 14 /Date(1681986561000)/ 0

Getting Option Chain data

For getting option chain data, user needs the expiry date for the option chain. Available expiry dates can be weekly or monthly depending upon the type of security. For indices, the options can have weekly expiry and for all of the stocks the expiry is monthly.

dates1 = c_5p.get_monthly_option_expiry()
dates1 = c_5p.get_weekly_option_expiry()
dates1
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
Exch ExchType ExpiryDate Timestamp Date
0 N D /Date(1681981200000+0530)/ 1681981200000 2023-04-20
1 N D /Date(1682586000000+0530)/ 1682586000000 2023-04-27
... ... ... ... ... ...
5 N D /Date(1685005200000+0530)/ 1685005200000 2023-05-25
6 N D /Date(1688029200000+0530)/ 1688029200000 2023-06-29

7 rows × 5 columns

c_5p.get_option_chain(sl1[0],'2023-04-27')
<style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th {
    vertical-align: top;
}

.dataframe thead th {
    text-align: right;
}
</style>
CPType ChangeInOI EXCH ExchType LastRate Name OpenInterest Prev_OI PreviousClose ScripCode StrikeRate Volume
0 CE 0 N D 0.0 RELIANCE 27 APR 2023 CE 1160.00 0 0 0.0 55506 1160 0
1 PE 0 N D 0.0 RELIANCE 27 APR 2023 PE 1160.00 0 0 0.0 55507 1160 0
... ... ... ... ... ... ... ... ... ... ... ... ...
232 CE 0 N D 0.0 RELIANCE 27 APR 2023 CE 3480.00 0 0 0.0 41363 3480 0
233 PE 0 N D 0.0 RELIANCE 27 APR 2023 PE 3480.00 0 0 0.0 41366 3480 0

234 rows × 12 columns



Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finmetry-0.1.7.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

finmetry-0.1.7-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file finmetry-0.1.7.tar.gz.

File metadata

  • Download URL: finmetry-0.1.7.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for finmetry-0.1.7.tar.gz
Algorithm Hash digest
SHA256 744977db4ec12697e363d21b5e9cd06a7065e2090c743b7a2f59ee6c69195cf3
MD5 8b0da83216e8ef5ad52acdaa36a5d69e
BLAKE2b-256 5c4b387641e1a2be54f5f91db01ec13ae60620b8cfa128aec71b67bab8666f50

See more details on using hashes here.

File details

Details for the file finmetry-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: finmetry-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for finmetry-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 431b15ed7e06e9752287012075f439f94d0a67c9e6af8b7911d70d3e320d54b2
MD5 4c15fb4e74ee1bbec4c855d3a07de749
BLAKE2b-256 eee7f9bceee40ad3e99b84d3be198fca6fc67758c2aa2cee26e3b6020a13aa57

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page