def __init__(self): self.login_df= self.un = self.login_df['Username'].iloc[0] self.pw = self.login_df['Password'].iloc[0] self.my_portfolio = Portfolio() self.mt = ModelTools() self.L = Log()
class ModelTools: def __init__(self): self.model_loc = r'/home/jeb/TraderBotLogs/testmodel.dat' self.my_model = pickle.load(open(self.model_loc, 'rb')) self.FE = FeatureEngineerer() self.L = Log() def get_model_inputs(self, symbol, _date): """ Current features --> ['Volume', 'Dividends', 'Stock Splits', 'Pct_Change', 'Three_Day_Movement', 'Five_Day_Movement']""" if _date.weekday() == 0: _date = _date - timedelta(days=2) tckr = yf.Ticker(symbol) result = pd.DataFrame( tckr.history(start=_date - timedelta(days=1), end=_date)) if result.empty: print('No info for this day.') return pd.DataFrame() result = result.iloc[-1] running_result = pd.DataFrame( tckr.history(start=_date - timedelta(days=5), end=_date)) running_result['Date'] = running_result.index running_result['Date'] = running_result['Date'].dt.date model_inputs = pd.DataFrame() model_inputs['Date'] = [_date] try: model_inputs['Volume'] = [int(result['Volume'])] except: print('NaNs in inputs') return pd.DataFrame() model_inputs['Dividends'] = [int(result['Dividends'])] model_inputs['Stock Splits'] = [int(result['Stock Splits'])] model_inputs['Pct_Change'] = [self.FE.daily_pct_change(result)] model_inputs['Three_Day_Movement'] = [ self.FE.prior_trend(running_result, _date, 3) ] model_inputs['Five_Day_Movement'] = [ self.FE.prior_trend(running_result, _date, 5) ] return model_inputs def make_prediction(self, symbol, _date): """Returns True for whether or not a stock should be bought.""" inputs = self.get_model_inputs(symbol, _date) if not inputs.empty: inputs.drop('Date', inplace=True, axis=1) results = self.my_model.predict(inputs) if results == 1: return True else: return False self.L.add_line(f'No valid model inputs found for {symbol}', symbol) return False
def __init__(self): self.model_loc = r'/home/jeb/TraderBotLogs/testmodel.dat' self.my_model = pickle.load(open(self.model_loc, 'rb')) self.FE = FeatureEngineerer() self.L = Log()
class ATRaC: def __init__(self): self.login_df= self.un = self.login_df['Username'].iloc[0] self.pw = self.login_df['Password'].iloc[0] self.my_portfolio = Portfolio() self.mt = ModelTools() self.L = Log() def _login(self): chirp.login(self.un, self.pw) def amount_to_buy(self): """ Current Rules: 1. Never buy more than 10% of total accound value 2. If there isn't 10% of total available, spend roughly what is left. 3. If less than a dollar is left of buying power, return 0 """ buying_power = float(chirp.load_account_profile()['portfolio_cash']) if buying_power<1: return 0 total_equity = float(chirp.load_portfolio_profile()['equity']) ten_percent_of_portfolio = (buying_power+total_equity)*.1 if ten_percent_of_portfolio<buying_power: return ten_percent_of_portfolio else: return buying_power*.9 def buy_stock(self, symbol, value): value = self.amount_to_buy() if value==0: self.L.add_line(f'Not enough money to buy {symbol}, purchase not made.') else: chirp.order_buy_fractional_by_price(symbol, value) self.L.add_line('', symbol, 'BOUGHT', value) self.my_portfolio.add_to_portfolio(symbol) def sell_stock(self, symbol): """"Sell all of a stock """ amount_to_sell = self.get_equity(symbol) chirp.order_sell_fractional_by_price(symbol, amount_to_sell) self.L.add_line('', symbol, 'SOLD', amount_to_sell) def get_equity(self, symbol): self.update_portfolio() return float(self.my_portfolio[symbol]['equity']) def update_portfolio(self): self.my_portfolio = chirp.build_holdings() def stocks_to_sell(): port = self.my_portfolio.portfolio_contents() today = date.today() sell_me = [] if today.weekday() not in [5, 6]: for i in list(port['Symbol'].unique()): bought_date = port[port['Symbol'] == i]['Date'].iloc[0] if bought_date+timedelta(days=2)<today: sell_me.append(i) return sell_me