Skip to content

Instantly share code, notes, and snippets.

View marcosan93's full-sized avatar

Marco Santos marcosan93

View GitHub Profile
def getData(ticker, window, ma_period):
"""
Grabs price data from a given ticker. Retrieves prices based on the given time window; from now
to N days ago. Sets the moving average period for prediction. Returns a preprocessed DF
formatted for FB Prophet.
"""
# Time periods
now = datetime.now()
# How far back to retrieve tweets
# Initiating options
options = Options()
# Performing without GUI
options.headless = True
options.add_argument("--window-size=1920,1200")
# Accepting downloads without GUI
options.add_experimental_option("prefs", {
"download.default_directory": r"/Users/marcosantos/Downloads",
# Adding adblocker extension
options = Options()
options.add_extension(
"/Users/marcosantos/Downloads/extension_1_37_2_0.crx"
)
# Opening the browser
driver = webdriver.Chrome(
executable_path="/Users/marcosantos/Downloads/chromedriver",
# Adding adblocker extension
options = Options()
options.add_extension(
"/Users/marcosantos/Downloads/extension_1_37_2_0.crx"
)
# Opening the browser
driver = webdriver.Chrome(
executable_path="/Users/marcosantos/Downloads/chromedriver",
def vectBacktest(df, thres=.01, short=True):
"""
Accepts a dataframe of sentiment and log returns. Returns a series of the
portfolio performance from the backtest.
"""
# Getting the initial positions
df['positions'] = df['sentiment'].apply(lambda x: getPositions(x,
thres=thres,
short=short))
def getPositions(val, thres=.01, short=True):
"""
Provided a specific sentiment value, will return 1, 0, or -1 representing
buy, hold, or sell respectively. The threshold will determined how high or low the score
needs to be to trigger a buy or sell, otherwise it will be a hold.
"""
# Sentiment score threshold
thres = .01
def sentimentAndPrice(ticker, start, end, numtweets=20):
"""
Visually compares sentiment with the closing price of a given stock ticker.
"""
# Creating a DF that contains daily tweets between two dates
df = tweetByDay(start, end, pd.DataFrame(), search="$"+ticker, limit=numtweets)
# Analyzing the sentiment of each tweet
sent_df = getSentiment(
df,
def getStockPrices(ticker, start, end):
"""
Gets the historical daily prices between two dates. Applies the logarithmic function
to return a dataframe of log returns each day.
"""
# Setting the stock
stock = yf.Ticker(ticker)
# Getting historical prices
stock_df = stock.history(start=end, end=start, interval="1d")[['Close']]
def sentimentAndPrice(ticker, start, end, numtweets=20):
"""
Visually compares sentiment with the closing price of a given stock ticker.
"""
# Creating a DF that contains daily tweets between two dates
df = tweetByDay(start, end, pd.DataFrame(), search="$"+ticker, limit=numtweets)
# Analyzing the sentiment of each tweet
sent_df = getSentiment(
df,
def getStockPrices(ticker, start, end):
"""
Gets the historical daily prices between two dates. Scaling the prices based on a
given sentiment dataframe.
"""
# Setting the stock
stock = yf.Ticker(ticker)
# Getting historical prices
stock_df = stock.history(start=end, end=start, interval="1d")[['Close']]