def maybe_invest(api: tradeapi.REST) -> None: target_symbols = ['AAPL', 'VOO'] user_context = UserContext() user_context.set_account(api.get_account()) stock_context_list = [ StockContext(target_symbol) for target_symbol in target_symbols ] # Populate stock context list for potential investment. for stock_context in stock_context_list: last_trade = api.get_last_trade(stock_context.symbol) stock_context.set_last_trade(last_trade) # Filter out a list of stocks that should be considered # for investment at the moment. stock_feasibility_checker = StockFeasibilityChecker(user_context) filtered_stock_context_list = filter(stock_feasibility_checker, stock_context_list) # Rank the potential list of stocks to invest based on # the potential growth. stock_comparator = StockComparator(user_context) ranked_stock_context_list = sorted(filtered_stock_context_list, key=stock_comparator) # Send order to invest if fund is sufficient stock_action_executor = StockActionExecutor(user_context) for ranked_stock_context in ranked_stock_context_list: stock_action_executor(ranked_stock_context)
if open_orders: for order in open_orders: qty = order.qty symbol = order.symbol side = order.side try: print( f'Cancelling {side} order for {qty} shares of {symbol}' ) api.cancel_order(order.id) # submit new market order print( f'Resubmitting {side} order for {qty} shares of {symbol}' ) api.get_last_trade(symbol) api.submit_order(symbol=symbol, qty=str(qty), side=side, type='market', time_in_force='day') except Exception as e: print(f'Error with {symbol} order:', e) discord_webhook.send_error( f'Error trying to fill {symbol}:', e) else: complete = True # Send trade alert discord_webhook._send_messsage(alert)