Example #1
0
def sync(ledger, accounts, args):
    sync = OfxSynchronizer(ledger, shortenaccount=args.shortenaccount)
    for acct in accounts:
        try:
            (ofx, txns) = sync.get_new_txns(acct,
                                            resync=args.resync,
                                            max_days=args.max)
            if ofx is not None:
                converter = make_ofx_converter(
                    account=ofx.account,
                    name=acct.description,
                    ledger=ledger,
                    indent=args.indent,
                    fid=None,
                    unknownaccount=args.unknownaccount,
                    payee_format=args.payee_format,
                    hardcodeaccount=None,
                    shortenaccount=args.shortenaccount,
                    security_list=SecurityList(ofx),
                    date_format=args.date_format,
                    infer_account=args.infer_account)
                print_results(converter, ofx, ledger, txns, args)
        except KeyboardInterrupt:
            raise
        except BaseException:
            sys.stderr.write("Caught exception processing %s\n" %
                             (acct.description))
            traceback.print_exc(file=sys.stderr)
Example #2
0
def import_ofx(ledger, args):
    sync = OfxSynchronizer(ledger,
                           hardcodeaccount=args.hardcodeaccount,
                           shortenaccount=args.shortenaccount)
    ofx = OfxSynchronizer.parse_file(args.PATH)
    txns = sync.filter(ofx.account.statement.transactions,
                       ofx.account.account_id)
    accountname = args.account
    if accountname is None:
        if ofx.account.institution is not None:
            accountname = "%s:%s" % (ofx.account.institution.organization,
                                     ofx.account.account_id)
        else:
            accountname = UNKNOWN_BANK_ACCOUNT

    # build SecurityList (including indexing by CUSIP and ticker symbol)
    security_list = SecurityList(ofx)

    converter = make_ofx_converter(account=ofx.account,
                                   name=accountname,
                                   ledger=ledger,
                                   indent=args.indent,
                                   fid=args.fid,
                                   unknownaccount=args.unknownaccount,
                                   payee_format=args.payee_format,
                                   hardcodeaccount=args.hardcodeaccount,
                                   shortenaccount=args.shortenaccount,
                                   security_list=security_list,
                                   date_format=args.date_format,
                                   infer_account=args.infer_account)
    print_results(converter, ofx, ledger, txns, args)
def test_fee(ofx):
    """Test that fees are parsed correctly.

    In this case we have a 7-cent fee. We need to make sure that
    the net sale price which shows up is the gross price of 3239.44
    minus 7 cents which equals 3239.37 and that the 7 cent fee
    shows up as an extra posting.
    """
    converter = OfxConverter(account=ofx.account,
                             name="Foo",
                             security_list=SecurityList(ofx))
    # test fee
    assert (make_clean_posting(
        converter, ofx.account.statement.transactions[1]) == """2012/08/01 SELL
  Foo  -100.0 "929042109" @ $32.3944
  ; ofxid: 7776.01234567890.0123456789021401420120801
  Assets:Unknown  $3239.37
  Expenses:Fees  $0.07
""")
    # test fee and comission
    assert (make_clean_posting(
        converter,
        ofx.account.statement.transactions[0]) == """2020/05/22=2020/05/26 SELL
  Foo  -1.0 "Z9977810Z" @ $8.27
  ; ofxid: 7776.01234567890.987654321
  Assets:Unknown  $8.25
  Expenses:Fees  $0.02
  Expenses:Commission  $1.00
""")
    def test_fee(self):
        """Test that fees are parsed correctly.

In this case we have a 7-cent fee. We need to make sure that
the net sale price which shows up is the gross price of 3239.44
minus 7 cents which equals 3239.37 and that the 7 cent fee
shows up as an extra posting.
        """
        with open(os.path.join('fixtures', 'fidelity_fee.ofx'),
                  'rb') as ofx_file:
            ofx = OfxParser.parse(ofx_file)
        converter = OfxConverter(account=ofx.account,
                                 name="Foo",
                                 security_list=SecurityList(ofx))
        # test fee
        self.assertEqualLedgerPosting(
            converter.convert(ofx.account.statement.transactions[1]).format(),
            """2012/08/01 SELL
    Foo                                        -100.0 "929042109" @ $32.3944
    ; ofxid: 7776.01234567890.0123456789021401420120801
    Assets:Unknown                                       $3239.37
    Expenses:Fees                                           $0.07
""")
        # test fee and comission
        self.assertEqualLedgerPosting(
            converter.convert(ofx.account.statement.transactions[0]).format(),
            """2020/05/22=2020/05/26 SELL
  Foo  -1.0 "Z9977810Z" @ $8.27
  ; ofxid: 7776.01234567890.987654321
  Assets:Unknown  $8.25
  Expenses:Fees  $0.02
  Expenses:Commission  $1.00
""")
    def test_position(self):
        ofx = OfxParser.parse(open(os.path.join('fixtures', 'cusip.ofx')))
        converter = OfxConverter(account=ofx.account, name="Foo", indent=4,
                                 unknownaccount='Expenses:Unknown',
                                 security_list=SecurityList(ofx))
        self.assertEqual(
            converter.format_position(
                ofx.account.statement.positions[0]),
            """P 2016/10/08 07:30:08 SHSAX 47.8600000
""")
def test_position(ofx):
    converter = OfxConverter(
        account=ofx.account,
        name="Foo",
        indent=4,
        unknownaccount="Expenses:Unknown",
        security_list=SecurityList(ofx),
    )
    assert (converter.format_position(ofx.account.statement.positions[0]) ==
            """P 2016/10/08 07:30:08 SHSAX 47.8600000
""")
def test_quote_commodity(ofx):
    converter = OfxConverter(account=ofx.account,
                             name="Foo",
                             security_list=SecurityList(ofx))
    assert (make_clean_posting(
        converter,
        ofx.account.statement.transactions[0]) == """2012/07/20 YOU BOUGHT
  Foo  100.00000 INTC @ $25.635000000
  ; ofxid: 7776.01234567890.0123456789020201120120720
  Assets:Unknown  -$2563.50
  Expenses:Commission  $7.95
""")
Example #8
0
    def test_quote_commodity(self):
        with open(os.path.join('fixtures', 'fidelity.ofx'), 'rb') as ofx_file:
            ofx = OfxParser.parse(ofx_file)
        converter = OfxConverter(account=ofx.account,
                                 name="Foo",
                                 security_list=SecurityList(ofx))
        self.assertEqualLedgerPosting(
            converter.convert(ofx.account.statement.transactions[0]).format(),
            """2012/07/20 YOU BOUGHT
  Foo  100.00000 INTC @ $25.635000000
  ; ofxid: 7776.01234567890.0123456789020201120120720
  Assets:Unknown  -$2563.50
""")
Example #9
0
def import_ofx(ledger, args):
    sync = OfxSynchronizer(ledger,
                           hardcodeaccount=args.hardcodeaccount,
                           shortenaccount=args.shortenaccount)
    ofx = OfxSynchronizer.parse_file(args.PATH)

    if args.account != None and len(args.account) != len(ofx.accounts):
        sys.stderr.write(
            "number of account name (--account) does not match the number of accounts in the OFX file\n"
        )
        exit(1)

    account_idx = 0
    for account in ofx.accounts:
        txns = sync.filter(account.statement.transactions, account.account_id)
        try:
            accountname = args.account[account_idx]
        except (IndexError, TypeError):
            accountname = None
        if accountname is None:
            if account.institution is not None:
                accountname = "%s:%s" % (account.institution.organization,
                                         account.account_id)
            elif args.account_format is not None:
                accountname = args.account_format.format(
                    account_id=account.account_id,
                    account_type=account.account_type,
                    routing_number=account.routing_number,
                    branch_id=account.branch_id)
            else:
                accountname = "%s:%s" % (UNKNOWN_BANK_ACCOUNT, account_idx)

        # build SecurityList (including indexing by CUSIP and ticker symbol)
        security_list = SecurityList(ofx)

        converter = make_ofx_converter(
            account=account,
            name=accountname,
            ledger=ledger,
            indent=args.indent,
            fid=args.fid,
            unknownaccount=args.unknownaccount,
            payee_format=args.payee_format,
            hardcodeaccount=args.hardcodeaccount,  #TODO
            shortenaccount=args.shortenaccount,  #TODO
            security_list=security_list)
        print_results(converter, ofx, ledger, txns, args, account_idx)
        account_idx += 1
Example #10
0
    def test_investments(self):
        with open(os.path.join('fixtures', 'fidelity.ofx'), 'rb') as ofx_file:
            ofx = OfxParser.parse(ofx_file)
        converter = OfxConverter(account=ofx.account,
                                 name="Foo",
                                 security_list=SecurityList(ofx))
        self.assertEqualLedgerPosting(
            converter.convert(ofx.account.statement.transactions[0]).format(),
            """2012/07/20 YOU BOUGHT
  Foo  100.00000 INTC @ $25.635000000
  ; ofxid: 7776.01234567890.0123456789020201120120720
  Assets:Unknown  -$2563.50
""")
        # test no payee/memo
        self.assertEqualLedgerPosting(
            converter.convert(ofx.account.statement.transactions[1]).format(),
            """2012/07/27 Foo: buystock
  Foo  128.00000 SDRL @ $39.390900000
  ; ofxid: 7776.01234567890.0123456789020901120120727
  Assets:Unknown  -$5042.04
""")
Example #11
0
def test_investments(ofx):
    converter = OfxConverter(
        account=ofx.account,
        name="Foo",
        security_list=SecurityList(ofx),
    )
    assert (make_clean_posting(
        converter,
        ofx.account.statement.transactions[0]) == """2012/07/20 YOU BOUGHT
  Foo  100.00000 INTC @ $25.635000000
  ; ofxid: 7776.01234567890.0123456789020201120120720
  Assets:Unknown  -$2563.50
  Expenses:Commission  $7.95
""")
    # test no payee/memo
    assert (make_clean_posting(
        converter,
        ofx.account.statement.transactions[1]) == """2012/07/27 Foo: buystock
  Foo  128.00000 SDRL @ $39.390900000
  ; ofxid: 7776.01234567890.0123456789020901120120727
  Assets:Unknown  -$5042.04
  Expenses:Commission  $7.95
""")