How to make simple watchlist from a running list of tickers with python?I am trying to gather tickers that close above the previous day close from a small list of tickers during a specific time period using python?I am coming up with No data based on my conditions written below but when I look manual I know the conditions are true for ABML on 12-7-21. I can't seem to figure out what I am missing. - import datetime as dt
- import pandas as pd
- from pandas_datareader import data as pdr
- import yfinance as yf
- from tkinter import Tk
- from tkinter.filedialog import askopenfilename
- import os
- from pandas import ExcelWriter
- yf.pdr_override()
- start = dt.datetime(2021, 11, 30)
- now = dt.datetime(2021, 12, 15) # dt.datetime.now()
- filePath = ('your file path')
- stocklist = pd.read_excel(filePath)
- stocklist = stocklist.head()
- #*************************************************************************
- exportList = pd.DataFrame(columns=['Stock', "Date", "Open"])
- for i in stocklist.index:
- stock = str(stocklist["Symbol"][i])
- #RS_Rating = stocklist["RS Rating"][i]
- try:
- df = pdr.get_data_yahoo(stock, start, now)
- PreviousClose = df["Close"][-1]
- CurrentClose = df["Close"][0]
- CurrentDate = df["Date"][0]
- CurrentOpen = df["Open"][0]
- # Condition 1: Explain your criteria
- if (PreviousClose < CurrentClose):
- cond_1 = True
- else:
- cond_1 = False
- # Condition 2: Explain your criteria
- if (PreviousClose < CurrentClose):
- cond_2 = True
- else:
- cond_2 = False
- if (cond_1 and cond_2):
- exportList = exportList.append({'Stock': stock, "Date": CurrentDate, "Open": CurrentOpen}, ignore_index=True)
- except Exception:
- print("No data on " + stock)
- print(exportList)
- newFile = os.path.dirname(filePath) + "/ScreenOutput.xlsx"
- writer = ExcelWriter(newFile)
- exportList.to_excel(writer, "Sheet1")
- writer.save()
複製代碼
|