First simple case prediction (no training, just return prediction based on last day return)

pull/1/head
MasterRoby3 2023-10-27 03:30:49 +02:00
parent 3e1437e05e
commit 4ba6464262
8 changed files with 6096 additions and 6 deletions

10
appunti.md 100644
View File

@ -0,0 +1,10 @@
# Appunti sullo sviluppo del progetto
Il titolo di riferimento per la prima parte di studio è Microsoft. Perché:
* molto capitalizzato
* longevo
* è un titolo tech ma non subisce dinamiche troppo "strane" rispetto al normale andamento di mercato (es. Tesla)
Per prima cosa si testa la performance di un modello non trainato, semplice, che prova a predire il segno del ritorno del giorno successivo sulla base del ritorno del giorno precedente (+ segue + e - segue -).
Si riporta anche un piccolo grafico a barre per avere un'idea della distribuzione dei ritorni.

View File

@ -0,0 +1 @@
,master-roby3,master-roby3,27.10.2023 01:59,file:///home/master-roby3/.config/libreoffice/4;

View File

@ -1 +0,0 @@
,master-roby3,master-roby3,26.10.2023 16:32,file:///home/master-roby3/.config/libreoffice/4;

5993
data/MSFT_data.csv 100644

File diff suppressed because it is too large Load Diff

BIN
data/MSFT_data.pkl 100644

Binary file not shown.

View File

@ -1,5 +0,0 @@
import pandas as pd
stock_data = pd.read_csv("data/daily_MSFT.csv", index_col="timestamp")
print(stock_data)

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

View File

@ -0,0 +1,92 @@
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import yfinance as yf
from datetime import datetime
import os
#bodacious colors
colors=sns.color_palette("rocket", 8)
#Ram's colors, if desired
seshadri = ['#c3121e', '#0348a1', '#ffb01c', '#027608', '#0193b0', '#9c5300', '#949c01', '#7104b5']
# 0sangre, 1neptune, 2pumpkin, 3clover, 4denim, 5cocoa, 6cumin, 7berry
#stock_data = pd.read_csv("data/daily_MSFT.csv").iloc[::-1].reset_index(drop=True)
if os.path.isfile("data/MSFT_data.pkl"):
stock_data = pd.read_pickle("data/MSFT_data.pkl")
elif os.path.isfile("data/MSFT_data.csv"):
stock_data = pd.read_csv("data/MSFT_data.csv")
else:
start_date = datetime(2000, 1, 1)
end_date = datetime(2023, 10, 26)
stock_data = yf.download('MSFT', start=start_date, end=end_date)
stock_data.to_pickle("data/MSFT_data.pkl")
stock_data.to_csv("data/MSFT_data.csv")
daily_returns = stock_data["Close"] - stock_data["Open"]
win_lose = np.zeros(daily_returns.size - 1)
for index, return_ in enumerate(daily_returns[:-1]):
if (return_ > 0 and daily_returns[index + 1] > 0) or (return_ < 0 and daily_returns[index + 1] < 0):
win_lose[index] = 1
else:
win_lose[index] = 0
win_rate = np.count_nonzero(win_lose == 1) / win_lose.size
print(win_rate)
percent_returns = daily_returns / stock_data["Open"] * 100
print([ret for ret in percent_returns if ret > 10 or ret < -10])
fig = plt.figure(1, figsize=(15,10))
plt.hist(percent_returns, bins = 120, range=(-12,12), facecolor=seshadri[0], alpha=0.8, edgecolor="white", label="Percentage daily returns occurrences")
#plt.plot(stock_data.index, stock_data["Close"], linestyle="-", color=seshadri[0])
#plt.plot(stock_data.index, stock_data["Adj Close"], linestyle="-", color=seshadri[1])
#plt.plot(stock_data.index, stock_data["close"] - 20, linestyle="-", color=seshadri[2])
#plt.plot(stock_data.index, stock_data["close"] - 30, linestyle="-", color=seshadri[3])
#plt.plot(stock_data.index, stock_data["close"] - 40, linestyle="-", color=seshadri[4])
#plt.plot(stock_data.index, stock_data["close"] - 50, linestyle="-", color=seshadri[5])
#plt.plot(stock_data.index, stock_data["close"] - 60, linestyle="-", color=seshadri[6])
#plt.plot(stock_data.index, stock_data["close"] - 70, linestyle="-", color=seshadri[7])
#plt.show()
#plot params
plt.xlim([-12,12])
#plt.ylim([-0.5,16])
plt.minorticks_on()
plt.tick_params(labelsize=14)
plt.tick_params(labelbottom=True, labeltop=False, labelright=False, labelleft=True)
#xticks = np.arange(0, 1e4,10)
#yticks = np.arange(0,16.1,4)
plt.tick_params(direction='in',which='minor', length=5, bottom=True, top=True, left=True, right=True)
plt.tick_params(direction='in',which='major', length=10, bottom=True, top=True, left=True, right=True)
#plt.xticks(xticks)
#plt.yticks(yticks)
#plt.text(1,325, f'y={Decimal(coefs[3]):.4f}x$^3$+{Decimal(coefs[2]):.2f}x$^2$+{Decimal(coefs[1]):.2f}x+{Decimal(coefs[0]):.1f}',fontsize =13)
plt.xlabel(r'Percentage daily return', fontsize=14)
plt.ylabel(r'Occurrences',fontsize=14) # label the y axis
plt.legend(fontsize=14, loc="upper right", bbox_to_anchor=(0.99, 0.99)) # add the legend (will default to 'best' location)
plt.savefig("plots/MSFT_daily_occs.png", dpi=300)
plt.show()