|
|
|
"""Master Card&VisaStockData.159 |
|
|
|
Automatically generated by Colab. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/127-oS8O1T914B2Fx1z0r0JAfHc3RJ8NB |
|
""" |
|
|
|
import pandas as pd |
|
|
|
data = pd.read_csv('MVR.csv') |
|
|
|
print(data.head()) |
|
|
|
print(data.isnull().sum()) |
|
|
|
data['Date'] = pd.to_datetime(data['Date']) |
|
|
|
data.set_index('Date', inplace=True) |
|
|
|
print(data.dtypes) |
|
|
|
print(data.info()) |
|
|
|
print(data.describe()) |
|
|
|
import matplotlib.pyplot as plt |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data.index, data['Close_M'], label='MasterCard Close') |
|
plt.plot(data.index, data['Close_V'], label='Visa Close') |
|
plt.title('Stock Prices of MasterCard and Visa') |
|
plt.xlabel('Date') |
|
plt.ylabel('Stock Price') |
|
plt.legend() |
|
plt.show() |
|
|
|
data['MA_Close_M'] = data['Close_M'].rolling(window=30).mean() |
|
data['MA_Close_V'] = data['Close_V'].rolling(window=30).mean() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data['Close_M'], label='MasterCard Close Price') |
|
plt.plot(data['MA_Close_M'], label='MasterCard 30-Day MA') |
|
plt.title('Moving Averages of Stock Prices') |
|
plt.xlabel('Date') |
|
plt.ylabel('Price') |
|
plt.legend() |
|
plt.show() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data['Volume_M'], label='MasterCard Volume') |
|
plt.plot(data['Volume_V'], label='Visa Volume') |
|
plt.title('Volume of Stocks Traded') |
|
plt.xlabel('Date') |
|
plt.ylabel('Volume') |
|
plt.legend() |
|
plt.show() |
|
|
|
data['SMA50_M'] = data['Close_M'].rolling(window=50).mean() |
|
data['SMA200_M'] = data['Close_M'].rolling(window=200).mean() |
|
|
|
data['SMA50_V'] = data['Close_V'].rolling(window=50).mean() |
|
data['SMA200_V'] = data['Close_V'].rolling(window=200).mean() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data.index, data['Close_M'], label='MasterCard Close') |
|
plt.plot(data.index, data['SMA50_M'], label='MasterCard SMA50') |
|
plt.plot(data.index, data['SMA200_M'], label='MasterCard SMA200') |
|
plt.title('MasterCard Stock Price and Moving Averages') |
|
plt.xlabel('Date') |
|
plt.ylabel('Stock Price') |
|
plt.legend() |
|
plt.show() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data.index, data['Close_V'], label='Visa Close') |
|
plt.plot(data.index, data['SMA50_V'], label='Visa SMA50') |
|
plt.plot(data.index, data['SMA200_V'], label='Visa SMA200') |
|
plt.title('Visa Stock Price and Moving Averages') |
|
plt.xlabel('Date') |
|
plt.ylabel('Stock Price') |
|
plt.legend() |
|
plt.show |
|
|
|
data['Volatility_M'] = data['Close_M'].rolling(window=30).std() |
|
data['Volatility_V'] = data['Close_V'].rolling(window=30).std() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data.index, data['Volatility_M'], label='MasterCard Volatility') |
|
plt.plot(data.index, data['Volatility_V'], label='Visa Volatility') |
|
plt.title('Stock Price Volatility of MasterCard and Visa') |
|
plt.xlabel('Date') |
|
plt.ylabel('Volatility') |
|
plt.legend() |
|
plt.show() |
|
|
|
data['Return_M'] = data['Close_M'].pct_change() |
|
data['Return_V'] = data['Close_V'].pct_change() |
|
|
|
data['Cumulative_Return_M'] = (1 + data['Return_M']).cumprod() |
|
data['Cumulative_Return_V'] = (1 + data['Return_V']).cumprod() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(data.index, data['Cumulative_Return_M'], label='MasterCard Cumulative Return') |
|
plt.plot(data.index, data['Cumulative_Return_V'], label='Visa Cumulative Return') |
|
plt.title('Cumulative Returns of MasterCard and Visa') |
|
plt.xlabel('Date') |
|
plt.ylabel('Cumulative Return') |
|
plt.legend() |
|
plt.show() |
|
|
|
correlation = data[['Close_M', 'Close_V']].corr() |
|
print(correlation) |
|
|
|
from statsmodels.tsa.seasonal import seasonal_decompose |
|
|
|
decomposition_M = seasonal_decompose(data['Close_M'], model='multiplicative', period=365) |
|
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(15, 12)) |
|
|
|
ax1.plot(decomposition_M.observed) |
|
ax1.set_title('Observed - MasterCard') |
|
ax2.plot(decomposition_M.trend) |
|
ax2.set_title('Tren - MasterCard') |
|
ax3.plot(decomposition_M.seasonal) |
|
ax3.set_title('Seasonal - MasterCard') |
|
ax4.plot(decomposition_M.resid) |
|
ax4.set_title('Residual - MasterCard') |
|
|
|
plt.tight_layout() |
|
plt.show |
|
|
|
decomposition_V = seasonal_decompose(data['Close_V'], model='multiplicative', period=365) |
|
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(15, 12)) |
|
|
|
ax1.plot(decomposition_V.observed) |
|
ax1.set_title('Observed - Visa') |
|
ax2.plot(decomposition_V.trend) |
|
ax2.set_title('Trend - Visa') |
|
ax3.plot(decomposition_V.seasonal) |
|
ax3.set_title('Seasonal - Visa') |
|
ax4.plot(decomposition_V.resid) |
|
ax4.set_title('Residual - Visa') |
|
|
|
plt.tight_layout() |
|
plt.show() |
|
|
|
from statsmodels.tsa.stattools import adfuller |
|
|
|
def adf_test(series): |
|
result = adfuller(series.dropna()) |
|
print('ADF Statistic:', result[0]) |
|
print('p-value:', result[1]) |
|
for key, value in result[4].items(): |
|
print('Critial Values:') |
|
print(f' {key}, {value}') |
|
|
|
print("ADF Test for MasterCard Close Price:") |
|
adf_test(data['Close_M']) |
|
|
|
print("\ADF Test for Visa Close Price:") |
|
adf_test(data['Close_V']) |
|
|
|
import numpy as np |
|
from sklearn.preprocessing import MinMaxScaler |
|
from keras.models import Sequential |
|
from keras.layers import LSTM, Dense, Input |
|
from sklearn.metrics import mean_squared_error |
|
|
|
scaler = MinMaxScaler(feature_range=(0, 1)) |
|
scaled_data_M = scaler.fit_transform(data[['Close_M']]) |
|
scaled_data_V = scaler.fit_transform(data[['Close_V']]) |
|
|
|
train_len_M = int(len(scaled_data_M) * 0.8) |
|
train_len_V = int(len(scaled_data_V) * 0.8) |
|
|
|
train_data_M = scaled_data_M[:train_len_M] |
|
test_data_M = scaled_data_M[train_len_M:] |
|
|
|
train_data_V = scaled_data_V[:train_len_V] |
|
test_data_V = scaled_data_V[train_len_V:] |
|
|
|
def create_sequences(data, seq_length): |
|
x = [] |
|
y = [] |
|
for i in range(seq_length, len(data)): |
|
x.append(data[i-seq_length:i, 0]) |
|
y.append(data[i, 0]) |
|
return np.array(x), np.array(y) |
|
|
|
seq_length = 60 |
|
x_train_M, y_train_M = create_sequences(train_data_M, seq_length) |
|
x_test_M, y_test_M = create_sequences(test_data_M, seq_length) |
|
|
|
x_train_V, y_train_V = create_sequences(train_data_V, seq_length) |
|
x_test_V, y_test_V = create_sequences(test_data_V, seq_length) |
|
|
|
x_train_M = np.reshape(x_train_M, (x_train_M.shape[0], x_train_M.shape[1], 1)) |
|
x_test_M = np.reshape(x_test_M, (x_test_M.shape[0], x_test_M.shape[1], 1)) |
|
|
|
x_train_V = np.reshape(x_train_V, (x_train_V.shape[0], x_train_V.shape[1], 1)) |
|
x_test_V = np.reshape(x_test_V, (x_test_V.shape[0], x_test_V.shape[1], 1)) |
|
|
|
model_M = Sequential() |
|
model_M.add(Input(shape=(x_train_M.shape[1], 1))) |
|
model_M.add(LSTM(units=50, return_sequences=True)) |
|
model_M.add(LSTM(units=50, return_sequences=False)) |
|
model_M.add(Dense(units=25)) |
|
model_M.add(Dense(units=1)) |
|
|
|
model_M.compile(optimizer='adam', loss='mean_squared_error') |
|
|
|
model_V = Sequential() |
|
model_V.add(Input(shape=(x_train_V.shape[1], 1))) |
|
model_V.add(LSTM(units=50, return_sequences=True)) |
|
model_V.add(LSTM(units=50, return_sequences=False)) |
|
model_V.add(Dense(units=25)) |
|
model_V.add(Dense(units=1)) |
|
|
|
model_V.compile(optimizer ='adam', loss='mean_squared_error') |
|
|
|
model_M.fit(x_train_M, y_train_M, batch_size=32, epochs=100) |
|
model_V.fit(x_train_V, y_train_V, batch_size=32, epochs=100) |
|
|
|
predictions_M = model_M.predict(x_test_M) |
|
predictions_M = scaler.inverse_transform(predictions_M) |
|
|
|
predictions_V = model_V.predict(x_test_V) |
|
predictions_V = scaler.inverse_transform(predictions_V) |
|
|
|
rmse_M = np.sqrt(mean_squared_error(y_test_M, predictions_M)) |
|
rmse_V = np.sqrt(mean_squared_error(y_test_V, predictions_V)) |
|
|
|
print(f'RMSE for MasterCard: {rmse_M}') |
|
print(f'RMSE for Visa: {rmse_V}') |
|
|
|
train_M = data[:train_len_M]['Close_M'] |
|
valid_M = data[train_len_M:train_len_M + len(predictions_M)]['Close_M'] |
|
valid_M = valid_M.to_frame() |
|
valid_M['Predictions'] = predictions_M |
|
|
|
train_V = data[:train_len_V]['Close_V'] |
|
valid_V = data[train_len_V:train_len_V + len(predictions_V)]['Close_V'] |
|
valid_V = valid_V.to_frame() |
|
valid_V['Predictions'] = predictions_V |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(train_M, label='Train - MasterCard') |
|
plt.plot(valid_M['Close_M'], label='Valid - MasterCard') |
|
plt.plot(valid_M['Predictions'], label='Predictions - MasterCard') |
|
plt.legend() |
|
plt.show() |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(train_V, label ='Train -Visa') |
|
plt.plot(valid_V['Close_V'], label='Valid -Visa') |
|
plt.plot(valid_V['Predictions'], label='Predictions - Visa') |
|
plt.legend() |
|
plt.show() |
|
|
|
from statsmodels.tsa.arima.model import ARIMA |
|
|
|
data = data.asfreq('B') |
|
|
|
train_size = int(len(data) * 0.8) |
|
train, test = data['Close_M'][:train_size], data['Close_M'][train_size:] |
|
|
|
model = ARIMA(train, order=(5, 1, 0)) |
|
model_fit = model.fit() |
|
print(model_fit.summary()) |
|
|
|
predictions = model_fit.forecast(steps=len(test)) |
|
predictions = pd.Series(predictions, index=test.index) |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(train, label='Training Data') |
|
plt.plot(test, label='Test Data') |
|
plt.plot(predictions, label='Predicted Data') |
|
plt.title('ARIMA Model Predictions for MasterCard') |
|
plt.xlabel('Date') |
|
plt.ylabel('Price') |
|
plt.legend() |
|
plt.show() |
|
|
|
data = data.asfreq('B') |
|
|
|
train_size = int(len(data) * 0.8) |
|
train_V, test_V = data['Close_V'][:train_size], data['Close_V'][train_size:] |
|
|
|
model_V = ARIMA(train_V, order=(5, 1, 0)) |
|
model_fit_V = model_V.fit() |
|
print(model_fit_V.summary()) |
|
|
|
predictions_V = model_fit_V.forecast(steps=len(test_V)) |
|
predictions_V = pd.Series(predictions_V, index=test_V.index) |
|
|
|
plt.figure(figsize=(14, 7)) |
|
plt.plot(train_V, label='Training Data') |
|
plt.plot(test_V, label='Test Data') |
|
plt.plot(predictions_V, label='Predicted Data'), |
|
plt.title('ARIMA Model Predictions for Visa') |
|
plt.xlabel('Date') |
|
plt.ylabel('Price') |
|
plt.legend() |
|
plt.show() |
|
|
|
import warnings |
|
warnings.filterwarnings('ignore') |
|
import plotly.graph_objects as go |
|
|
|
def predict_stock_price(data, column_name, forecast_periods): |
|
train_size = int(len(data) * 0.8) |
|
train, test = data[column_name][:train_size], data[column_name][train_size:] |
|
|
|
model = ARIMA(train, order=(5, 1, 0)) |
|
model_fit = model.fit() |
|
|
|
future_dates = pd.date_range(start=data.index[-1], periods=forecast_periods, freq='B') |
|
forecast = model_fit.forecast(steps=forecast_periods) |
|
forecast_series = pd.Series(forecast, index=future_dates) |
|
|
|
return forecast_series |
|
|
|
forecast_periods = 3 * 252 |
|
forecast_M = predict_stock_price(data, 'Close_M', forecast_periods) |
|
forecast_V = predict_stock_price(data, 'Close_V', forecast_periods) |
|
|
|
extended_data_M = pd.concat([data['Close_M'], forecast_M]) |
|
extended_data_V = pd.concat([data['Close_V'], forecast_V]) |
|
|
|
candlestick_data_M = pd.DataFrame({ |
|
'Date': extended_data_M.index, |
|
'Open': extended_data_M.shift(1).fillna(method='bfill'), |
|
'High': extended_data_M.rolling(2).max(), |
|
'Low': extended_data_M.rolling(2).min(), |
|
'Close': extended_data_M |
|
}).reset_index(drop=True) |
|
|
|
candlestick_data_V = pd.DataFrame({ |
|
'Date': extended_data_V.index, |
|
'Open': extended_data_V.shift(1).fillna(method='bfill'), |
|
'High': extended_data_V.rolling(2).max(), |
|
'Low': extended_data_V.rolling(2).min(), |
|
'Close': extended_data_V |
|
}).reset_index(drop=True) |
|
|
|
fig = go.Figure() |
|
|
|
fig.add_trace(go.Candlestick( |
|
x=candlestick_data_M['Date'], |
|
open=candlestick_data_M['Open'], |
|
high=candlestick_data_M['High'], |
|
low=candlestick_data_M['Low'], |
|
close=candlestick_data_M['Close'], |
|
name='MasterCard', |
|
increasing_line_color='blue', decreasing_line_color='red' |
|
)) |
|
|
|
fig.add_trace(go.Candlestick( |
|
x=candlestick_data_V['Date'], |
|
open=candlestick_data_V['Open'], |
|
high=candlestick_data_V['High'], |
|
low=candlestick_data_V['Low'], |
|
close=candlestick_data_V['Close'], |
|
name='Visa', |
|
increasing_line_color='green', decreasing_line_color='orange' |
|
)) |
|
|
|
fig.update_layout( |
|
title='MasterCard and Visa Stock Prices (Historical and Predicted)', |
|
xaxis_title='Date', |
|
yaxis_title='Price', |
|
xaxis_rangeslider_visible=False |
|
) |
|
|
|
fig.show() |