Advertisement
actorcat

Yahoo Pairs

Sep 11th, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.98 KB | None | 0 0
  1. import yfinance as yf
  2. from openpyxl import load_workbook
  3. from tkinter import *
  4. import datetime
  5.  
  6. root = Tk()
  7. root.geometry(f'300x400')
  8. root.configure(background="green")
  9.  
  10. global ticker1, ticker2, sdate, edate
  11. global eticker1, eticker2, esdate, eedate
  12.  
  13. edate = datetime.date.today()
  14. sdate = edate.replace(year=edate.year - 1)
  15.  
  16. # # Fetch data for specific stocks
  17. def get_tickers():
  18.     wb = load_workbook("pairs.xlsx")
  19.     ws = wb.active
  20.  
  21.     def clear_columns(ws, columns):
  22.         for col in columns:
  23.             for cell in ws[col]:
  24.                 cell.value = None
  25.  
  26.     clear_columns(ws, ['A', 'B', 'C', 'D', 'E'])
  27.  
  28.     ticker1 = eticker1.get()
  29.     ticker2 = eticker2.get()
  30.    
  31.     tickers = [ticker1, ticker2]  
  32.     columns = ['Close']
  33.     headers = ['Date'] + columns
  34.    
  35.     # Define starting column for each ticker
  36.     column_start = 1  # Start writing from column A
  37.     # Write headers for each ticker
  38.     for ticker_index, ticker in enumerate(tickers):
  39.         base_col = column_start + ticker_index * 3  # 3 columns per ticker (Date + 2 additional columns)
  40.         ws.cell(row=1, column=base_col, value='Date')
  41.         ws.cell(row=1, column=base_col + 1, value='Close')
  42.  
  43.         # Download the data
  44.         data = yf.download(ticker, start=sdate, end=edate)
  45.         print(sdate, edate)
  46.         # Reset index to include the date as a column
  47.         data.reset_index(inplace=True)
  48.        
  49.         # Check the columns in the DataFrame
  50.         #print("Columns in DataFrame:", data.columns)  # Debugging: Check column names
  51.  
  52.         # Write the data to the worksheet
  53.         for row_num, row_data in enumerate(data[['Date'] + columns].itertuples(index=False), start=2):
  54.             # Debugging: Print row data
  55.             #print("Writing row:", row_data)
  56.             ws.cell(row=row_num, column=base_col, value=row_data.Date)  # Write the date
  57.             ws.cell(row=row_num, column=base_col + 1, value=row_data.Close)  # Write the Close value
  58.     ws['G1'].value = ticker1
  59.     ws['H1'].value = ticker2
  60.     wb.save('pairs.xlsx')
  61.  
  62. def get_BTC_tickers():
  63.  
  64.     wb = load_workbook("BITCOIN.xlsx")
  65.     ws = wb.active
  66.    
  67.     def clear_columns(ws, columns):
  68.         for col in columns:
  69.             for cell in ws[col]:
  70.                 cell.value = None
  71.  
  72.     # Clear columns A to E
  73.     clear_columns(ws, ['A', 'B', 'C', 'D', 'E'])
  74.  
  75.  
  76.     ticker1 = eticker1.get()
  77.     ticker2 = eticker2.get()
  78.    
  79.     tickers = [ticker1, ticker2]  # Ensure cell references are correct
  80.     columns = ['Close']
  81.     headers = ['Date'] + columns
  82.  
  83.     # Define starting column for each ticker
  84.     column_start = 1  # Start writing from column A
  85.  
  86.     # Write headers for each ticker
  87.     for ticker_index, ticker in enumerate(tickers):
  88.         base_col = column_start + ticker_index * 3  # 3 columns per ticker (Date + 2 additional columns)
  89.         ws.cell(row=1, column=base_col, value='Date')
  90.         ws.cell(row=1, column=base_col + 1, value='Close')
  91.  
  92.         # Download the data
  93.         data = yf.download(ticker, start=sdate, end=edate)
  94.        
  95.         # Reset index to include the date as a column
  96.         data.reset_index(inplace=True)
  97.        
  98.         # Check the columns in the DataFrame
  99.         #print("Columns in DataFrame:", data.columns)  # Debugging: Check column names
  100.  
  101.         # Write the data to the worksheet
  102.         for row_num, row_data in enumerate(data[['Date'] + columns].itertuples(index=False), start=2):
  103.             # Debugging: Print row data
  104.             #print("Writing row:", row_data)
  105.             ws.cell(row=row_num, column=base_col, value=row_data.Date)  # Write the date
  106.             ws.cell(row=row_num, column=base_col + 1, value=row_data.Close)  # Write the Close value
  107.    
  108.     ws['G1'].value = ticker1
  109.     ws['H1'].value = ticker2
  110.     wb.save('BITCOIN.xlsx')
  111.     # Save the workbook
  112.    
  113.  
  114.  
  115. def startdate():
  116.     esdate.delete(0,END)
  117.     eedate.delete(0,END)
  118.     esdate.insert(0,sdate)
  119.     eedate.insert(0,edate)
  120.  
  121.  
  122.  
  123. # Create Text frame For textboxes
  124. T_frame = Frame(root, bg="green")
  125. # T_frame.pack_propagate(False)
  126. T_frame.pack(pady=20)
  127.  
  128. label1 = Label(T_frame, text = "Ticker1")
  129. label1.grid(row=1,column=0, pady = 0)
  130. eticker1 = Entry(T_frame, width = 15)
  131. eticker1.grid(row=1,column=1, pady = 10)
  132.  
  133. label2 = Label(T_frame, text = "Ticker2")
  134. label2.grid(row=2,column=0, pady = 0)
  135. eticker2= Entry(T_frame, width = 15)
  136. eticker2.grid(row=2,column=1, pady = 10)
  137.  
  138. label3 = Label(T_frame, text = "begin date")
  139. label3.grid(row=3,column=0, padx = 5, pady = 0)
  140. esdate = Entry(T_frame, width = 15)
  141. esdate.grid(row=3,column=1, pady = 10)
  142.  
  143.  
  144. label4 = Label(T_frame, text = "end date")
  145. label4.grid(row=4,column=0, pady = 0)
  146. eedate = Entry(T_frame, width = 15)
  147. eedate.grid(row=4,column=1, pady = 10)
  148.  
  149. button = Button(root, text="get STOCK", font=("Helvetica", 12), command=get_tickers)
  150. button.pack(pady=30)
  151.  
  152. button = Button(root, text="get BTC", font=("Helvetica", 12), command=get_BTC_tickers)
  153. button.pack()
  154.  
  155. startdate()
  156.  
  157. root.mainloop()
  158.  
  159.  
  160. print('fin')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement