diff --git a/opcoes.ipynb b/opcoes.ipynb new file mode 100644 index 00000000..13d683d3 --- /dev/null +++ b/opcoes.ipynb @@ -0,0 +1,463 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "mount_file_id": "1ME1xiIf2eXz115bQ4QqlTgeoriPk_3DE", + "authorship_tag": "ABX9TyNwFg4zPSwRFR6KsRv3MHsr" + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "import requests\n", + "import datetime as dt\n", + "import numpy as np\n", + "from bs4 import BeautifulSoup\n", + "import pandas as pd" + ], + "metadata": { + "id": "mqOove8b3gpT" + }, + "execution_count": 1, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "#Para dataframe\n", + "\n", + "#Insira os códidos das opções\n", + "Opções = ['BOVAT6','BOVAD117','BOVAD118','BOVAP4','BOVAP124', 'BOVAT7', 'BOVAD117','BOVAP3']\n", + "\n", + "data = []\n", + "for value in Opções:\n", + "\n", + " url = 'https://opcoes.net.br/' + value\n", + " # Get the content of the webpage\n", + " response = requests.get(url)\n", + "\n", + " # Check if content is received without error\n", + " if response.status_code == 200:\n", + " page_content = response.content\n", + "\n", + " soup = BeautifulSoup(page_content, 'html.parser')\n", + "\n", + " Strike = soup.title.text.replace(' - ', ';').replace('Strike R$ ', '').replace(',000;Vencimento ', ',00;')\n", + "\n", + " Tipo = soup.find_all('h1')[0].text.replace(' - ', ';').replace(' de BOVA11', '').replace('Strike R$ ', '').replace(',000;Vencimento ', ',00;')\n", + "\n", + " #Tipo = soup.find_all('h1')[0].text.replace(' - ', ';').replace(' de B3SA3', '').replace('Strike R$ ', '').replace('0;Vencimento ', ';')\n", + "\n", + " Dia = soup.find_all('td')[21].text\n", + "\n", + " Cotação = soup.find_all('td')[25].text\n", + "\n", + " BOVA11 = soup.find_all('td')[37].text\n", + "\n", + " Dados = Tipo + ';' + Dia + ';' + Cotação + ';' + BOVA11 #+ \"\\n\"\n", + " Dados1 = Dados.split(\";\")\n", + " data.append(Dados1)\n", + " print(Dados1)\n", + " #print( Strike + ';' + value + ';' + Dia + ';' + Cotação, end=\"\\n\")\n", + " #for tag in soup.find_all('td'):\n", + " # print(tag.text)\n", + " #print(soup.td.text)\n", + " #print(soup.find('td'))\n", + " #print(soup)\n", + " #sleep(2)\n", + " else:\n", + " print(f'Error: {response.status_code}')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RRzGsIbPUzUl", + "outputId": "dbc1a4bf-1742-46b9-8323-32bf0de7649b" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['BOVAT6', 'PUT', '130,00', '21/08/2026', '07/03/2025', '5,15', '121,95']\n", + "['BOVAD117', 'CALL', '117,00', '17/04/2025', '07/03/2025', '6,87', '121,95']\n", + "['BOVAD118', 'CALL', '118,00', '17/04/2025', '07/03/2025', '6,14', '121,95']\n", + "['BOVAP4', 'PUT', '122,00', '17/04/2025', '07/03/2025', '1,64', '121,95']\n", + "['BOVAP124', 'PUT', '124,00', '17/04/2025', '07/03/2025', '2,56', '121,95']\n", + "['BOVAT7', 'PUT', '135,00', '21/08/2026', '07/03/2025', '6,50', '121,95']\n", + "['BOVAD117', 'CALL', '117,00', '17/04/2025', '07/03/2025', '6,87', '121,95']\n", + "['BOVAP3', 'PUT', '121,00', '17/04/2025', '07/03/2025', '1,29', '121,95']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "df = pd.DataFrame(data)\n", + "df.rename(columns={0: 'CÓDIGO', 1: 'TIPO', 2: 'STRIKE', 3: 'VENCIMENTO', 4: 'DATA', 5: 'COTAÇÃO' , 6: 'FECHAMENTO_BOVA11',7: 'DIFERENÇA_PARA_STRIKE',8: 'PRAZO_PARA_STRIKE'}, inplace=True)\n", + "\n", + "df['STRIKE'] = df['STRIKE'].str.replace(',', '.').astype(float)\n", + "df['COTAÇÃO'] = df['COTAÇÃO'].str.replace(',', '.').astype(float)\n", + "df['VENCIMENTO'] = pd.to_datetime(df['VENCIMENTO'],dayfirst=True)\n", + "df['DATA'] = pd.to_datetime(df['DATA'], dayfirst=True)\n", + "df['FECHAMENTO_BOVA11'] = df['FECHAMENTO_BOVA11'].str.replace(',', '.').astype(float)\n", + "df['DIFERENÇA_PARA_STRIKE'] = df['STRIKE'] - df['FECHAMENTO_BOVA11']\n", + "Hoje = np.datetime64('today')\n", + "df['PRAZO_PARA_STRIKE'] = df['VENCIMENTO'] - Hoje #- pd.Timedelta(days=1)\n", + "df['POSIÇÃO'] = [100,-91,-24,-54,-64,175,-112,-175]\n", + "df['FINANCEIRO'] = df['COTAÇÃO'] * df['POSIÇÃO']\n", + "df['EXPOSIÇÃO'] = df['POSIÇÃO'] * df['STRIKE']\n", + "df.loc['TOTAL'] = df.sum(numeric_only=True, axis=0)\n", + "print('MONTADO')\n", + "print(df)\n", + "\n", + "#df.to_excel('BOVA11.xlsx')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_CF5JdYVbnRQ", + "outputId": "bdf2687f-a075-4058-9c2f-061e76521864" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "MONTADO\n", + " CÓDIGO TIPO STRIKE VENCIMENTO DATA COTAÇÃO \\\n", + "0 BOVAT6 PUT 130.0 2026-08-21 2025-03-07 5.15 \n", + "1 BOVAD117 CALL 117.0 2025-04-17 2025-03-07 6.87 \n", + "2 BOVAD118 CALL 118.0 2025-04-17 2025-03-07 6.14 \n", + "3 BOVAP4 PUT 122.0 2025-04-17 2025-03-07 1.64 \n", + "4 BOVAP124 PUT 124.0 2025-04-17 2025-03-07 2.56 \n", + "5 BOVAT7 PUT 135.0 2026-08-21 2025-03-07 6.50 \n", + "6 BOVAD117 CALL 117.0 2025-04-17 2025-03-07 6.87 \n", + "7 BOVAP3 PUT 121.0 2025-04-17 2025-03-07 1.29 \n", + "TOTAL NaN NaN 984.0 NaT NaT 37.02 \n", + "\n", + " FECHAMENTO_BOVA11 DIFERENÇA_PARA_STRIKE PRAZO_PARA_STRIKE POSIÇÃO \\\n", + "0 121.95 8.05 529 days 100.0 \n", + "1 121.95 -4.95 38 days -91.0 \n", + "2 121.95 -3.95 38 days -24.0 \n", + "3 121.95 0.05 38 days -54.0 \n", + "4 121.95 2.05 38 days -64.0 \n", + "5 121.95 13.05 529 days 175.0 \n", + "6 121.95 -4.95 38 days -112.0 \n", + "7 121.95 -0.95 38 days -175.0 \n", + "TOTAL 975.60 8.40 NaT -245.0 \n", + "\n", + " FINANCEIRO EXPOSIÇÃO \n", + "0 515.00 13000.0 \n", + "1 -625.17 -10647.0 \n", + "2 -147.36 -2832.0 \n", + "3 -88.56 -6588.0 \n", + "4 -163.84 -7936.0 \n", + "5 1137.50 23625.0 \n", + "6 -769.44 -13104.0 \n", + "7 -225.75 -21175.0 \n", + "TOTAL -367.62 -25657.0 \n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + ":15: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", + " df.loc['TOTAL'] = df.sum(numeric_only=True, axis=0)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Para dataframe\n", + "\n", + "Opções = ['PETRP394']\n", + "\n", + "data10 = []\n", + "for value in Opções:\n", + "\n", + " url = 'https://opcoes.net.br/' + value\n", + " # Get the content of the webpage\n", + " response = requests.get(url)\n", + "\n", + " # Check if content is received without error\n", + " if response.status_code == 200:\n", + " page_content = response.content\n", + "\n", + " soup = BeautifulSoup(page_content, 'html.parser')\n", + "\n", + " Strike = soup.title.text.replace(' - ', ';').replace('Strike R$ ', '').replace(',000;Vencimento ', ',00;')\n", + "\n", + " #Tipo = soup.find_all('h1')[0].text.replace(' - ', ';').replace(' de PETR4', '').replace('Strike R$ ', '').replace(',000;Vencimento ', ',00;')\n", + "\n", + " Tipo = soup.find_all('h1')[0].text.replace(' - ', ';').replace(' de PETR4', '').replace('Strike R$ ', '').replace('0;Vencimento ', ';')\n", + "\n", + " Dia = soup.find_all('td')[21].text\n", + "\n", + " Cotação = soup.find_all('td')[25].text\n", + "\n", + " BOVA11 = soup.find_all('td')[37].text\n", + "\n", + " Dados = Tipo + ';' + Dia + ';' + Cotação + ';' + BOVA11 #+ \"\\n\"\n", + " Dados1 = Dados.split(\";\")\n", + " data10.append(Dados1)\n", + " print(Dados1)\n", + " #print( Strike + ';' + value + ';' + Dia + ';' + Cotação, end=\"\\n\")\n", + " #for tag in soup.find_all('td'):\n", + " # print(tag.text)\n", + " #print(soup.td.text)\n", + " #print(soup.find('td'))\n", + " #print(soup)\n", + " #sleep(2)\n", + " else:\n", + " print(f'Error: {response.status_code}')" + ], + "metadata": { + "id": "r8-FzfoKKR7-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c26f11db-094a-489b-82ec-eb79ebd7743c" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "['PETRP394', 'PUT', '35,66', '17/04/2025', '07/03/2025', '1,29', '34,63']\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "df = pd.DataFrame(data10)\n", + "df.rename(columns={0: 'CÓDIGO', 1: 'TIPO', 2: 'STRIKE', 3: 'VENCIMENTO', 4: 'DATA', 5: 'COTAÇÃO' , 6: 'FECHAMENTO_PETR4',7: 'DIFERENÇA_PARA_STRIKE',8: 'PRAZO_PARA_STRIKE'}, inplace=True)\n", + "\n", + "df['STRIKE'] = df['STRIKE'].str.replace(',', '.').astype(float)\n", + "df['COTAÇÃO'] = df['COTAÇÃO'].str.replace(',', '.').astype(float)\n", + "df['VENCIMENTO'] = pd.to_datetime(df['VENCIMENTO'],dayfirst=True)\n", + "df['DATA'] = pd.to_datetime(df['DATA'], dayfirst=True)\n", + "df['FECHAMENTO_PETR4'] = df['FECHAMENTO_PETR4'].str.replace(',', '.').astype(float)\n", + "df['DIFERENÇA_PARA_STRIKE'] = df['STRIKE'] - df['FECHAMENTO_PETR4']\n", + "Hoje = np.datetime64('today')\n", + "df['PRAZO_PARA_STRIKE'] = df['VENCIMENTO'] - Hoje #- pd.Timedelta(days=1)\n", + "df['POSIÇÃO'] = [-100]\n", + "df['FINANCEIRO'] = df['COTAÇÃO'] * df['POSIÇÃO']\n", + "df['EXPOSIÇÃO'] = df['POSIÇÃO'] * df['STRIKE']\n", + "df.loc['TOTAL'] = df.sum(numeric_only=True, axis=0)\n", + "print('MONTADO')\n", + "print(df)\n", + "#df.to_excel('PETR4.xlsx')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "DvGjbFp8AIFG", + "outputId": "562cfcaa-6a3f-4623-ef90-9777cafd81e7" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "MONTADO\n", + " CÓDIGO TIPO STRIKE VENCIMENTO DATA COTAÇÃO FECHAMENTO_PETR4 \\\n", + "0 PETRP394 PUT 35.66 2025-04-17 2025-03-07 1.29 34.63 \n", + "TOTAL NaN NaN 35.66 NaT NaT 1.29 34.63 \n", + "\n", + " DIFERENÇA_PARA_STRIKE PRAZO_PARA_STRIKE POSIÇÃO FINANCEIRO EXPOSIÇÃO \n", + "0 1.03 38 days -100.0 -129.0 -3566.0 \n", + "TOTAL 1.03 NaT -100.0 -129.0 -3566.0 \n" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + ":15: FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries is deprecated. In a future version, this will no longer exclude empty or all-NA columns when determining the result dtypes. To retain the old behavior, exclude the relevant entries before the concat operation.\n", + " df.loc['TOTAL'] = df.sum(numeric_only=True, axis=0)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "Opções = ['BOVAT12','BOVAT13','BOVAH125','BOVAT125','BOVAH125W4','BOVAT125W4']\n", + "\n", + "url = 'https://opcoes.net.br/' + Opções[0]\n", + " # Get the content of the webpage\n", + "response = requests.get(url)\n", + "\n", + " # Check if content is received without error\n", + "if response.status_code == 200:\n", + " page_content = response.content\n", + "\n", + " soup = BeautifulSoup(page_content, 'html.parser')\n", + "\n", + " Tabela = soup.find_all('table')[0]\n", + "\n", + " #Dados = Strike + ';' + value + ';' + Dia + ';' + Cotação #+ \"\\n\"\n", + " rows = Tabela.findChildren(['td'])\n", + "\n", + " table = soup.find('table', attrs={'class':'table table-bordered table-condensed top-buffer-20'})\n", + " table_rows = table.find_all('tr')\n", + "\n", + " #print(Tabela)\n", + " data = []\n", + " for row in table.find_all('tr'):\n", + " row_data = []\n", + " for cell in row.find_all('td'):\n", + " row_data.append(cell.text)\n", + " data.append(row_data)\n", + "else:\n", + " print(f'Error: {response.status_code}')\n", + "\n", + "df = pd.DataFrame(data)\n", + "print(df)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Fx408V40us1S", + "outputId": "a9a8311f-aa2e-4b1d-a62a-559655fda172" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " 0 1 \\\n", + "0 BOVAT12 - Cotação não ajustada \n", + "1 Min Pri \n", + "2 07/08/2024 4,55 \n", + "3 06/08/2024 4,61 \n", + "4 05/08/2024 5,20 \n", + ".. ... ... \n", + "251 02/08/2023 9,09 \n", + "252 27/07/2023 8,99 \n", + "253 26/07/2023 9,00 \n", + "254 Min 3,99 \n", + "255 Max 10,71 \n", + "\n", + " 2 3 4 \\\n", + "0 BOVAT12 - Volatilidade implícita BOVA11 - Cotação não ajustada None \n", + "1 Med Ult Max \n", + "2 4,97 4,92 5,04 \n", + "3 5,60 5,47 4,62 \n", + "4 5,76 5,75 5,50 \n", + ".. ... ... ... \n", + "251 9,25 9,20 9,09 \n", + "252 9,00 8,99 9,25 \n", + "253 9,00 9,00 9,00 \n", + "254 4,00 4,06 4,10 \n", + "255 11,40 11,19 11,38 \n", + "\n", + " 5 6 7 8 9 10 11 12 \\\n", + "0 None None None None None None None None \n", + "1 Negócios Vol. Fin. Min Pri Med Ult Max Min \n", + "2 5,04 6 10.298,050 19,11 20,23 19,81 20,14 20,23 \n", + "3 5,60 12 26.713,440 19,20 20,46 20,15 19,26 20,53 \n", + "4 5,80 13 86.980,800 19,18 19,33 19,35 19,96 19,96 \n", + ".. ... ... ... ... ... ... ... ... \n", + "251 9,25 3 174,950 30,69 30,76 30,73 30,69 30,76 \n", + "252 9,25 4 2.357,850 30,56 30,56 30,60 30,73 30,73 \n", + "253 9,00 1 90,000 30,75 30,75 30,75 30,75 30,75 \n", + "254 4,20 1 12,250 14,93 16,75 18,53 18,49 18,79 \n", + "255 12,00 2.032 843.169,760 30,75 32,65 30,76 32,86 33,06 \n", + "\n", + " 13 14 15 16 17 \n", + "0 None None None None None \n", + "1 Abe Med Ult Max None \n", + "2 123,18 123,74 123,62 124,04 124,09 \n", + "3 117,94 122,18 122,77 122,74 123,49 \n", + "4 119,53 119,88 121,01 121,77 121,80 \n", + ".. ... ... ... ... ... \n", + "251 115,97 116,92 116,85 117,01 117,47 \n", + "252 116,00 118,66 116,80 116,04 118,80 \n", + "253 117,55 117,85 118,23 118,74 118,89 \n", + "254 108,19 108,90 109,53 108,96 110,15 \n", + "255 129,86 129,86 130,27 130,46 130,52 \n", + "\n", + "[256 rows x 18 columns]\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# The URL for the webpage\n", + "\n", + "Opções = ['BOVAT12','BOVA11']\n", + "\n", + "url = 'https://www.ibovx.com.br/historico-opcoes-bovespa.aspx?tipo=1&papel=' + Opções[0]\n", + "# Get the content of the webpage\n", + "response = requests.get(url)\n", + "\n", + "# Check if content is received without error\n", + "if response.status_code == 200:\n", + " page_content = response.content\n", + " soup = BeautifulSoup(page_content, 'html.parser')\n", + "\n", + " Dia = soup.find_all('td')[9].text\n", + " print(Dia)\n", + " Cotação = soup.find_all('td')[12].text\n", + "\n", + " print(Cotação)\n", + " #for tag in soup.find_all('td'):\n", + " # print(tag.text)\n", + " #print(soup.td.text)\n", + " #print(soup.find('td'))\n", + " #print(soup)\n", + "\n", + "\n", + "else:\n", + " print(f'Error: {response.status_code}')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bQVvC6JGG5Ui", + "outputId": "4d1aa950-1ad7-47f7-ba54-0f695baa01a7" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "12/12/2024\n", + "3,81\n" + ] + } + ] + } + ] +}