Stock Data Analysis Using Python
India has one of the fastest-growing stock markets in the world. The National Stock Exchange (NSE) and Bombay Stock Exchange (BSE) are the two primary stock exchanges in India. Stock data analysis can provide valuable insights for investors to make informed decisions. In this article, we will explore the process of Indian stock data analysis using Python.
Data Acquisition
The first step in the process of Indian stock data analysis is to acquire the data. There are various sources of stock data such as Yahoo Finance, Alpha Vantage, and Quandl. In this article, we will use the nsepy library to acquire the data from the National Stock Exchange. The following code can be used to acquire the data for a particular stock:
from nsepy import get_history
from datetime import date
start_date = date(2015,1,1)
end_date = date(2021,12,31)
ticker = 'SBIN'
df = get_history(symbol=ticker, start=start_date, end=end_date, index=False)
The above code will import the nsepy library and use it to acquire the data for State Bank of India (SBIN) stock from the National Stock Exchange for the time period of January 1, 2015, to December 31, 2021. The data is stored in a pandas dataframe named df.
Data Exploration
Once the data has been acquired, the next step is to explore the data. Data exploration helps in understanding the data and identifying any patterns or trends. The following code can be used to explore the data:
import matplotlib.pyplot as plt
# Plot the closing price of the stock
plt.plot(df['Close'])
plt.title('Closing Price')
plt.xlabel('Date')
plt.ylabel('Price (Rs.)')
plt.show()
# Plot the volume of the stock
plt.plot(df['Volume'])
plt.title('Volume')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.show()
# Calculate and plot the daily returns of the stock
daily_returns = (df['Close'] / df['Close'].shift(1)) - 1
plt.plot(daily_returns)
plt.title('Daily Returns')
plt.xlabel('Date')
plt.ylabel('Returns')
plt.show()
The above code will plot the closing price of the stock, the volume of the stock, and the daily returns of the stock. The daily returns are calculated as the percentage change in the closing price from the previous day. The plots help in identifying any trends, patterns, or outliers in the data.
Data Preprocessing
Data preprocessing involves cleaning, transforming, and normalizing the data to make it suitable for analysis. In the following code, we will perform some data preprocessing steps:
# Fill in missing values with the mean of the column
df.fillna(df.mean(), inplace=True)
# Scale the data using the MinMaxScaler function
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
df[['Open', 'High', 'Low', 'Close']] = scaler.fit_transform(df[['Open', 'High', 'Low', 'Close']])
# Calculate the moving average of the closing price
df['MA'] = df['Close'].rolling(window=50).mean()
# Remove missing values
df.dropna(inplace=True)
The above code fills in the missing values in each column with the mean value of that column. Then, the data is scaled using the MinMaxScaler() function. The rolling() function is used to calculate the moving average of the closing price. Finally, any rows with missing values are removed.
Data Analysis
After the data has been preprocessed, we can begin the data analysis. Data analysis involves applying various statistical techniques to the data. In this section, we will explore some commonly used techniques in Indian stock data analysis.
Moving Average
A moving average is a popular technique used in stock data analysis. It helps in smoothing out the data and identifying any trends. The following code can be used to plot the moving average of the closing price:
plt.plot(df['Close'], label='Closing Price')
plt.plot(df['MA'], label='Moving Average')
plt.title('Moving Average')
plt.xlabel('Date')
plt.ylabel('Price (Rs.)')
plt.legend()
plt.show()
The above code will plot the closing price of the stock and the moving average of the closing price. The moving average is calculated using a rolling window of 50 days.
Bollinger Bands
Bollinger Bands are another popular technique used in stock data analysis. They are used to identify the volatility and trend of the stock. The following code can be used to plot the Bollinger Bands:
# Calculate the rolling mean and standard deviation
df['Rolling Mean'] = df['Close'].rolling(window=20).mean()
df['Rolling Std'] = df['Close'].rolling(window=20).std()
# Calculate the upper and lower Bollinger Bands
df['Upper Band'] = df['Rolling Mean'] + (2 * df['Rolling Std'])
df['Lower Band'] = df['Rolling Mean'] - (2 * df['Rolling Std'])
# Plot the Bollinger Bands
plt.plot(df['Close'], label='Closing Price')
plt.plot(df['Rolling Mean'], label='Rolling Mean')
plt.plot(df['Upper Band'], label='Upper Band')
plt.plot(df['Lower Band'], label='Lower Band')
plt.title('Bollinger Bands')
plt.xlabel('Date')
plt.ylabel('Price (Rs.)')
plt.legend()
plt.show()
The above code will plot the closing price of the stock, the rolling mean, upper Bollinger Band, and lower Bollinger Band. The rolling mean and standard deviation are calculated using a rolling window of 20 days.
Correlation Analysis
Correlation analysis is used to identify the relationship between two variables. In stock data analysis, correlation analysis is used to identify the relationship between different stocks. The following code can be used to perform correlation analysis:
# Acquire data for two different stocks
df1 = get_history(symbol='SBIN', start=start_date, end=end_date, index=False)
df2 = get_history(symbol='INFY', start=start_date, end=end_date, index=False)
# Merge the two dataframes
df = pd.merge(df1['Close'], df2['Close'], left_index=True, right_index=True)
df.columns = ['SBIN', 'INFY']
# Calculate the correlation matrix
corr_matrix = df.corr()
# Plot the correlation matrix
import seaborn as sns
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
The above code will acquire the data for State Bank of India (SBIN) and Infosys (INFY) and merge the two dataframes. The correlation matrix is then calculated and plotted using a heatmap.
Conclusion
In conclusion, Indian stock data analysis using Python involves acquiring the data, exploring the data, preprocessing the data, and analyzing the data using various statistical techniques. By performing stock data analysis, investors can make informed decisions and maximize their returns.
While the above techniques are just a few examples, there are many other statistical techniques that can be applied to Indian stock data using Python. It is important to note that stock data analysis should be done with caution and should not be relied upon as the sole basis for making investment decisions. It is always recommended to seek advice from a financial advisor before making any investment decisions.
In addition, it is important to keep in mind that the accuracy of the analysis depends on the accuracy of the data. Therefore, it is important to ensure that the data is accurate and up-to-date. It is also recommended to perform backtesting on historical data to ensure the accuracy of the analysis.
Python is a powerful tool for analyzing Indian stock data. It provides a wide range of libraries and functions that make data analysis easier and more efficient. By utilizing the techniques discussed in this article, investors can gain valuable insights into the stock market and make informed decisions.
Overall, Indian stock data analysis using Python is a complex and nuanced process, but with the right tools and techniques, it can be a powerful way to analyze and understand the stock market. With the right analysis and a little bit of luck, investors can maximize their returns and make successful investments.
0 comments:
Post a Comment
Please do not enter any spam link in the comment box.