def run_stock_pipelines(stock_database: [str]): for ticker_symbol in stock_database: if ticker_symbol not in trading_constants.blacklist: try: trend_following_polarity = trend_following(ticker_symbol) ema_crossover_polarity = ema_crossover(ticker_symbol) console_output = "{0} price: {1} at {2}, strategy polarity: {3}\n".format( ticker_symbol, (yf_extender.get_stock_state( yf.Ticker(ticker_symbol))['Close'] * 1000) / 1000, datetime.now().strftime("%H:%M:%S"), trend_following_polarity + ema_crossover_polarity) print(console_output, end='') if (trend_following_polarity + ema_crossover_polarity) >= 0.5: stock_price = yf_extender.get_stock_state( yf.Ticker(ticker_symbol))['Close'] stock_quantity = math.floor( portfolio_manager.buying_power * trading_constants.max_investment_partition * (trend_following_polarity + ema_crossover_polarity) / stock_price) if stock_quantity > 0 and portfolio_manager.buying_power > ( stock_quantity * stock_price): portfolio_manager.buy_stock(ticker_symbol, stock_quantity) except IndexError: print(ticker_symbol + ": No Data")
def buy_stock(ticker_symbol: str, quantity: int): with lock: global buying_power json_simp.read_json() purchased_copy = dict(purchased) ticker = yf.Ticker(ticker_symbol) stock_info = yf_ext.get_stock_state(ticker) if ticker_symbol not in purchased_copy and buying_power > ( quantity * stock_info['Close']): api.submit_order(symbol=ticker_symbol, qty=quantity, side='buy', type='market', time_in_force='day') stock_info['Quantity'] = quantity purchased[ticker_symbol] = stock_info console_output = "Buying " + ticker_symbol + " Quantity: {0}".format( stock_info['Quantity']) + "\n" print(console_output, end=' ') buying_power -= (quantity * yf_ext.get_stock_state(ticker)['Close']) alerts.say_beep(1) json_simp.updated_purchased() json_simp.read_json()
def evaluate_purchased_stocks(): time.sleep(2) while True: purchased_copy = dict(portfolio_manager.purchased) for ticker_symbol in purchased_copy: ticker = yf.Ticker(ticker_symbol) stock_info = yf_extender.get_stock_state(ticker) price_change_since_purchase = stock_info['Close'] - purchased_copy[ ticker_symbol]['Close'] print( "Checking " + ticker_symbol + " | Direction: {0} | Price Change: {1} | Gains/Losses: {2}". format( yf_extender.get_direction(ticker), price_change_since_purchase, price_change_since_purchase * purchased_copy[ticker_symbol]['Quantity'])) if stock_info['Close'] < yf_extender.calculate_ema(ticker): print("Because stock price dropped below EMA line, " + ticker_symbol) portfolio_manager.sell_stock(ticker_symbol) break elif yf_extender.get_direction( ticker) < -0.001: #should depend on proportion print("Because direction is downward {0}".format( yf_extender.get_direction(ticker))) portfolio_manager.sell_stock(ticker_symbol) break time.sleep(0.1)
def refresh_account_balance(): with lock: global buying_power global account_value json_simp.read_json() buying_power = trading_constants.starting_account_value account_value = trading_constants.starting_account_value purchased_copy = dict(purchased) sold_copy = dict(sold) for ticker_symbol in purchased_copy: current_ticker_price = yf_ext.get_stock_state( yf.Ticker(ticker_symbol))['Close'] purchased_ticker_price = purchased_copy[ticker_symbol]['Close'] purchased_ticker_quantity = purchased_copy[ticker_symbol][ 'Quantity'] account_value += current_ticker_price - purchased_ticker_price buying_power -= purchased_ticker_price * purchased_ticker_quantity for ticker_symbol in sold_copy: temp = sold[ticker_symbol]['Close'] * abs( sold[ticker_symbol]['Quantity']) buying_power += temp account_value += temp
def trend_following(ticker_symbol: str): ticker = yf.Ticker(ticker_symbol) stock_info = yf_extender.get_stock_state(ticker) previous_2mo_high = yf_extender.previous_high(ticker, "2mo") if previous_2mo_high < stock_info[ 'Close'] and yf_extender.get_high2current_price_change_percent( ticker) > -0.0025: return 0.2 return 0.0
def sell_stock(ticker_symbol: str): api.close_position(ticker_symbol) global buying_power refresh_account_balance() sold_copy = dict(sold) ticker = yf.Ticker(ticker_symbol) stock_info = Counter(yf_ext.get_stock_state(ticker)) purchased_copy = dict(purchased) console_output = "Selling " + ticker_symbol + " Quantity: {0}".format( stock_info['Quantity']) + "\n" if ticker_symbol not in sold_copy and ticker_symbol != "": purchase_info = Counter(purchased.pop(ticker_symbol)) console_output = "Selling " + ticker_symbol + " Quantity: {0}".format( purchase_info['Quantity']) + "\n" stock_info.pop('Time') purchase_info.pop('Time') stock_info.subtract(purchase_info) stock_info['Time'] = datetime.now().strftime("%H:%M:%S") sold[ticker_symbol] = stock_info buying_power += stock_info['Close'] * abs(stock_info['Quantity']) elif ticker_symbol in purchased_copy: purchase_info = Counter(purchased.pop(ticker_symbol)) console_output = "Selling " + ticker_symbol + " Quantity: {0}".format( purchase_info['Quantity']) + "\n" sold_info = Counter(sold.pop(ticker_symbol)) stock_info.pop('Time') purchase_info.pop('Time') sold_info.pop('Time') stock_info.subtract(purchase_info) for i in stock_info and sold_info: stock_info[i] = stock_info[i] + sold_info[i] stock_info['Time'] = datetime.now().strftime("%H:%M:%S") sold[ticker_symbol] = stock_info buying_power += stock_info['Close'] * abs(stock_info['Quantity']) json_simp.updated_purchased() json_simp.updated_sold() json_simp.read_json() print(console_output, end=' ') alerts.say_beep(2)
def ema_crossover(ticker_symbol: str): ticker = yf.Ticker(ticker_symbol) stock_info = yf_extender.get_stock_state(ticker) stock_history = ticker.history(period="1d", interval="5m") previous_price = stock_history.iloc[len(stock_history) - 2].to_dict()['Close'] ticker_ema = yf_extender.calculate_ema(ticker) ticker_previous_ema = yf_extender.calculate_previous_ema(ticker) if stock_info[ 'Close'] > ticker_ema and previous_price < ticker_previous_ema and stock_info[ 'Close'] > 3: print( ticker_symbol + " previous price ema {0} previous price {1} current price ema {2} current price {3} direction {4}" .format(ticker_previous_ema, previous_price, ticker_ema, stock_info['Close'], yf_extender.get_direction(ticker))) return 0.5 return 0.0