Last active
February 25, 2022 02:09
-
-
Save marcosan93/26d57937f2edbffc06512372eac4956e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getPrices(ticker, training_days, tweet_df, mov_avg, forecast_period): | |
""" | |
Using a tweet DataFrame to find the first day and training data from 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 tweet DF as well | |
as the moving average and forecast period in order to accurately line up | |
with the tweet DF. | |
Also prepares the price data for FBProphet forecasting. | |
""" | |
# The first day from the tweet DF minus the amount of days to train with, moving avg, and forecast period | |
ago = datetime.strptime( | |
tweet_df.index[0], | |
"%Y-%m-%d" | |
) - timedelta( | |
days=training_days+mov_avg+forecast_period | |
) | |
# The last day of the tweet DF | |
now = datetime.strptime( | |
tweet_df.index[-1], | |
"%Y-%m-%d" | |
) | |
# Getting prices | |
prices = pd.DataFrame( | |
client.get_prices_eod( | |
ticker+"-USD.CC", | |
from_=ago.strftime("%Y-%m-%d"), | |
to=now.strftime("%Y-%m-%d") | |
) | |
) | |
# Set index | |
prices = prices.set_index('date', drop=True) | |
# Getting the N Day Moving Average and rounding the values for some light data preprocessing | |
prices['MA'] = prices[['open']].rolling( | |
window=mov_avg | |
).mean().apply(lambda x: round(x, 2)) | |
# Dropping Nans | |
prices.dropna(inplace=True) | |
# Resetting format for FBP | |
prices = prices.reset_index().rename( | |
columns={"date": "ds", "MA": "y"} | |
) | |
return prices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment