Skip to content

Instantly share code, notes, and snippets.

@pretorh
Created December 18, 2022 06:11
Show Gist options
  • Save pretorh/eb8118fdbe0d53ff9a1d2523c3377ffc to your computer and use it in GitHub Desktop.
Save pretorh/eb8118fdbe0d53ff9a1d2523c3377ffc to your computer and use it in GitHub Desktop.
print stocks' last price in month as a csv string
"print stock prices as a csv string. see https://github.com/ranaroussi/yfinance for disclaimers"
import datetime
import sys
import yfinance as yf
def get_last_stock_price(stock, before):
"get the last price of the stock before the given date"
start = before - datetime.timedelta(days=7)
tick = yf.Ticker(stock)
history = tick.history(start=start, end=before)
last_history = list(history.Close.items())[-1]
last_date = last_history[0]
last_price = last_history[1]
return {
'stock': stock,
'date': last_date,
'price': last_price,
}
def main():
"prints the last price in previous month for given stock (from CLI arguments) as a csv string"
first_day_of_current_month = datetime.datetime.now().date().replace(day=1)
stocks = sys.argv[1:]
print('date,stock,price')
for stock in stocks:
last = get_last_stock_price(stock, before=first_day_of_current_month)
print(f'{last["date"].isoformat()},{stock},{last["price"]}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment