Skip to content

Instantly share code, notes, and snippets.

View marcosan93's full-sized avatar

Marco Santos marcosan93

View GitHub Profile
def fbpPositions(pred_df, short=True):
"""
Gets positions based on the predictions and the actual values. This
is the logic of the trading strategy.
"""
if pred_df['open'] < pred_df['yhat_lower']:
return 1
elif pred_df['open'] > pred_df['yhat_upper'] and short:
return -1
else:
def runningFBP(prices, forecast_hours, training_hours):
"""
Runs Facebook Prophet to get predictions over a set period
of time. Uses FBP to train and predict every hour and gets the
price forecasts in the next N hours.
"""
# DF for the predicted values
pred_df = pd.DataFrame()
# Running the model each day
def fbpTrainPredict(df, forecast_hours):
"""
Uses FB Prophet and fits to a appropriately formatted DF. Makes a prediction N days into
the future based on given forecast period. Returns predicted values as a DF.
"""
# Setting up prophet
m = Prophet(
yearly_seasonality=False,
weekly_seasonality=False,
daily_seasonality=False
def getIntradayPrices(crypto, n_hours, training_hours, mov_avg):
"""
Gets crypto prices from now to N days ago and training amount will be in addition
to the number of days to train. (According to the EOD documentation: The maximum
periods between ‘from’ and ‘to’ are 120 days for 1-minute interval, 600 days
for 5-minute interval and 7200 days for 1 hour interval.)
There also appears to be a time delay on the data of about 15-20 hours, which
is added to the from(ago) variable.
"""
# Adding sentiment positions to the forecast DF
positions = pred_df.merge(
sent_df,
right_index=True,
left_index=True,
how='inner'
)
# Getting forecast prophet positions
positions['fbp_positions'] = positions.apply(
def runningFBP(prices, forecast_period, training_days):
"""
Runs Facebook Prophet to get predictions over a set period
of time. Uses FBP to train and predict every N days and gets the
price forecasts.
"""
# DF for the predicted values
pred_df = pd.DataFrame()
# Running the model each day
def getPrices(ticker, training_days, sent_df, mov_avg):
"""
Using a sentiment DataFrame to find the first day and trains with the previous
N days to make predictions that coincide with the sentiment date range.
In order to properly backtest with sentiment, a certain amount of training
days need to come before the initial date from the sentiment DF.
Also prepares the price data for FBProphet.
"""
def sentimentPositions(val, thresh=0.1, short=True):
"""
Returns position as 1, -1, or 0 for Buy, Sell,
and Do Nothing respectively based on the given
sentiment value and threshold.
"""
if val > thresh:
return 1
elif val < -thresh and short:
return -1
def getNews(ticker, days):
"""
Retrieves financial news over the course of a specified number of days for
a given stock ticker.
"""
# List of news
news = []
# How many days back to retrieve
def getNewsAndPrices(ticker, days):
"""
Retrieves financial news and price history over the course of a
specified number of days fora given stock ticker.
"""
# List of news
news = []
# How many days back to retrieve