From 0c338eeb4a0d1edfd2198d20e438f3b588a2e677 Mon Sep 17 00:00:00 2001 From: Dane Isburgh Date: Sun, 4 Feb 2024 09:13:55 -0500 Subject: [PATCH] refactor: update schwab product price variable names --- invaas/schwab/schwab_api/schwab.py | 6 ++--- invaas/schwab/schwab_task.py | 36 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/invaas/schwab/schwab_api/schwab.py b/invaas/schwab/schwab_api/schwab.py index 0d2b704..861f9aa 100644 --- a/invaas/schwab/schwab_api/schwab.py +++ b/invaas/schwab/schwab_api/schwab.py @@ -144,13 +144,13 @@ def trade( def buy_slice( self, tickers, - amount_usd, + price_usd, dry_run=True, valid_return_codes={20, 25}, ): """ tickers (List[str]) - The stock symbols you want to trade. - amount_usd (int) - The total dollar amount to buy (min $5/stock). + price_usd (int) - The total dollar price to buy (min $5/stock). Returns messages (list of strings), is_success (boolean) """ @@ -173,7 +173,7 @@ def buy_slice( data = { "AccountNo": self.schwab_account_id, - "TotalAmount": amount_usd, + "TotalAmount": price_usd, "IsAffirmed": True, "OrderBundleId": 0, "Stocks": stocks, diff --git a/invaas/schwab/schwab_task.py b/invaas/schwab/schwab_task.py index 68ce7d5..80c3871 100644 --- a/invaas/schwab/schwab_task.py +++ b/invaas/schwab/schwab_task.py @@ -18,10 +18,10 @@ def __init__(self, env: str): super().__init__(env=env) self.schwab_api = Schwab(schwab_account_id=self.get_secret("SCHWAB-ACCOUNT-ID")) - self.min_buy_amount = 10 - self.max_buy_amount = 200 - self.logger.info(f"Min buy amount: ${self.min_buy_amount}") - self.logger.info(f"Max buy amount: ${self.max_buy_amount}") + self.min_buy_price = 10 + self.max_buy_price = 200 + self.logger.info(f"Min buy price: ${self.min_buy_price}") + self.logger.info(f"Max buy price: ${self.max_buy_price}") async def setup_api(self): self.logger.info("Logging in to Schwab") @@ -113,17 +113,17 @@ def __buy_product(self, product_id: str, asset_class: str, quantity: float = Non if quantity is None and available_cash is None: raise Exception("Must include quantity or available cash to buy") elif quantity is None: - buy_amount = int(available_cash / 10) - buy_amount = buy_amount if buy_amount >= self.min_buy_amount else self.min_buy_amount - buy_amount = buy_amount if buy_amount <= self.max_buy_amount else self.max_buy_amount + buy_price = int(available_cash / 10) + buy_price = buy_price if buy_price >= self.min_buy_price else self.min_buy_price + buy_price = buy_price if buy_price <= self.max_buy_price else self.max_buy_price - if buy_amount >= available_cash: + if buy_price >= available_cash: self.logger.info(f"Not enough funds to buy {product_id}") return else: messages, success = self.schwab_api.buy_slice( tickers=[product_id], - amount_usd=buy_amount, + price_usd=buy_price, dry_run=(self.env == "local"), ) else: @@ -246,23 +246,22 @@ def get_sell_dte(owned_option, current_dte, expire_date, transaction_history): df_options_chain = self.__get_df_options_chain(ticker=ticker) owned_call_options, owned_put_options = self.__get_owned_options() - max_bought_options = 25 bought_options = 0 + max_buy_amount = 25 for index, row in df_options_chain.iterrows(): call_ask_price = row.C_ASK * 100 put_ask_price = row.P_ASK * 100 - current_max_buy_amount = available_cash / 10 + current_max_buy_price = available_cash / 10 if row.DTE > min_dte_buy and row.STRIKE_DISTANCE_PCT < max_strike_distance_pct: if ( good_call_buy and row.C_VOLUME > min_volume - and available_cash >= call_ask_price - and self.max_buy_amount >= call_ask_price - and current_max_buy_amount >= call_ask_price + and self.max_buy_price >= call_ask_price + and current_max_buy_price >= call_ask_price and len([x for x in owned_call_options if x["symbol"] == row.C_ID]) == 0 - and bought_options < max_bought_options + and bought_options < max_buy_amount ): self.logger.info(f"Buying {buy_sell_quantity} contracts of '{row.C_ID}' for ${call_ask_price:.2f}") self.__buy_product( @@ -276,11 +275,10 @@ def get_sell_dte(owned_option, current_dte, expire_date, transaction_history): elif ( good_put_buy and row.P_VOLUME > min_volume - and available_cash >= put_ask_price - and self.max_buy_amount >= put_ask_price - and current_max_buy_amount >= put_ask_price + and self.max_buy_price >= put_ask_price + and current_max_buy_price >= put_ask_price and len([x for x in owned_put_options if x["symbol"] == row.P_ID]) == 0 - and bought_options < max_bought_options + and bought_options < max_buy_amount ): self.logger.info(f"Buying {buy_sell_quantity} contracts of '{row.P_ID}' for ${put_ask_price:.2f}") self.__buy_product(