Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. # ---------------------------------------------------------------------------- #
  2.                     ### General packages ###
  3. import datetime as dt
  4. from datetime import date
  5. import time
  6.                     ### Data / DataViz packages ###
  7. import pandas as pd
  8. import numpy as np
  9. import pandas_datareader.data as web
  10. from pandas import ExcelWriter
  11.                     ### GUI packages ###
  12. import tkinter as tk
  13. from tkinter import PhotoImage
  14.                     ### Exporting to executable ###
  15. # ---------------------------------------------------------------------------- #
  16.                     ### Deriving current date ###
  17. currentDT = dt.datetime.now()
  18.  
  19.                     ### Setting stage for final export ###
  20. def export_excel(x):
  21.     try:
  22.         ## Datetime Modulation
  23.         start = date.today() - dt.timedelta(days=365*2)
  24.         end = date.today()
  25.         ## Ticker Modulation
  26.         port = web.DataReader(x, 'yahoo', start, end)
  27.         port = port.drop("Adj Close", axis=1) #OUSEMG
  28.         port[''] = np.nan
  29.         port.reindex(columns=[('Open',x), ('High',x), ('Low',x), ('Close',x), ('Volume',x), ('', '')])
  30.         port.columns = ['Open', 'High', 'Low', 'Close', 'Volume', '']
  31.         ## S&P 500 Modulation
  32.         SP500 = web.DataReader('^GSPC', 'yahoo', start, end)
  33.         SP500.index = SP500.index.date
  34.         SP500 = SP500.drop("Adj Close", axis=1)
  35.         SP500['Date'] = SP500.index
  36.         SP500 = SP500.reindex(columns=[('Date'),('Open'), ('High'), ('Low'), ('Close'), ('Volume')])
  37.         ## Combine both user input and S&P data
  38.         port = pd.concat([port, SP500], axis=1) #OUSEMG
  39.         port.index = SP500['Date']
  40.         file_name = str(currentDT.month)+"."+str(currentDT.day)+"."+str(currentDT.year)
  41.         port.to_excel(x+"."+file_name+"_PyPortvas.xlsx")
  42.         print("Done! Check your folder. Your file is named "+x+"."+file_name+"_PyPortvas.xlsx"".")
  43.     except:
  44.         ## In case of any type of error
  45.         print("An error has occurred. You either mistyped the ticker \nor this stock has not been public for two years.")
  46.  
  47. ## General attributes
  48. HEIGHT = 300
  49. WIDTH = 550
  50.  
  51. ## Root, canvas, frame and general tkinter attributes
  52. root = tk.Tk()
  53. root.configure(bg="white",borderwidth=0, highlightthickness=0)
  54. canvas = tk.Canvas(root, bg="white",height=HEIGHT, width=WIDTH)
  55. canvas.pack()
  56. root.title("PyPortvas OUSEMG Edition 0.0.1")
  57. frame = tk.Frame(canvas, bg="white",borderwidth=0, highlightthickness=0).place(relwidth=0.75, relheight=0.1, anchor='n')
  58.  
  59. ## "Ticker: "
  60. label = tk.Label(frame, text="Ticker: ", font=("Tahoma",20),bg="white").place(relwidth=0.25, relheight=.25, relx=(.15),rely=.750)
  61.  
  62. ### User input
  63. stock_entry = tk.Entry(frame, font=("Tahoma",20))
  64. stock_entry.place(relwidth=0.35, relheight=.15,relx= .25+.1, rely=.8)
  65.  
  66. ## Logo
  67. photo = tk.PhotoImage(file = 'Logo.gif')
  68. logo = tk.Label(frame ,image = photo, borderwidth=0, highlightthickness=0)
  69. logo.image = photo #keeping a reference in this line
  70. logo.place(rely=.15,relx=.3)
  71.  
  72.  
  73. ## Final export button
  74. button= tk.Button(frame, text="Export",font=("Tahoma",20), fg="#ffffff",bg="#006600",command=lambda: export_excel(stock_entry.get()))
  75. button.place(relwidth=0.3, relheight=.15, relx=.5, rely=.8)
  76.  
  77. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement