Python Numpy Mini Projects:Stock Price Analyzer
This project analyses the stock prices. It has following functionalities.
Code Implementation:
This
program starts by creating stock by using NumPy array.
- The basic statistics include Average price, maximum, minimum prices and standard deviations.
- Next, Daily price changes are calculated using np.diff(prices).
- Daily returns price is calculated and the data is rounded to 2 digit.
- Stock Volatility is calculated and displayed.
- 7 day moving window is calculated along with average value.
- Best and worst days are displayed according to the returns value.
- It shows how a simple 30,000 initial value gives you profit.
Code:
import numpy as np
# Sample stock closing prices
prices = np.array([80, 100, 108, 120, 208, 110, 125, 311, 117])
print("Stock Prices:")
print(prices)
#Basic statistics
print("The basic statistics of the data:")
print("Average Price:", np.mean(prices))
print("Maximum Price:", np.max(prices))
print("Minimum Price:", np.min(prices))
print("Standard Deviation:", np.std(prices))
#Daily Price changes
daily_change = np.diff(prices)
print("Daily Changes:")
print(daily_change)
#Daily Returns
returns = (prices[1:] - prices[:-1]) / prices[:-1] * 100
print("Daily Returns (%):")
print(np.round(returns, 2))
# Average Daily Return
avg_return = np.mean(returns)
print("Average Daily Return (%):", round(avg_return,2))
#Stock Volatility
volatility = np.std(returns)
print("Volatility (%):", round(volatility,2))
# 7-Day Moving Average
window = 7
moving_avg = np.convolve(
prices,
np.ones(window)/window,
mode='valid'
)
print("7-Day Moving Average:")
print(np.round(moving_avg,2))
#Best and Worst Day
best_day = np.argmax(returns)
worst_day = np.argmin(returns)
print("Best Return Day:", best_day + 1)
print("Worst Return Day:", worst_day + 1)
print("Best Return:", round(returns[best_day],2), "%")
print("Worst Return:", round(returns[worst_day],2),
"%")
#Growth of ₹30,000 Investment
initial_investment = 30000
shares = initial_investment / prices[0]
final_value = shares * prices[-1]
profit = final_value - initial_investment
print("Final Value: ₹", round(final_value,2))
print("Profit: ₹", round(profit,2))
Output:
Stock Prices:
[ 80 100 108 120 208 110 125 311 117]
The basic statistics of the data:
Average Price: 142.11111111111111
Maximum Price: 311
Minimum Price: 80
Standard Deviation: 68.39662831918032
Daily Changes:
[ 20 8
12 88 -98
15 186 -194]
Daily Returns (%):
[ 25. 8. 11.11
73.33 -47.12 13.64 148.8 -62.38]
Average Daily Return (%): 21.3
Volatility (%): 62.25
7-Day Moving Average:
[121.57 154.57 157. ]
Best Return Day: 7
Worst Return Day: 8
Best Return: 148.8 %
Worst Return: -62.38 %
Final Value: ₹ 43875.0
Profit: ₹ 13875.0
This is the way of implementing Stock Price Analyzer in
NumPy using Python. Hope, this code is useful. Keep Coding!!!
Comments
Post a Comment