<jupyter_start><jupyter_text><jupyter_code>!pip install yfinance
!pip install --upgrade mplfinance

#Instalação da Biblioteca TA-lib
url = 'https://launchpad.net/~mario-mariomedina/+archive/ubuntu/talib/+files'
!wget $url/libta-lib0_0.4.0-oneiric1_amd64.deb -qO libta.deb
!wget $url/ta-lib0-dev_0.4.0-oneiric1_amd64.deb -qO ta.deb
!dpkg -i libta.deb ta.deb
!pip install ta-lib
import numpy as np
import pandas as pd
import scipy as sp
import seaborn as sn
import matplotlib.pyplot as plt

import yfinance as yf

import talib as ta

from sklearn import metrics
from sklearn.naive_bayes import GaussianNB, MultinomialNB, ComplementNB, BernoulliNB, CategoricalNB
from sklearn.linear_model import LinearRegression
stock_ticker = 'PETR4.SA'
start_date = '2015-11-30'
end_date = '2021-5-27'

yf_petr4 = yf.Ticker(stock_ticker)
df_petr4 = yf_petr4.history(start=start_date, end=end_date)

stock_ticker = '^BVSP'
yf_ibov = yf.Ticker(stock_ticker)
df_ibov = yf_ibov.history(start=start_date, end=end_date)
df_ibov
df_petr4
df_petr4_beta = pd.concat([df_ibov["Close"].pct_change(),df_petr4["Close"].pct_change()],axis=1,ignore_index=True)

df_petr4_beta.columns=["Close_IBOV","Close_PETR4"]

df_petr4_beta["Beta"] = df_petr4_beta["Close_PETR4"].rolling(30).cov(df_petr4_beta["Close_IBOV"].rolling(30))/df_petr4_beta["Close_IBOV"].rolling(30).var()

df_petr4_beta["Beta_expected_PETR4"] = df_petr4_beta["Close_IBOV"]*df_petr4_beta["Beta"]

df_petr4_beta["PETR4_Excess_Variat"] = (df_petr4_beta["Close_PETR4"] - df_petr4_beta["Beta_expected_PETR4"])/df_petr4_beta["Beta_expected_PETR4"]

var_tolerance = df_petr4_beta["Close_IBOV"].var()/df_petr4_beta["Close_IBOV"].mean()

df_petr4_beta.fillna(0,inplace=True)
df_petr4_beta.loc[df_petr4_beta["PETR4_Excess_Variat"]  > var_tolerance, "PETR4_Excess_Variat_Disc"] = 3
df_petr4_beta.loc[df_petr4_beta["PETR4_Excess_Variat"] <= var_tolerance , "PETR4_Excess_Variat_Disc"] = 2
df_petr4_beta.loc[df_petr4_beta["PETR4_Excess_Variat"] >= -var_tolerance , "PETR4_Excess_Variat_Disc"] = 2
df_petr4_beta.loc[df_petr4_beta["PETR4_Excess_Variat"]  < -var_tolerance, "PETR4_Excess_Variat_Disc"] = 1
df_petr4_beta<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12973222530009235, 'fraction_numerical': 0.04016620498614958, 'mean_word_length': 8.50438596491228, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '27878', 'license': 'MIT', 'n_tokens_mistral': 1026, 'n_tokens_neox': 966, 'n_words': 122, 'path': 'I2A2_PETR4_Beta.ipynb', 'repo_name': 'nakanoelio/I2A2-challenge-petr4-trad-sys', 'size': '2166'}