Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------------------------------------------------------------------------- #
- ### General packages ###
- import datetime as dt
- from datetime import date
- import time
- ### Data / DataViz packages ###
- import pandas as pd
- import numpy as np
- import pandas_datareader.data as web
- from pandas import ExcelWriter
- ### GUI packages ###
- import tkinter as tk
- from tkinter import PhotoImage
- ### Exporting to executable ###
- # ---------------------------------------------------------------------------- #
- ### Deriving current date ###
- currentDT = dt.datetime.now()
- ### Setting stage for final export ###
- def export_excel(x):
- try:
- ## Datetime Modulation
- start = date.today() - dt.timedelta(days=365*2)
- end = date.today()
- ## Ticker Modulation
- port = web.DataReader(x, 'yahoo', start, end)
- port = port.drop("Adj Close", axis=1) #OUSEMG
- port[''] = np.nan
- port.reindex(columns=[('Open',x), ('High',x), ('Low',x), ('Close',x), ('Volume',x), ('', '')])
- port.columns = ['Open', 'High', 'Low', 'Close', 'Volume', '']
- ## S&P 500 Modulation
- SP500 = web.DataReader('^GSPC', 'yahoo', start, end)
- SP500.index = SP500.index.date
- SP500 = SP500.drop("Adj Close", axis=1)
- SP500['Date'] = SP500.index
- SP500 = SP500.reindex(columns=[('Date'),('Open'), ('High'), ('Low'), ('Close'), ('Volume')])
- ## Combine both user input and S&P data
- port = pd.concat([port, SP500], axis=1) #OUSEMG
- port.index = SP500['Date']
- file_name = str(currentDT.month)+"."+str(currentDT.day)+"."+str(currentDT.year)
- port.to_excel(x+"."+file_name+"_PyPortvas.xlsx")
- print("Done! Check your folder. Your file is named "+x+"."+file_name+"_PyPortvas.xlsx"".")
- except:
- ## In case of any type of error
- print("An error has occurred. You either mistyped the ticker \nor this stock has not been public for two years.")
- ## General attributes
- HEIGHT = 300
- WIDTH = 550
- ## Root, canvas, frame and general tkinter attributes
- root = tk.Tk()
- root.configure(bg="white",borderwidth=0, highlightthickness=0)
- canvas = tk.Canvas(root, bg="white",height=HEIGHT, width=WIDTH)
- canvas.pack()
- root.title("PyPortvas OUSEMG Edition 0.0.1")
- frame = tk.Frame(canvas, bg="white",borderwidth=0, highlightthickness=0).place(relwidth=0.75, relheight=0.1, anchor='n')
- ## "Ticker: "
- label = tk.Label(frame, text="Ticker: ", font=("Tahoma",20),bg="white").place(relwidth=0.25, relheight=.25, relx=(.15),rely=.750)
- ### User input
- stock_entry = tk.Entry(frame, font=("Tahoma",20))
- stock_entry.place(relwidth=0.35, relheight=.15,relx= .25+.1, rely=.8)
- ## Logo
- photo = tk.PhotoImage(file = 'Logo.gif')
- logo = tk.Label(frame ,image = photo, borderwidth=0, highlightthickness=0)
- logo.image = photo #keeping a reference in this line
- logo.place(rely=.15,relx=.3)
- ## Final export button
- button= tk.Button(frame, text="Export",font=("Tahoma",20), fg="#ffffff",bg="#006600",command=lambda: export_excel(stock_entry.get()))
- button.place(relwidth=0.3, relheight=.15, relx=.5, rely=.8)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement