def on_buy_fill(self, trade): try: fill = trade.fills[0] except Exception as e: log_exception(e, "on_buy_fill") return self.schedule_stop_order_task(fill.contract.right)
async def fetch_and_report(): try: self.set_nope_value() except Exception as e: log_exception(e, "set_nope_value") curr_date, curr_dt = get_datetime_for_logging() with open(f"logs/{curr_date}.txt", "a") as f: f.write(f'NOPE @ {self._nope_value} | Stock Price @ {self._underlying_price} | {curr_dt}\n')
def check_acc_balance(self, price, quantity): ib_account = self.config["ib"]["account"] if not ib_account: return True acc_values = self.ib.accountValues(account=ib_account) buy_power = list( filter(lambda a: a.tag == "BuyingPower" and a.currency == "USD", acc_values)) try: balance = buy_power[0] except Exception as e: log_exception(e, "check_acc_balance") return False return float(balance.value) > price * 100 * quantity
async def cancel_unfilled_orders(): cancellable_statuses = ["PreSubmitted", "Submitted"] trades = self.get_trades() filtered = list( filter( lambda t: t.order.orderType != "STP", trades, )) for trade in filtered: submit_logs = list( filter(lambda l: l.status in cancellable_statuses, trade.log)) try: submit_log = submit_logs[0] except Exception as e: log_exception(e, "cancel_unfilled_orders") return diff = get_datetime_diff_from_now(submit_log.time) if diff > cancel_after: self.ib.cancelOrder(trade.order) self.console_log("Cancelled unfilled order")
async def refresh_token(): try: self.qt.refresh_access_token() except Exception as e: log_exception(e, "refresh_token")
async def exit_pos(): try: self.exit_positions() except Exception as e: log_exception(e, "exit_positions")
async def enter_pos(): try: self.enter_positions() except Exception as e: log_exception(e, "enter_positions")
async def schedule_stop_loss(): try: self.set_stop_loss(right) except Exception as e: log_exception(e, "schedule_stop_order_task")