net = gains + losses return gains, losses, net, wins, defeats #::: Driver ::: p = Printer() t = Robinhood() t.login() p.format('\n[bold:purple]Robinhood Account Info:', log=True) p.chevron(f'User: {t.username}\n') account_info = t.get_account() margin = account_info['margin_balances'] transfers = t.consume_pages(t.get_transfers()) options = t.consume_pages(t.get_options_orders()) orders = t.consume_pages(t.order_history()) withdraws, deposits = get_transfers(transfers) gains, losses, net, wins, defeats = get_trades(options) buy_power = float(margin['day_trade_buying_power']).round(2) av_withdraw = float(margin['cash_available_for_withdrawal']).round(2) unsettled = float(margin['unsettled_funds']).round(2) print('') p.arrow(p.format(f"[bold:green]Buying Power: [reset]${buy_power}", ret=True)) p.arrow( p.format(f"[bold:purple]Available for Withdrawal: [reset]${av_withdraw}", ret=True))
def get_all_transfers(trader: Robinhood) -> List[Transfer]: all_transfers: List[Transfer] = [] # These are transfers that were initiated from within RH to an outside acc transfers = trader.get_transfers()["results"] for transfer in transfers: sign = 1 if transfer["direction"] == "deposit" else -1 all_transfers.append( Transfer( amount=float(transfer["amount"]) * sign, date=transfer["created_at"], transfer_type=Transfer.TransferType.internal_transfers)) # This is money from interest sweeps = trader.get_sweeps()["results"] for sweep in sweeps: assert sweep["amount"]["currency_code"] == "USD" amount = get_signed_amount( sweep["amount"]["amount"], key=sweep["direction"], pos="credit", neg="debit") all_transfers.append( Transfer( amount=amount, date=sweep["pay_date"], transfer_type=Transfer.TransferType.interest)) # These are transfers that were initiated from an outside account to RH received_transfers = trader.get_received_transfers()["results"] for transfer in received_transfers: assert transfer["amount"]["currency_code"] == "USD" amount = get_signed_amount( transfer["amount"]["amount"], key=transfer["direction"], pos="credit", neg="debit") all_transfers.append( Transfer( amount=amount, date=transfer["created_at"], transfer_type=Transfer.TransferType.received_transfers)) # These are transfers initiated by the RH Cash debit card settled_transactions = trader.get_settled_transactions()["results"] for transaction in settled_transactions: if transaction["source_type"] != "settled_card_transaction": continue assert transaction["amount"]["currency_code"] == "USD" amount = get_signed_amount(amount=transaction["amount"]["amount"], key=transaction["direction"], pos="credit", neg="debit") all_transfers.append( Transfer( amount=amount, date=transaction["initiated_at"], transfer_type=Transfer.TransferType.settled_transactions)) # Update internal stock purchases and sales order_history = trader.order_history()["results"] for order in order_history: if not order["state"] == "filled" or len(order["executions"]) == 0: logging.info("Ignoring order because it is not marked 'filled'." f" Order {order}") continue symbol = trader.get_url(order["instrument"])["symbol"] amount = 0 if order["dollar_based_amount"] is not None: logging.info("Using dollar based amount") amount = order["dollar_based_amount"]["amount"] else: logging.info("Evaluating amount from executions") for execution in order["executions"]: price = float(execution["price"]) quantity = float(execution["quantity"]) amount += price * quantity amount = get_signed_amount( amount, key=order["side"], pos="sell", neg="buy") memo = f"Robinhood {symbol} {'Purchased' if amount < 0 else 'Sold'}" all_transfers.append( Transfer( amount=amount, date=order["last_transaction_at"], transfer_type=Transfer.TransferType.stock_purchase, memo=memo)) # Update Crypto purchases cryptos = trader.crypto_orders()["results"] for crypto in cryptos: amount = get_signed_amount( amount=crypto["rounded_executed_notional"], key=crypto["side"], pos="sell", neg="buy") memo = f"Robinhood Crypto {'Purchased' if amount < 0 else 'Sold'}" all_transfers.append( Transfer( amount=amount, date=crypto["last_transaction_at"], transfer_type=Transfer.TransferType.stock_purchase, memo=memo)) # Update dividend payouts dividends = trader.dividends()["results"] for dividend in dividends: # Make sure the divident actually went through (could be voided/pending) if dividend["state"] != "paid": continue symbol = trader.get_url(dividend["instrument"])["symbol"] all_transfers.append( Transfer( amount=dividend["amount"], memo=f"Dividend from {symbol}", date=dividend["paid_at"], transfer_type=Transfer.TransferType.dividend)) all_transfers.sort(key=lambda t: t.date) return all_transfers