Skip to content
This repository was archived by the owner on Nov 1, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions looptrader/basetypes/Broker/tdaBroker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)

Comment on lines -103 to +105
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.get_account refactored with the following changes:

if attempt == self.maxretries - 1:
return None

Expand All @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.get_order refactored with the following changes:

if attempt == self.maxretries - 1:
return None

Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.get_option_chain refactored with the following changes:

return None

for attempt in range(self.maxretries):
Expand All @@ -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

Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.get_quote refactored with the following changes:

if attempt == self.maxretries - 1:
return None

Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.get_market_hours refactored with the following changes:

if attempt == self.maxretries - 1:
return None

Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.place_order refactored with the following changes:

except Exception:
logger.exception("Failed to place order.")
return None
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.cancel_order refactored with the following changes:

return None

response = baseRR.CancelOrderResponseMessage()
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TdaBroker.translate_account_position_instrument refactored with the following changes:


# Map the other fields
accountposition.description = instrument.get("description", str)
Expand Down
5 changes: 3 additions & 2 deletions looptrader/basetypes/Strategy/singlebydeltastrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SingleByDeltaStrategy.cancel_order refactored with the following changes:

# Send Request
self.mediator.cancel_order(cancelorderrequest)

Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SingleByDeltaStrategy.get_best_strike_and_quantity refactored with the following changes:


# Calculate Risk Free Rate
risk_free_rate = helpers.get_risk_free_rate()
Expand Down
24 changes: 6 additions & 18 deletions looptrader/basetypes/Strategy/spreadsbydeltastrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpreadsByDeltaStrategy.process_strategy refactored with the following changes:

return

# Check market hours
Expand All @@ -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
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpreadsByDeltaStrategy.process_pre_market refactored with the following changes:

)

def process_open_market(self):
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SpreadsByDeltaStrategy.calculate_order_quantity refactored with the following changes:


# Return quantity
return int(trade_size)

Expand Down