Created
January 1, 2022 21:42
-
-
Save marcosan93/0299729db06a4b94f7be87dece5d8e8b 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, 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. | |
| """ | |
| # The first day from the sentiment DF minus the amount of days to train with | |
| ago = datetime.strptime( | |
| sent_df.index[0], "%Y-%m-%d" | |
| ) - timedelta( | |
| days=training_days+mov_avg | |
| ) | |
| # Getting prices | |
| prices = pd.DataFrame( | |
| client.get_prices_eod( | |
| ticker+"-USD.CC", | |
| from_=ago.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