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 fitModels(new_df, tuning=False): | |
| """ | |
| From a list of models. Fit and trains them to the dataframe. | |
| Returns the fitted models. | |
| """ | |
| # Models | |
| models = { | |
| "adaboost":AdaBoostClassifier(random_state=11), | |
| "gradboost":GradientBoostingClassifier(random_state=11), | |
| "randomforest":RandomForestClassifier(random_state=11), |
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
| # Other models | |
| from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier | |
| from sklearn.neighbors import KNeighborsClassifier | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.naive_bayes import GaussianNB | |
| from sklearn.svm import SVC | |
| from sklearn.tree import DecisionTreeClassifier | |
| models = { | |
| "adaboost":AdaBoostClassifier(random_state=11), |
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 balanceDecisions(n_df): | |
| """ | |
| Rebalances data so that each class/decision is represented equally. | |
| """ | |
| # Counting each class or decision | |
| counts = n_df['decision'].value_counts() | |
| # The lowest number of represented classes | |
| low_num = counts.min() |
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 transformData(df, days=1): | |
| """ | |
| Transforming data into X variables for training. Uses percent change and | |
| multiplies the percentage by 100 rounded to 2 decimal places. | |
| """ | |
| # Transforming data | |
| new_df = df.pct_change( | |
| days | |
| ).apply( | |
| lambda x: round(x*100, 2) |
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 getMultipleFunds(tickers, api_token): | |
| """ | |
| Gets fundamental data from multiple stock tickers given as a list. Returns | |
| a large dataframe containing the concatenated information for all the given | |
| tickers. | |
| """ | |
| # Verifying if the list of tickers is compatible | |
| available = client.get_exchange_symbols("US") |
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 formatFundamentals(ticker, dropna=False): | |
| """ | |
| Formats the given ticker's fundamental and price data. Cleans up the data by dropping | |
| any empty/nan values if requested. | |
| """ | |
| # Getting fundamental data | |
| fund_data = getFundamentals(ticker) | |
| # Getting accompanying price data |
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(df, ticker): | |
| """ | |
| Gets the stock price at the time for each date in the financial statements for | |
| the given ticker and dataframe of financial information. | |
| """ | |
| # Getting stock price at the time | |
| prices = client.get_prices_eod(ticker, period='d') | |
| prices = pd.DataFrame(prices).set_index('date')[['adjusted_close', 'close', 'volume']] |
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 getFundamentals(ticker): | |
| """ | |
| Returns the fundamental data from the financial data API. Combines the quarterly balance | |
| sheet, cash flow, income statement, and earnings for a specific stock ticker. | |
| """ | |
| # Getting data | |
| fund_data = client.get_fundamental_equity(ticker) | |
| # Financials |
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
| # API Key | |
| with open("../eodHistoricalData-API.txt", "r") as f: | |
| api_key = f.read() | |
| # Backtest over 1 year and train on about 1 year's worth of data | |
| train_days = 365 | |
| range_of_bt = 365 | |
| # Usual Backtest |
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 fitModel(new_df): | |
| """ | |
| Fits model to new data and returns the fitted model to be used for predictions. | |
| """ | |
| # Column name | |
| col_name = new_df.columns[-1] | |
| # Variables | |
| X = new_df.drop(col_name, axis=1) |