Skip to content

Instantly share code, notes, and snippets.

@fnneves
Created May 11, 2021 16:42
Show Gist options
  • Save fnneves/62006a9e879311daf68da39dcf25e75b to your computer and use it in GitHub Desktop.
Save fnneves/62006a9e879311daf68da39dcf25e75b to your computer and use it in GitHub Desktop.
# getting the accumulated positions for our tickers
last_positions = final_filtered.groupby(['ticker']).agg({'cml_units': 'last', 'cml_cost': 'last',
'gain_loss': 'sum', 'cashflow': 'sum'}).reset_index()
curr_prices = []
# not the most elegant way of getting prices but will do for now
for tick in last_positions['ticker']:
stonk = yf.Ticker(tick)
price = stonk.info['regularMarketPrice']
curr_prices.append(price)
print(f'Done for {tick}')
last_positions['price'] = curr_prices # adding it to our dataframe
last_positions['current_value'] = (last_positions.price * last_positions.cml_units).round(2) # and now we can calculate
last_positions['avg_price'] = (last_positions.cml_cost / last_positions.cml_units).round(2) # and now we can calculate
last_positions = last_positions.sort_values(by='current_value', ascending=False) # sorting by current value
# Plotly part
donut_top = go.Figure()
donut_top.layout.template = CHART_THEME
donut_top.add_trace(go.Pie(labels=last_positions.head(15).ticker, values=last_positions.head(15).current_value))
donut_top.update_traces(hole=.4, hoverinfo="label+value+percent")
donut_top.update_traces(textposition='outside', textinfo='label+value')
donut_top.update_layout(showlegend=False)
donut_top.update_layout(margin = dict(t=50, b=50, l=25, r=25))
donut_top.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment