Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import yfinance as yf
- from openpyxl import load_workbook
- from tkinter import *
- import datetime
- root = Tk()
- root.geometry(f'300x400')
- root.configure(background="green")
- global ticker1, ticker2, sdate, edate
- global eticker1, eticker2, esdate, eedate
- edate = datetime.date.today()
- sdate = edate.replace(year=edate.year - 1)
- # # Fetch data for specific stocks
- def get_tickers():
- wb = load_workbook("pairs.xlsx")
- ws = wb.active
- def clear_columns(ws, columns):
- for col in columns:
- for cell in ws[col]:
- cell.value = None
- clear_columns(ws, ['A', 'B', 'C', 'D', 'E'])
- ticker1 = eticker1.get()
- ticker2 = eticker2.get()
- tickers = [ticker1, ticker2]
- columns = ['Close']
- headers = ['Date'] + columns
- # Define starting column for each ticker
- column_start = 1 # Start writing from column A
- # Write headers for each ticker
- for ticker_index, ticker in enumerate(tickers):
- base_col = column_start + ticker_index * 3 # 3 columns per ticker (Date + 2 additional columns)
- ws.cell(row=1, column=base_col, value='Date')
- ws.cell(row=1, column=base_col + 1, value='Close')
- # Download the data
- data = yf.download(ticker, start=sdate, end=edate)
- print(sdate, edate)
- # Reset index to include the date as a column
- data.reset_index(inplace=True)
- # Check the columns in the DataFrame
- #print("Columns in DataFrame:", data.columns) # Debugging: Check column names
- # Write the data to the worksheet
- for row_num, row_data in enumerate(data[['Date'] + columns].itertuples(index=False), start=2):
- # Debugging: Print row data
- #print("Writing row:", row_data)
- ws.cell(row=row_num, column=base_col, value=row_data.Date) # Write the date
- ws.cell(row=row_num, column=base_col + 1, value=row_data.Close) # Write the Close value
- ws['G1'].value = ticker1
- ws['H1'].value = ticker2
- wb.save('pairs.xlsx')
- def get_BTC_tickers():
- wb = load_workbook("BITCOIN.xlsx")
- ws = wb.active
- def clear_columns(ws, columns):
- for col in columns:
- for cell in ws[col]:
- cell.value = None
- # Clear columns A to E
- clear_columns(ws, ['A', 'B', 'C', 'D', 'E'])
- ticker1 = eticker1.get()
- ticker2 = eticker2.get()
- tickers = [ticker1, ticker2] # Ensure cell references are correct
- columns = ['Close']
- headers = ['Date'] + columns
- # Define starting column for each ticker
- column_start = 1 # Start writing from column A
- # Write headers for each ticker
- for ticker_index, ticker in enumerate(tickers):
- base_col = column_start + ticker_index * 3 # 3 columns per ticker (Date + 2 additional columns)
- ws.cell(row=1, column=base_col, value='Date')
- ws.cell(row=1, column=base_col + 1, value='Close')
- # Download the data
- data = yf.download(ticker, start=sdate, end=edate)
- # Reset index to include the date as a column
- data.reset_index(inplace=True)
- # Check the columns in the DataFrame
- #print("Columns in DataFrame:", data.columns) # Debugging: Check column names
- # Write the data to the worksheet
- for row_num, row_data in enumerate(data[['Date'] + columns].itertuples(index=False), start=2):
- # Debugging: Print row data
- #print("Writing row:", row_data)
- ws.cell(row=row_num, column=base_col, value=row_data.Date) # Write the date
- ws.cell(row=row_num, column=base_col + 1, value=row_data.Close) # Write the Close value
- ws['G1'].value = ticker1
- ws['H1'].value = ticker2
- wb.save('BITCOIN.xlsx')
- # Save the workbook
- def startdate():
- esdate.delete(0,END)
- eedate.delete(0,END)
- esdate.insert(0,sdate)
- eedate.insert(0,edate)
- # Create Text frame For textboxes
- T_frame = Frame(root, bg="green")
- # T_frame.pack_propagate(False)
- T_frame.pack(pady=20)
- label1 = Label(T_frame, text = "Ticker1")
- label1.grid(row=1,column=0, pady = 0)
- eticker1 = Entry(T_frame, width = 15)
- eticker1.grid(row=1,column=1, pady = 10)
- label2 = Label(T_frame, text = "Ticker2")
- label2.grid(row=2,column=0, pady = 0)
- eticker2= Entry(T_frame, width = 15)
- eticker2.grid(row=2,column=1, pady = 10)
- label3 = Label(T_frame, text = "begin date")
- label3.grid(row=3,column=0, padx = 5, pady = 0)
- esdate = Entry(T_frame, width = 15)
- esdate.grid(row=3,column=1, pady = 10)
- label4 = Label(T_frame, text = "end date")
- label4.grid(row=4,column=0, pady = 0)
- eedate = Entry(T_frame, width = 15)
- eedate.grid(row=4,column=1, pady = 10)
- button = Button(root, text="get STOCK", font=("Helvetica", 12), command=get_tickers)
- button.pack(pady=30)
- button = Button(root, text="get BTC", font=("Helvetica", 12), command=get_BTC_tickers)
- button.pack()
- startdate()
- root.mainloop()
- print('fin')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement