def __init__(self, r_f, index="^GSPC", from_year="2000", from_month="00", from_day="1", to_year=None, to_month=None, to_day=None, frequency="d"): if type(index) == str: if index not in ["^GSPC", "^DJI", "^IXIC", "^NYA", "^GSPTSE", "^STOXX50E", "^FTSE", "^GDAXI", "^FCHI", "^IBEX", "^N225", "IJIIX", "^HSI", "000300.SS", "^AXJO"]: print("The index provided is not in the list of supported indexes - take extra care.") to_year = date.today().strftime("%Y") if to_year is None else to_year to_month = str(int(date.today().strftime("%m"))-1) if to_month is None else to_month to_day = date.today().strftime("%d") if to_day is None else to_day df = yahoo_finance.get_stock_price_dataframe(index, from_month, from_day, from_year, to_month, to_day, to_year, frequency) df = df[["Close"]] df.columns = ["market_portfolio"] if type(index) == pd.core.frame.DataFrame: df = index if np.isscalar(r_f): df["risk_free"] = pd.Series([r_f]*df.index.size, df.index) else: df["risk_free"] = pd.Series(r_f, df.index) Portfolio.__init__(self, df)
def __init__(self): # format of links to the txt files self.url_base = "http://web.mta.info/developers/data/nyct/turnstile/turnstile_{0}.txt" # first day of data self.begining_of_time = datetime(2010, 5, 1) # date when format of data changed self.new_era = datetime(2014, 10, 18) self.today = datetime.today() # prepare station df for old format data self.data_dir = "static/data/" station_df_path = os.path.join(self.data_dir, "station.pkl") if os.path.isfile(station_df_path): with open(station_df_path) as f: self.station_df = pickle.load(f) else: self.station_df = pd.read_excel( "http://web.mta.info/developers/resources/nyct/turnstile/Remote-Booth-Station.xls" ) self.station_df.columns = [ "UNIT", "C/A", "STATION", "LINENAME", "DIVISION" ] # save to data directory if not os.path.exists(self.data_dir): os.makedirs(self.data_dir) with open(station_df_path, "wb") as f: pickle.dump(self.station_df, f)
def buy(self, cash, date=datetime.today().strftime("%Y-%m-%d")): stock_count = cash // market.open_prices.ix[date] commission = self.commission(stock_count, date) total_cost = stock_count * market.open_prices.ix[date] * (1 + commission) if total_cost > cash: stock_count -= (total_cost - cash) // market.open_prices.ix[date] + 1 stock_count = max(0, stock_count) total_cost = stock_count * market.open_prices.ix[date] * ( 1 + commission) if stock_count > 0: self.buy_count += 1 return stock_count, total_cost
def perform_empirical_analysis(ticker, freq, polarities): yf.download_from_yahoo_finance([ticker], "2020-12-07", datetime.today().strftime('%Y-%m-%d')) file_name = ticker + "_" + freq + ".csv" dataset = pd.read_csv(file_name) if type(polarities) == str: stocks = helper.itemize_emp_data(dataset) else: stocks = helper.itemize_pol_data(dataset, polarities) stocks = helper.get_normalised_data(stocks) print(stocks.head()) print("\n") print("Open --- mean :", np.mean(stocks['Open']), " \t Std: ", np.std(stocks['Open']), " \t Max: ", np.max(stocks['Open']), " \t Min: ", np.min(stocks['Open'])) print("Close --- mean :", np.mean(stocks['Close']), " \t Std: ", np.std(stocks['Close']), " \t Max: ", np.max(stocks['Close']), " \t Min: ", np.min(stocks['Close'])) print("Volume --- mean :", np.mean(stocks['Volume']), " \t Std: ", np.std(stocks['Volume']), " \t Max: ", np.max(stocks['Volume']), " \t Min: ", np.min(stocks['Volume'])) if type(polarities) != str: print("Polarity --- mean :", np.mean(stocks['Polarity']), " \t Std: ", np.std(stocks['Polarity']), " \t Max: ", np.max(stocks['Polarity']), " \t Min: ", np.min(stocks['Polarity'])) # Print the dataframe head and tail print(stocks.head()) print("---") print(stocks.tail()) return stocks
# evaluate an ARIMA model using a walk-forward validation from pandas import read_csv from pandas import datetime from matplotlib import pyplot from statsmodels.tsa.arima.model import ARIMA from sklearn.metrics import mean_squared_error from math import sqrt import pandas_datareader.data as pdr #Load Bitcoin price from yahoo finance end = datetime.today() start = datetime(end.year - 5, end.month, end.day) BTC = pdr.DataReader('BTC-USD', 'yahoo', start, end) #BTC.to_csv('BTC_USD.csv',index=False) bt = BTC['Close'].resample('W').last() # split into train and test sets X = series.values size = int(len(X) * 0.7) train, test = X[0:size], X[size:len(X)] history = [x for x in train] predictions = list() # walk-forward validation for t in range(len(test)): model = ARIMA(history, order=(5, 1, 0)) model_fit = model.fit() output = model_fit.forecast() yhat = output[0] predictions.append(yhat) obs = test[t]
def idDate(): data = read_csv('crmclientrecords.csv', encoding='utf-8') data['id'] = range(len(data)) data['as_of'] = datetime.today() data.to_csv('crmclientrecords1.csv')
def sell(self, stock_count, date=datetime.today().strftime("%Y-%m-%d")): commission = self.commission(stock_count, date) self.sell_count += 1 return stock_count * market.open_prices.ix[date] * (1 - commission)