Skip to content

Commit

Permalink
addition of valuePaid line compared currentValue
Browse files Browse the repository at this point in the history
  • Loading branch information
eethansmith committed Dec 5, 2023
1 parent ee73b6d commit 09b21a6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
10 changes: 5 additions & 5 deletions backend/data/investments_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1628,11 +1628,11 @@
"Date": "19-08-2021",
"Time": "16:03",
"Ticker Symbol": "GOOG",
"No. of Shares": "0.03422148",
"Price per Share USD": "2716.13",
"No. of Shares": "0.67994620733",
"Price per Share USD": "$120.11",
"Image Filename": "IMG_2110.PNG",
"Transaction Valuation USD": 92.9499884724,
"Overall Holdings": 0.03422148,
"Overall Holdings": 0.74294620733,
"Average Cost per Share USD": 2716.13,
"Realized Gain/Loss USD": 0,
"Portfolio Valuation USD": 2885.8096126549
Expand Down Expand Up @@ -1740,8 +1740,8 @@
"Date": "13-09-2021",
"Time": "17:18",
"Ticker Symbol": "GOOG",
"No. of Shares": "0.03422148",
"Price per Share USD": "$2848.50",
"No. of Shares": "0.67994620733",
"Price per Share USD": "$132.39",
"Image Filename": "IMG_2199.PNG",
"Transaction Valuation USD": 97.47988577999999,
"Overall Holdings": 0.0,
Expand Down
Binary file modified backend/myapp/__pycache__/graph_stock_holdings.cpython-38.pyc
Binary file not shown.
14 changes: 13 additions & 1 deletion backend/myapp/graph_stock_holdings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,39 @@ def get_stock_history(request, ticker):

historical_values = []
current_shares = 0
value_paid = 0

# Before the loop, sort transactions by date
transactions.sort(key=lambda x: datetime.strptime(x["Date"], '%d-%m-%Y'))

for date, row in historical_prices.iterrows():
date = date.to_pydatetime().replace(tzinfo=None) # Make date timezone naive
current_shares = 0 # Reset current shares for each date
value_paid = 0

# Accumulate shares up to the current date
for transaction in transactions:
transaction_date = datetime.strptime(transaction["Date"], '%d-%m-%Y')

if transaction_date <= date:
shares = float(transaction["No. of Shares"])
transaction_amount = float(transaction["Transaction Valuation USD"])
if transaction["Transaction Type"] == "BUY":
value_paid += transaction_amount
current_shares += shares
elif transaction["Transaction Type"] == "SELL":
value_paid -= transaction_amount
current_shares -= shares
if value_paid <= 0:
value_paid = 0

# Calculate value for the current date
value_paid = value_paid
value = current_shares * row['Close']
historical_values.append({"date": date.strftime('%Y-%m-%d'), "value": value})
historical_values.append({
"date": date.strftime('%Y-%m-%d'),
"value": value,
"value_paid": value_paid
})

return JsonResponse(historical_values, safe=False)
37 changes: 26 additions & 11 deletions frontend/src/StockGraphing.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,32 @@ function StockGraph({ ticker }) { // Use destructuring to get the ticker prop

const chartData = {
labels: stockData.map(data => data.date),
datasets: [{
data: stockData.map(data => data.value),
fill: true,
label: '',
backgroundColor: 'rgba(245, 245, 245, 0.2)',
borderColor: 'rgb(245, 245, 245)',
borderWidth: 0.8,
tension: 0.01,
pointRadius: 0,
hoverRadius: 0,
}]
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 = {
Expand Down

0 comments on commit 09b21a6

Please sign in to comment.