Ejemplo n.º 1
0
def _parseTransactions(path: Path, lenient: bool = False) -> List[Activity]:
    with open(path, newline="") as csvfile:
        reader = csv.reader(csvfile)

        # Filter out header row, and invalid data
        rows = [
            _SchwabTransaction._make(r[0:-1]) for r in reader
            if len(r) > 1 and r[0] != "Date"
        ]

        inboundTransfers = [
            _forceParseSchwabTransaction(r, flags=TradeFlags.OPEN)
            for r in rows if r.action == "Security Transfer" and r.quantity
            and Decimal(r.quantity) > 0
        ]

        # Annoying hack because some transactions depend on others around them.
        rowsByDate = {
            d: list(t)
            for d, t in groupby(sorted(rows, key=lambda t: t.date),
                                key=lambda t: t.date)
        }

        activity = filter(
            None,
            parsetools.lenientParse(
                rows,
                transform=lambda t: _parseSchwabTransaction(
                    t, otherTransactionsThisDate=rowsByDate[t.date]),
                lenient=lenient,
            ),
        )
        return _fixUpShortSales(list(activity), inboundTransfers)
def _parsePositions(path: Path,
                    activity: List[Activity],
                    lenient: bool = False) -> List[Position]:
    with open(path, newline="") as csvfile:
        criterion = csvsectionslicer.CSVSectionCriterion(
            startSectionRowMatch=["Account Number"],
            endSectionRowMatch=[],
            rowFilter=lambda r: r[1:6],
        )
        sections = csvsectionslicer.parseSectionsForCSV(csvfile, [criterion])

        if len(sections) == 0:
            return []

        vanPositions = (_VanguardPosition._make(r) for r in sections[0].rows)
        vanPosAndBases = list(
            map(lambda pos: _VanguardPositionAndActivity(pos, activity),
                vanPositions))

        return list(
            parsetools.lenientParse(
                vanPosAndBases,
                transform=_parseVanguardPositionAndActivity,
                lenient=lenient,
            ))
Ejemplo n.º 3
0
def _tradesFromReport(report: IB.FlexReport, lenient: bool) -> List[Trade]:
    return list(
        parsetools.lenientParse(
            (_IBTradeConfirm(**t.__dict__)
             for t in report.extract("TradeConfirm", parseNumbers=False)),
            transform=_parseTradeConfirm,
            lenient=lenient,
        ))
Ejemplo n.º 4
0
def _parsePositions(path: Path, lenient: bool = False) -> Sequence[Position]:
    with open(path, newline="") as csvfile:
        reader = csv.reader(csvfile)

        return list(
            filter(
                None,
                parsetools.lenientParse(
                    _schwabPositionsFromRows(reader),
                    transform=_parseSchwabPosition,
                    lenient=lenient,
                ),
            ))
Ejemplo n.º 5
0
def _downloadBalance(ib: IB.IB, lenient: bool) -> AccountBalance:
    accountValues = (val for val in ib.accountSummary()
                     if val.account == "All" and val.tag == "TotalCashBalance"
                     and val.currency != "BASE")

    cashByCurrency: Dict[Currency, Cash] = {}

    for cash in parsetools.lenientParse(accountValues,
                                        transform=_extractCash,
                                        lenient=lenient):
        cashByCurrency[cash.currency] = (cashByCurrency.get(
            cash.currency, Cash(currency=cash.currency, quantity=Decimal(0))) +
                                         cash)

    return AccountBalance(cash=cashByCurrency)
Ejemplo n.º 6
0
def _parseActivityType(
    report: IB.FlexReport,
    name: str,
    t: Type[_NT],
    transform: Callable[[_NT], Optional[Activity]],
    lenient: bool,
) -> Iterable[Activity]:
    return filter(
        None,
        parsetools.lenientParse(
            (t(**x.__dict__)
             for x in report.extract(name, parseNumbers=False)),
            transform=transform,
            lenient=lenient,
        ),
    )
Ejemplo n.º 7
0
def _parseBalance(path: Path, lenient: bool = False) -> AccountBalance:
    with open(path, newline="") as csvfile:
        reader = csv.reader(csvfile)

        interestingKeys = {"Futures Cash", "Cash & Money Market"}

        keysAndCash = parsetools.lenientParse(
            _schwabPositionsFromRows(reader), transform=_parseCashValue, lenient=lenient
        )

        return AccountBalance(
            cash={
                Currency.USD: reduce(
                    operator.add,
                    (cash for key, cash in keysAndCash if key in interestingKeys),
                    Cash(currency=Currency.USD, quantity=Decimal(0)),
                )
            }
        )
Ejemplo n.º 8
0
def _parseBalance(path: Path, lenient: bool = False) -> AccountBalance:
    with open(path, newline="") as csvfile:
        reader = csv.reader(csvfile)

        fieldLen = len(_FidelityPosition._fields)
        positions = (_FidelityPosition._make(r[0:fieldLen]) for r in reader
                     if len(r) >= fieldLen)

        return AccountBalance(
            cash={
                Currency.USD:
                reduce(
                    operator.add,
                    parsetools.lenientParse(
                        (p for p in positions if p.symbol == "CASH"),
                        transform=_parseCash,
                        lenient=lenient,
                    ),
                    Cash(currency=Currency.USD, quantity=Decimal(0)),
                )
            })
def _parseTransactions(path: Path, lenient: bool = False) -> List[Activity]:
    with open(path, newline="") as csvfile:
        transactionsCriterion = csvsectionslicer.CSVSectionCriterion(
            startSectionRowMatch=["Account Number", "Trade Date"],
            endSectionRowMatch=[],
            rowFilter=lambda r: r[1:-1],
        )

        sections = csvsectionslicer.parseSectionsForCSV(
            csvfile, [transactionsCriterion])

        if len(sections) == 0:
            return []

        return list(
            filter(
                None,
                parsetools.lenientParse(
                    (_VanguardTransaction._make(r) for r in sections[0].rows),
                    transform=_parseVanguardTransaction,
                    lenient=lenient,
                ),
            ))
Ejemplo n.º 10
0
def _parseTransactions(path: Path, lenient: bool = False) -> List[Activity]:
    with open(path, newline="") as csvfile:
        transactionsCriterion = csvsectionslicer.CSVSectionCriterion(
            startSectionRowMatch=["Run Date", "Account", "Action"],
            endSectionRowMatch=[],
            rowFilter=lambda r: r if len(r) >= 17 else None,
        )

        sections = csvsectionslicer.parseSectionsForCSV(
            csvfile, [transactionsCriterion])

        if not sections:
            return []

        return list(
            filter(
                None,
                parsetools.lenientParse(
                    (_FidelityTransaction._make(r) for r in sections[0].rows),
                    transform=_parseFidelityTransaction,
                    lenient=lenient,
                ),
            ))
Ejemplo n.º 11
0
def _downloadPositions(ib: IB.IB, lenient: bool) -> List[Position]:
    return list(
        parsetools.lenientParse(ib.positions(),
                                transform=_extractPosition,
                                lenient=lenient))