Skip to content

Commit

Permalink
feat: Create README file
Browse files Browse the repository at this point in the history
  • Loading branch information
renan-siqueira committed Nov 20, 2023
1 parent 2241e97 commit 3162836
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 10 deletions.
99 changes: 98 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
# README
# Stock Investment Simulator

---

## Introduction

This project provides tools for simulating stock investments using historical data. It features a Python-based environment for fetching stock data, simulating investments, and visualizing investment outcomes.

## Features

- Data extraction from **Yahoo Finance API**.
- Simulation of stock investments based on historical data.
- Graphical representation of investment performance.
- Ability to process a list of stock tickers from a **JSON** file.

---

## Installation and Setup

### Prerequisites
- Python 3.8 or higher
- Pip (Python package manager)

### Setting Up a Virtual Environment
It's recommended to use a virtual environment to avoid conflicts with other Python projects or system-wide packages. To set up a virtual environment, navigate to the project's root directory and run:

```bash
python -m venv venv
```

To activate the virtual environment:

- On Windows:
```bash
.\venv\Scripts\activate
```

- On macOS and Linux:
```bash
source venv/bin/activate
```

### Installing Dependencies

With the virtual environment activated, install the required packages using:

```bash
pip install -r requirements.txt
```

---

## Usage

**Before starting to use the project, make sure that the paths within `src/settings/config.py` are created**

### Data Processor

The `data_processor.py` script is used to fetch and save historical stock data. The stock tickers are read from a JSON file named `stocks.json`.

Create a `stocks.json` file in the project root with the following format:

```json
{
"data": [
"AAPL",
"MSFT",
"GOOGL",
...
]
}
```

Run the `data_processor.py` script:

```bash
python data_processor.py
```

---

## Investment Simulation

Use `main.py` to simulate investments based on the fetched data.

1. Edit `main.py` to specify the stock ticker, purchase date, and sale date.

2. Run the script:

```bash
python main.py
```

---

## License

This project is open-sourced and available to everyone under the [MIT License](LICENSE).
19 changes: 10 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
from datetime import timedelta
from src.settings import config


def simulate_investment(data, buy_date, sell_date):
# Convertendo strings de data para objetos datetime
buy_date = pd.to_datetime(buy_date)
sell_date = pd.to_datetime(sell_date)

# Calculando o intervalo de datas para o gráfico
start_date = buy_date - timedelta(days=90)
end_date = sell_date + timedelta(days=90)

# Limitando os dados ao intervalo de interesse
plot_data = data.loc[start_date:end_date]

if buy_date not in plot_data.index or sell_date not in plot_data.index:
Expand All @@ -23,11 +21,11 @@ def simulate_investment(data, buy_date, sell_date):
return_on_investment = (sell_price - buy_price) / buy_price * 100

plt.figure(figsize=(10, 6))
plt.plot(plot_data.index, plot_data['Close'], label='Preço da Ação')
plt.plot(plot_data.index, plot_data['Close'], label='Stock Price')
plt.scatter([buy_date, sell_date], [buy_price, sell_price], color='red')
plt.title(f'Investimento em Ações: Compra em {buy_date.date()}, Venda em {sell_date.date()}')
plt.xlabel('Data')
plt.ylabel('Preço da Ação')
plt.title(f'Investment in Stocks: Purchase in {buy_date.date()}, Sell ​​in {sell_date.date()}')
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend()
plt.grid(True)
plt.show()
Expand All @@ -36,16 +34,19 @@ def simulate_investment(data, buy_date, sell_date):


def main():
buy_date = '2020-06-01'
sell_date = '2020-12-01'
ticker = "AAPL"

data = pd.read_csv(f"{config.APP_PATH_ASSETS_CSV_FOLDER}/{ticker.lower()}.csv")

data['Date'] = data['Date'].apply(lambda x: pd.to_datetime(x).strftime('%Y-%m-%d'))
data.set_index('Date', inplace=True)
data.index = pd.to_datetime(data.index)

result = simulate_investment(data, '2020-06-01', '2020-12-01')
result = simulate_investment(data, buy_date, sell_date)
print('Return (%):', result)


if __name__ == "__main__":
main()
main()

0 comments on commit 3162836

Please sign in to comment.