Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _set_broker_commission(self, broker_commission):
"""
Check and set the BrokerCommission instance for
the broker. The class default is no commission.
"""
if broker_commission is None:
return ZeroBrokerCommission()
else:
if (
hasattr(broker_commission, "__class__") and
hasattr(broker_commission.__class__, "__name__") and
issubclass(broker_commission.__class__, BrokerCommission)
):
return broker_commission
else:
raise BrokerException(
"Provided broker commission is not a "
"BrokerCommission subclass, so could not "
def _set_initial_funds(self, initial_funds):
"""
Check and set the initial funds for the broker
master account. Raise BrokerException if the
amount is negative.
"""
if initial_funds < 0.0:
raise BrokerException(
"Could not create the SimulatedBroker entity as the "
"provided initial funds of '%s' were "
"negative." % initial_funds
)
else:
return initial_funds
def withdraw_funds_from_account(self, amount):
"""
Withdraws an amount of cash in the base currency
from the broker master cash account, assuming an
amount equal to or more cash is present. If less
cash is present, a BrokerException is raised.
"""
if amount < 0:
raise BrokerException(
"Cannot debit negative amount: "
"'%s' from the broker account." % amount
)
if amount > self.cash_balances[self.base_currency]:
raise BrokerException(
"Not enough cash in the broker account to "
"withdraw. %0.2f withdrawal request exceeds "
"current broker account cash balance of %0.2f." % (
amount, self.cash_balances[self.base_currency]
)
)
self.cash_balances[self.base_currency] -= amount
def subscribe_funds_to_portfolio(self, portfolio_id, amount):
"""
Subscribe funds to a particular sub-portfolio, assuming
it exists and the cash amount is positive. Otherwise raise
a BrokerException.
"""
if amount < 0.0:
raise BrokerException(
"Cannot add negative amount: "
"%0.2f to a portfolio account." % amount
)
if portfolio_id not in self.portfolios.keys():
raise BrokerException(
"Portfolio with ID '%s' does not exist. Cannot subscribe "
"funds to a non-existent portfolio." % portfolio_id
)
if amount > self.cash_balances[self.base_currency]:
raise BrokerException(
"Not enough cash in the broker master account to "
"fund portfolio '%s'. %0.2f subscription amount exceeds "
"current broker account cash balance of %0.2f." % (
portfolio_id, amount,
self.cash_balances[self.base_currency]
)
)
self.portfolios[portfolio_id].subscribe_funds(self.cur_dt, amount)
self.cash_balances[self.base_currency] -= amount
def withdraw_funds_from_portfolio(self, portfolio_id, amount):
"""
Withdraw funds from a particular sub-portfolio, assuming
it exists, the cash amount is positive and there is
sufficient remaining cash in the sub-portfolio to
withdraw. Otherwise raise a BrokerException.
"""
if amount < 0.0:
raise BrokerException(
"Cannot withdraw negative amount: "
"%0.2f from a portfolio account." % amount
)
if portfolio_id not in self.portfolios.keys():
raise BrokerException(
"Portfolio with ID '%s' does not exist. Cannot "
"withdraw funds from a non-existent "
"portfolio. " % portfolio_id
)
if amount > self.portfolios[portfolio_id].total_cash:
raise BrokerException(
"Not enough cash in portfolio '%s' to withdraw "
"into brokerage master account. Withdrawal "
"amount %0.2f exceeds current portfolio cash "
"balance of %0.2f." % (
portfolio_id, amount,
def _set_base_currency(self, base_currency):
"""
Check and set the base currency from a list of
allowed currencies. Raise BrokerException if the
currency is currently not supported by QSTrader.
"""
if base_currency not in settings.CURRENCIES:
raise BrokerException(
"Currency '%s' is not supported by QSTrader. Could not "
"set the base currency in the SimulatedBroker "
"entity." % base_currency
)
else:
return base_currency
def submit_order(self, portfolio_id, order):
"""
Execute an Order instance against the sub-portfolio
with ID 'portfolio_id'. For the SimulatedBroker class
specifically there are no restrictions on this occuring
beyond having sufficient cash in the sub-portfolio to
allow this to occur.
This does not take into settlement dates, as with most
brokerage accounts. The cash is taken immediately upon
entering a long position and returned immediately upon
closing out the position.
"""
# Check that the portfolio actually exists
if portfolio_id not in self.portfolios.keys():
raise BrokerException(
"Portfolio with ID '%s' does not exist. Order with "
"ID '%s' was not executed." % (
portfolio_id, order.order_id
)
)
# Obtain a price for the asset, if no price then
# raise a BrokerException
price_err_msg = "Could not obtain a latest market price for " \
"Asset with ticker symbol '%s'. Order with ID '%s' was " \
"not executed." % (
order.asset.symbol, order.order_id
)
bid_ask = self.get_latest_asset_price(order.asset)
if bid_ask == (np.NaN, np.NaN):
raise BrokerException(price_err_msg)
def get_portfolio_as_dict(self, portfolio_id):
"""
Return a particular portfolio with ID 'portolio_id' as
a dictionary with Asset objects as keys, with various
attributes as sub-dictionaries. This includes 'quantity',
'book_cost', 'market_value', 'gain' and 'perc_gain'.
"""
if portfolio_id not in self.portfolios.keys():
raise BrokerException(
"Cannot return portfolio as dictionary since "
"portfolio with ID '%s' does not exist." % portfolio_id
)
return self.portfolios[portfolio_id].portfolio_to_dict()