Skip to content

Commit ea94de0

Browse files
committed
initial backend version and fix front details
1 parent 55fedc5 commit ea94de0

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

backend/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
yfinance==0.2.40
2+
fastapi==0.114.2
3+
fastapi-cli==0.0.5

backend/server.py

+41-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
from yfinance_functions import process_symbol
12
from fastapi import FastAPI
3+
from fastapi.middleware.cors import CORSMiddleware
24

35
app = FastAPI()
46

7+
origins = [
8+
"http://localhost:3000",
9+
"http://localhost*",
10+
]
11+
12+
app.add_middleware(
13+
CORSMiddleware,
14+
allow_origins=origins,
15+
allow_credentials=True,
16+
allow_methods=["*"],
17+
allow_headers=["*"],
18+
)
19+
520

621
@app.get("/hello-world")
722
async def root():
@@ -10,5 +25,29 @@ async def root():
1025

1126
@app.post("/profit-calculate")
1227
async def profit_calculate(request: dict):
13-
print(request.json())
14-
return {"profit": 100}
28+
print(request)
29+
results = []
30+
for row in request['data']:
31+
symbol_result = process_symbol(
32+
row['SYMBOL'], row['AMOUNT'], row['PURCHASE_DATE'], row['PURCHASE_VALUE'])
33+
symbol_result['symbol'] = row['SYMBOL']
34+
symbol_result['amount'] = row['AMOUNT']
35+
results.append(symbol_result)
36+
return results
37+
38+
# {
39+
# "data": [
40+
# {
41+
# "SYMBOL": "BBSE3.sa",
42+
# "AMOUNT": 11,
43+
# "PURCHASE_DATE": "01/10/2023",
44+
# "PURCHASE_VALUE": 31.46,
45+
# }
46+
# {
47+
# "SYMBOL": "NIKE34.sa",
48+
# "AMOUNT": 7,
49+
# "PURCHASE_DATE": "09/10/2023",
50+
# "PURCHASE_VALUE": 47.04,
51+
# }
52+
# ]
53+
# }

backend/main.py backend/yfinance_functions.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import List
23

34
import pytz
45
import yfinance as yf
@@ -27,8 +28,21 @@ def get_current_price(symbol: str) -> float:
2728
current_price = stock.history(period="1d")["Close"].iloc[0]
2829
return current_price
2930

31+
def process_symbol(symbol: str,amount :int, purchase_date: str,purchase_value:float) -> dict:
32+
dividends = get_dividends(symbol, purchase_date)
33+
current_price = get_current_price(symbol)
34+
total_dividends = sum(dividends.values()) * amount if dividends else 0
3035

31-
def process_portfolio(filename):
36+
current_value = current_price * amount
37+
purchase_total = purchase_value * amount
38+
value_difference = current_value - purchase_total
39+
balance = value_difference + total_dividends
40+
41+
return {"current_value": current_value, "purchase_total": purchase_total, "value_difference": value_difference, "balance": balance, "total_dividends": total_dividends}
42+
43+
44+
45+
def process_portfolio(filename : str) -> List[dict]:
3246
results = []
3347

3448
with open(filename, newline="", encoding="utf-8") as csvfile:

0 commit comments

Comments
 (0)