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 backtestStockAndPredict(stock, n_days_lst, training_days_lst, mov_avg_lst, | |
forecast_period_lst, fbp_intervals,stop_early=True, visualize=True): | |
# Printing the stock | |
print(f"\nBacktesting {stock}. . .") | |
# Tuning parameters for stock | |
results = parameterTuning( | |
stock, | |
n_days_lst, |
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 parameterTuning(stock, n_days_lst, training_days_lst, mov_avg_lst, | |
forecast_period_lst, fbp_intervals, stop_early=True): | |
""" | |
Given a list of parameters for a specific stock. Iterates through | |
different combination of parameters until a successful backtest | |
performance is found. | |
Optional stop_early variable for stopping tuning immediately | |
when a positive backtest result is found | |
""" |
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 backtestStock(stock, pred_df, prices, price_history, interval_width): | |
# Adding positions to the forecast DF | |
positions = pred_df | |
# Getting forecast prophet positions | |
positions['fbp_positions'] = positions.apply( | |
lambda x: fbpPositions(x, short=True), | |
axis=1 | |
) |
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 riskAnalysis(performance, prices, price_history, interval_width): | |
""" | |
Analyzes the performance DataFrame to calculate various | |
evaluation metrics on the backtest to determine if | |
the backtest performance was favorable. | |
""" | |
### Hypothesis testing average returns | |
# Weekly returns for fb prophet | |
rets = performance['fbp_positions'].pct_change(5).dropna() |
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 getStockPrices(stock, n_days, training_days, mov_avg): | |
""" | |
Gets stock prices from now to N days ago and training amount will be in addition | |
to the number of days to train. | |
""" | |
# Designating the Ticker | |
ticker = yf.Ticker(stock) | |
# Getting all price history |
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 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 |
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 fbpTrainPredict(df, forecast_period): | |
""" | |
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( | |
daily_seasonality=True, | |
yearly_seasonality=True, | |
weekly_seasonality=True |
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. | |
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 getTweets(search_term, until, since, limit=20): | |
""" | |
Configures Twint and returns a dataframe of tweets for a specific day. | |
""" | |
# Configuring Twint for search | |
c = twint.Config() | |
# The limit of tweets to retrieve | |
c.Limit = limit |
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 backtestPerformanceVis(ticker, n_hours, training_hours, mov_avg, forecast_hours): | |
""" | |
Consolidates the previous functions that support the backtesting process. | |
""" | |
# Getting Price data | |
print("Getting price data...") | |
prices = getIntradayPrices( | |
crypto=ticker, | |
n_hours=n_hours, | |
training_hours=training_hours, |
NewerOlder