import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pandas_datareader as web
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
import sys
from loguru import logger
from collections import deque
import pprint
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
import os
from dotenv import load_dotenv
trade_spreadsheet_fullpath = 'trades_template.ods'
<<invest-imports>>
<<important-vars>>
<<fill-class>>
<<transaction-class>>
<<position-class>>
<<portfolio-class>>
<<imports-my-portfolio>>
<<env-vars>>
def emacs_output_position(ticker, value, shares, spent, gain, fifo):
display(f'----------------------------------------------------')
display(f'Ticker: {ticker}')
display(f'Value: {value}')
display(f'Spent: {spent}')
display(f'Owned Shares: {shares}')
display(f'Gain: {gain}')
display(f"FIFO Profit & Loss: {fifo}")
#display(f'----------------------------------------------------')
p = Portfolio(trade_spreadsheet_fullpath)
p.gen_positions()
total_gains = p.get_total_gain()
p.calc_stats()
print(f'Total portfolio gains: {total_gains}')
#%matplotlib inline
for ticker, position in p.positions.items():
fifo = position.calc_fifo()
value, shares, spent, gain = p.positions[ticker].calc_transactions()
emacs_output_position(ticker, value, shares, spent, gain, fifo)
<<invest-imports>>
<<important-vars>>
<<fill-class>>
<<transaction-class>>
<<position-class>>
<<portfolio-class>>
<<imports-my-portfolio>>
<<env-vars>>
ticker = "nvda"
start = five_yrs_ago
end = today_str
source = "yahoo"
ticker_data = web.DataReader(ticker, source, start, end)
<<invest-imports>>
<<important-vars>>
<<fill-class>>
<<transaction-class>>
<<position-class>>
<<portfolio-class>>
<<imports-my-portfolio>>
print(ticker_data.tail(7))
<<invest-imports>>
<<important-vars>>
<<fill-class>>
<<transaction-class>>
<<position-class>>
<<portfolio-class>>
<<imports-my-portfolio>>
# Vars
column_label = "Adj Close"
title = f'{ticker}: {column_label}'
xlabel = "Date"
ylabel = "Adjusted"
# Generate plot
%matplotlib inline
ticker_data[column_label].plot()
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
filename = f'{ticker}.png'
plt.savefig(filename)
filename