-
-
Notifications
You must be signed in to change notification settings - Fork 22
Offset Sold Puts/Calls (Sourcery refactored) #147
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,10 +100,9 @@ def get_account( | |
| ) | ||
| except Exception: | ||
| logger.exception( | ||
| "Failed to get Account {}. Attempt #{}".format( | ||
| self.account_number, attempt | ||
| ) | ||
| f"Failed to get Account {self.account_number}. Attempt #{attempt}" | ||
| ) | ||
|
|
||
| if attempt == self.maxretries - 1: | ||
| return None | ||
|
|
||
|
|
@@ -128,9 +127,7 @@ def get_order( | |
| account=self.account_number, order_id=str(request.orderid) | ||
| ) | ||
| except Exception: | ||
| logger.exception( | ||
| "Failed to read order {}.".format(str(request.orderid)) | ||
| ) | ||
| logger.exception(f"Failed to read order {str(request.orderid)}.") | ||
|
Comment on lines
-131
to
+130
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if attempt == self.maxretries - 1: | ||
| return None | ||
|
|
||
|
|
@@ -161,7 +158,7 @@ def get_option_chain( | |
| optionchainobj.query_parameters = optionchainrequest | ||
|
|
||
| if not optionchainobj.validate_chain(): | ||
| logger.exception("Chain Validation Failed. {}".format(optionchainobj)) | ||
| logger.exception(f"Chain Validation Failed. {optionchainobj}") | ||
|
Comment on lines
-164
to
+161
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return None | ||
|
|
||
| for attempt in range(self.maxretries): | ||
|
|
@@ -172,9 +169,7 @@ def get_option_chain( | |
| raise BaseException("Option Chain Status Response = FAILED") | ||
|
|
||
| except Exception: | ||
| logger.exception( | ||
| "Failed to get Options Chain. Attempt #{}".format(attempt) | ||
| ) | ||
| logger.exception(f"Failed to get Options Chain. Attempt #{attempt}") | ||
| if attempt == self.maxretries - 1: | ||
| return None | ||
|
|
||
|
|
@@ -206,9 +201,7 @@ def get_quote( | |
| quotes = self.getsession().get_quotes(request.instruments) | ||
| break | ||
| except Exception: | ||
| logger.exception( | ||
| "Failed to get quotes. Attempt #{}".format(attempt), | ||
| ) | ||
| logger.exception(f"Failed to get quotes. Attempt #{attempt}") | ||
|
Comment on lines
-209
to
+204
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if attempt == self.maxretries - 1: | ||
| return None | ||
|
|
||
|
|
@@ -252,10 +245,9 @@ def get_market_hours( | |
| break | ||
| except Exception: | ||
| logger.exception( | ||
| "Failed to get market hours for {} on {}. Attempt #{}".format( | ||
| markets, request.datetime, attempt | ||
| ), | ||
| f"Failed to get market hours for {markets} on {request.datetime}. Attempt #{attempt}" | ||
| ) | ||
|
|
||
|
Comment on lines
-255
to
+250
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if attempt == self.maxretries - 1: | ||
| return None | ||
|
|
||
|
|
@@ -423,14 +415,14 @@ def place_order( | |
| response = baseRR.PlaceOrderResponseMessage() | ||
|
|
||
| # Log the Order | ||
| logger.info("Your order being placed is: {} ".format(orderrequest)) | ||
| logger.info(f"Your order being placed is: {orderrequest} ") | ||
|
|
||
| # Place the Order | ||
| try: | ||
| orderresponse = self.getsession().place_order( | ||
| account=self.account_number, order=orderrequest | ||
| ) | ||
| logger.info("Order {} Placed".format(orderresponse["order_id"])) | ||
| logger.info(f'Order {orderresponse["order_id"]} Placed') | ||
|
Comment on lines
-426
to
+425
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| except Exception: | ||
| logger.exception("Failed to place order.") | ||
| return None | ||
|
|
@@ -468,7 +460,7 @@ def cancel_order( | |
| order_id=str(request.orderid), | ||
| ) | ||
| except Exception: | ||
| logger.exception("Failed to cancel order {}.".format(str(request.orderid))) | ||
| logger.exception(f"Failed to cancel order {str(request.orderid)}.") | ||
|
Comment on lines
-471
to
+463
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return None | ||
|
|
||
| response = baseRR.CancelOrderResponseMessage() | ||
|
|
@@ -727,7 +719,7 @@ def translate_account_position_instrument( | |
| if strike_match is not None: | ||
| accountposition.strikeprice = float(strike_match.group()) | ||
| elif accountposition.assettype == "OPTION": | ||
| logger.error("No strike price found for {}".format(symbol)) | ||
| logger.error(f"No strike price found for {symbol}") | ||
|
Comment on lines
-730
to
+722
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Map the other fields | ||
| accountposition.description = instrument.get("description", str) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -492,8 +492,9 @@ def build_leg( | |
| def cancel_order(self, order_id: int): | ||
| # Build Request | ||
| cancelorderrequest = baseRR.CancelOrderRequestMessage( | ||
| self.strategy_id, int(order_id) | ||
| self.strategy_id, order_id | ||
| ) | ||
|
|
||
|
Comment on lines
-495
to
+497
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| # Send Request | ||
| self.mediator.cancel_order(cancelorderrequest) | ||
|
|
||
|
|
@@ -773,7 +774,7 @@ def get_best_strike_and_quantity( | |
| # Set Variables | ||
| best_strike = None | ||
| best_premium = float(0) | ||
| best_quantity = int(0) | ||
| best_quantity = 0 | ||
|
Comment on lines
-776
to
+777
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Calculate Risk Free Rate | ||
| risk_free_rate = helpers.get_risk_free_rate() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ def process_strategy(self): | |
|
|
||
| # Check if should be sleeping | ||
| if now < self.sleepuntil: | ||
| logger.debug("Markets Closed. Sleeping until {}".format(self.sleepuntil)) | ||
| logger.debug(f"Markets Closed. Sleeping until {self.sleepuntil}") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return | ||
|
|
||
| # Check market hours | ||
|
|
@@ -75,10 +75,9 @@ def process_strategy(self): | |
| if hours.start.day != now.day: | ||
| self.sleepuntil = hours.end - dt.timedelta(minutes=10) | ||
| logger.info( | ||
| "Markets are closed until {}. Sleeping until {}".format( | ||
| hours.start, self.sleepuntil | ||
| ) | ||
| f"Markets are closed until {hours.start}. Sleeping until {self.sleepuntil}" | ||
| ) | ||
|
|
||
| return | ||
|
|
||
| # If Pre-Market | ||
|
|
@@ -108,10 +107,7 @@ def process_pre_market(self): | |
| ) | ||
|
|
||
| logger.info( | ||
| "Markets are closed until {}. Sleeping until {}".format( | ||
| nextmarketsession.start, | ||
| self.sleepuntil, | ||
| ) | ||
| f"Markets are closed until {nextmarketsession.start}. Sleeping until {self.sleepuntil}" | ||
|
Comment on lines
-111
to
+110
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| ) | ||
|
|
||
| def process_open_market(self): | ||
|
|
@@ -485,18 +481,10 @@ def calculate_order_quantity( | |
|
|
||
| # Log Values | ||
| logger.info( | ||
| "Short Strike: {} Long Strike: {} BuyingPower: {} LiquidationValue: {} MaxLoss: {} BalanceToRisk: {} RemainingBalance: {} TradeSize: {} ".format( | ||
| shortstrike, | ||
| longstrike, | ||
| account_balance.buyingpower, | ||
| account_balance.liquidationvalue, | ||
| max_loss, | ||
| balance_to_risk, | ||
| remainingbalance, | ||
| trade_size, | ||
| ) | ||
| f"Short Strike: {shortstrike} Long Strike: {longstrike} BuyingPower: {account_balance.buyingpower} LiquidationValue: {account_balance.liquidationvalue} MaxLoss: {max_loss} BalanceToRisk: {balance_to_risk} RemainingBalance: {remainingbalance} TradeSize: {trade_size} " | ||
| ) | ||
|
|
||
|
Comment on lines
488
to
486
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| # Return quantity | ||
| return int(trade_size) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
TdaBroker.get_accountrefactored with the following changes:use-fstring-for-formatting)