-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python cache alterations made to reduce fetch
- Loading branch information
1 parent
602f181
commit e36720a
Showing
12 changed files
with
206 additions
and
21 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+81 Bytes
(100%)
backend/myapp/__pycache__/graph_stock_holdings_day.cpython-38.pyc
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import json | ||
from django.http import JsonResponse | ||
from django.conf import settings | ||
from pathlib import Path | ||
import yfinance as yf | ||
import pytz | ||
from datetime import datetime, timedelta | ||
from collections import defaultdict | ||
|
||
# Your timezone, for example, 'UTC' | ||
timezone = pytz.timezone('EST') | ||
|
||
def get_current_shares_held(ticker): | ||
if not ticker: | ||
return JsonResponse({"error": "Ticker symbol is required."}, status=400) | ||
|
||
# Define the path to the JSON file | ||
json_file_path = Path(settings.BASE_DIR) / 'data' / 'investments_data.json' | ||
|
||
# Open the JSON file and load its content | ||
with open(json_file_path, 'r') as file: | ||
transactions_data = json.load(file) | ||
|
||
transactions = [t for t in transactions_data if t['Ticker Symbol'] == ticker] | ||
|
||
if not transactions: | ||
return JsonResponse({"error": "No transactions found for given ticker."}, status=404) | ||
|
||
current_shares = 0.0 | ||
|
||
# Calculate the current number of shares held | ||
for transaction in transactions: | ||
shares = float(transaction["No. of Shares"]) | ||
if transaction["Transaction Type"] == "BUY": | ||
current_shares += shares | ||
elif transaction["Transaction Type"] == "SELL": | ||
current_shares -= shares | ||
|
||
return current_shares | ||
|
||
def get_all_holdings_day(request): | ||
json_file_path = Path(settings.BASE_DIR) / 'data' / 'investments_data.json' | ||
|
||
with open(json_file_path, 'r') as file: | ||
transactions_data = json.load(file) | ||
|
||
# Get a list of unique tickers | ||
tickers = set(t['Ticker Symbol'] for t in transactions_data) | ||
|
||
# Aggregated data for all stocks | ||
aggregated_data = defaultdict(lambda: {"total_value": 0, "value_paid": 0}) | ||
|
||
# Store previous day's closing value | ||
total_value_paid = 0 | ||
|
||
for ticker in tickers: | ||
shares_held = get_current_shares_held(ticker) | ||
|
||
if shares_held <= 0: | ||
continue | ||
|
||
stock = yf.Ticker(ticker) | ||
today = datetime.now(tz=timezone).date() | ||
|
||
# Find most recent market day with data | ||
for i in range(7): | ||
check_day = today - timedelta(days=i) | ||
day_data = stock.history(period='1d', interval='1m', start=check_day, end=check_day + timedelta(days=1)) | ||
prev_day_data = stock.history(period="2d", interval="1d") | ||
if not day_data.empty and len(prev_day_data) >= 2: | ||
previous_close = prev_day_data['Close'].iloc[-2] | ||
total_value_paid += previous_close * shares_held | ||
break | ||
|
||
if day_data.empty or len(prev_day_data) < 2: | ||
continue # Skip if no data available | ||
|
||
for time, row in day_data.iterrows(): | ||
# Add the value of this stock to the aggregated data | ||
aggregated_data[time]["total_value"] += row['Close'] * shares_held | ||
aggregated_data[time]["value_paid"] = total_value_paid | ||
|
||
# Convert aggregated data to a list format suitable for JsonResponse | ||
historical_values = [{"date": time.tz_convert(timezone).strftime('%Y-%m-%d %H:%M:%S'), | ||
"total_value": data["total_value"], | ||
"value_paid": data["value_paid"]} | ||
for time, data in aggregated_data.items()] | ||
|
||
return JsonResponse(historical_values, safe=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Line } from 'react-chartjs-2'; | ||
import 'chart.js/auto'; | ||
import 'chartjs-adapter-date-fns'; | ||
|
||
function AllGraphDay({ ticker, timeFrame }) { | ||
const [stockData, setStockData] = useState([]); | ||
|
||
useEffect(() => { | ||
// Ensure the ticker value is included in the fetch URL | ||
fetch(`http://localhost:8000/api/graph_all_day/`) | ||
.then(response => response.json()) | ||
.then(data => setStockData(data)) | ||
.catch(error => console.error('Error fetching data:', error)); | ||
}, [ticker]); | ||
|
||
const chartData = { | ||
labels: stockData.map(data => data.date), | ||
datasets: [ | ||
{ | ||
// Dataset for stock value | ||
label: 'Stock Value', | ||
data: stockData.map(data => data.value), | ||
fill: false, | ||
backgroundColor: 'rgba(75, 192, 192 0.1)', | ||
borderColor: 'rgb(75, 245, 192)', | ||
borderWidth: 0.8, | ||
tension: 0.1, | ||
pointRadius: 0, | ||
hoverRadius: 0, | ||
}, | ||
{ | ||
// Dataset for cumulative investment | ||
label: 'Cumulative Investment', | ||
data: stockData.map(data => data.value_paid), | ||
fill: false, | ||
|
||
borderColor: 'rgb(245, 245, 245)', | ||
borderWidth: 0.75, | ||
tension: 0.1, | ||
pointRadius: 0, | ||
hoverRadius: 0, | ||
}, | ||
] | ||
}; | ||
|
||
const chartOptions = { | ||
responsive: true, | ||
plugins: { | ||
legend: { | ||
display: false | ||
}, | ||
tooltip: { | ||
mode: 'index', | ||
intersect: false | ||
}, | ||
}, | ||
scales: { | ||
y: { | ||
beginAtZero: false | ||
}, | ||
x: { | ||
type: 'time', | ||
time: { | ||
unit: 'month', | ||
displayFormats: { | ||
month: 'dd-MM-yy-HH:mm' | ||
} | ||
}, | ||
ticks: { | ||
display: false | ||
} | ||
} | ||
}, | ||
animation: { | ||
duration: 1000 // Animation duration in milliseconds | ||
} | ||
}; | ||
|
||
|
||
return ( | ||
<div className="stock-graph-container"> | ||
<Line data={chartData} options={chartOptions} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default AllGraphDay; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters