Moving Average charts for ONGC Stock

Published: 23 April 2024| Version 1 | DOI: 10.17632/mh7h4thwp9.1
Contributor:
Sunil Maria Benedict

Description

The provided code performs analysis and visualization of daily returns and moving averages for the historical stock price data of Oil and Natural Gas Corporation (ONGC) from a CSV file. The analysis is conducted using the pandas library for data manipulation, numpy for numerical computations, and matplotlib.pyplot for generating plots. Loading Data: The code imports the pandas library as pd, numpy as np, and matplotlib.pyplot as plt. It specifies the file path of the CSV file containing ONGC stock price data. The data is loaded into a DataFrame named df using the read_csv() function from pandas. Extracting Required Columns: The code extracts the required columns from the DataFrame, including 'Open', 'High', 'Low', 'Close', and 'Adj Close' prices, and assigns them to separate variables. Calculating Daily Returns: Daily returns are calculated for each price metric ('Open', 'High', 'Low', 'Close', 'Adj Close') using the pct_change() function, which computes the percentage change between consecutive values. Calculating Moving Averages: Moving averages are computed for each daily return series using the rolling() function with a specified window size. The window size for the moving averages is set to 20 days. Plotting Daily Returns and Moving Averages: Subplots are created for each price metric ('Open', 'High', 'Low', 'Close', 'Adj Close') along with their corresponding moving averages. Each subplot displays the daily returns and the moving average curve. Titles and legends are added to each subplot for clarity. Displaying Plots: The plt.tight_layout() function adjusts the spacing between subplots to prevent overlapping. Finally, the plt.show() function is called to display the plots. Overall, the code provides a comprehensive analysis of daily returns and moving averages for ONGC stock prices, allowing for insights into the trend and volatility of the stock over time.

Files

Steps to reproduce

import pandas as pd import numpy as np import matplotlib.pyplot as plt # Load the data file_path = '/Users/sunilbenedict/Downloads/ONGC.NS.csv' df = pd.read_csv(file_path) # Extract the required columns open_prices = df['Open'] high_prices = df['High'] low_prices = df['Low'] close_prices = df['Close'] adj_close_prices = df['Adj Close'] # Calculate daily returns open_returns = open_prices.pct_change() high_returns = high_prices.pct_change() low_returns = low_prices.pct_change() close_returns = close_prices.pct_change() adj_close_returns = adj_close_prices.pct_change() # Calculate moving averages window = 20 # Define the window size for moving averages open_ma = open_returns.rolling(window=window).mean() high_ma = high_returns.rolling(window=window).mean() low_ma = low_returns.rolling(window=window).mean() close_ma = close_returns.rolling(window=window).mean() adj_close_ma = adj_close_returns.rolling(window=window).mean() # Plot daily returns and moving averages plt.figure(figsize=(12, 8)) # Plot Open Prices plt.subplot(3, 2, 1) plt.plot(open_returns, label='Open Returns', color='blue') plt.plot(open_ma, label='Open Moving Average', color='red') plt.title('Open Prices and Moving Average') plt.legend() # Plot High Prices plt.subplot(3, 2, 2) plt.plot(high_returns, label='High Returns', color='green') plt.plot(high_ma, label='High Moving Average', color='orange') plt.title('High Prices and Moving Average') plt.legend() # Plot Low Prices plt.subplot(3, 2, 3) plt.plot(low_returns, label='Low Returns', color='purple') plt.plot(low_ma, label='Low Moving Average', color='yellow') plt.title('Low Prices and Moving Average') plt.legend() # Plot Close Prices plt.subplot(3, 2, 4) plt.plot(close_returns, label='Close Returns', color='brown') plt.plot(close_ma, label='Close Moving Average', color='blue') plt.title('Close Prices and Moving Average') plt.legend() # Plot Adjusted Close Prices plt.subplot(3, 2, 5) plt.plot(adj_close_returns, label='Adj Close Returns', color='black') plt.plot(adj_close_ma, label='Adj Close Moving Average', color='yellow') plt.title('Adjusted Close Prices and Moving Average') plt.legend() plt.tight_layout() plt.show()

Institutions

Independent

Categories

Statistics, Equity, Stockholder, Stock Exchange, Volatility

Licence