Example #1
0
    def setUp(self):

        ddiirr = os.path.dirname(__file__)
        os.chdir(os.path.realpath(ddiirr))
        os.chdir(os.path.realpath('../'))
        self.inputtype = 'IB_CSV'

        indir = 'data/'
        # f2 = 'ActivityStatement.20190313_PL.html'

        jf = JournalFiles(indir=indir, infile=self.f1, theDate=self.theDate, inputType='IB_CSV', mydevel=False)

        jf.inputType = 'IB_CSV'
        # statement = Statement_IBActivity(jf)
        # df = statement.getTrades_IBActivity(jf.inpathfile)

        ibs = IbStatement(db=self.testdb)
        ibdb = StatementDB(self.testdb)
        ibdb.reinitializeTradeTables()
        ibs.openIBStatementCSV(jf.inpathfile)
        df2 = ibdb.getStatement(self.theDate)
        if df2.empty:
            sdate = self.theDate.strftime('%Y-%m-%d')
            msg = f'In test_dailycontrol.setup: Error: found no trades in db for {sdate}'
            self.assertTrue(not df2.empty, msg)

        tu = DefineTrades(jf.inputType)
        self.df, ldf = tu.processDBTrades(df2)
        sc = SumControl()
        lf = LayoutForms(sc, jf, self.df)
        lf.runTtoSummaries(ldf)
        self.ts = lf.ts
        ibdb.addTradeSummariesSA(self.ts, ldf)
Example #2
0
    def test_findTradeSummary(self):
        '''
        Test findTradeSummary, a helper method for addTradeSummaries and updateTradeSummaries.
        Note that one of those needs to have run and succeeded inorder to test this method.
        '''
        infile = "data/flex.369463.ActivityFlexMonth.20191008.20191106.csv"
        theDate = pd.Timestamp('2019-10-16')

        # Create these three objects
        ibs = IbStatement(db=self.db)
        ibdb = StatementDB(self.db)
        ibdb.reinitializeTradeTables()
        trades = DefineTrades("DB")

        # This call loads the statement into the db
        ibs.openIBStatementCSV(infile)

        # Here is an example of processing a single day of trades (3 calls)
        # This gets a collection of trades from a single day that can become a trade_sum entry
        df = ibdb.getStatement(theDate)

        # The following method and function process the statement transactions into a collection
        # of trades where each trade is a single row representing multiple transactions
        dframe, ldf = trades.processDBTrades(df)
        tradeSummaries, ts, entries, initialImageNames = runSummaries(ldf)

        ibdb.addTradeSummaries(ts, ldf)

        # The test database trades_sum should now only the trades from theDate, one
        # entry per trade
        for i, trade in enumerate(tradeSummaries):
            x = ibdb.findTradeSummary(theDate, trade['Start'].unique()[0])
            self.assertEqual(trade['Name'].unique()[0], x[1])
Example #3
0
    def test_figureBAPL(self):
        '''
        figureBAPL is called by openIBStatement and is give the trade tables and
        position tables from an IB statement. figureBAPL fills in Balance, Average
        and pnl. Every Balance entry should be filled. There could be blanks for
        Average. Here we can test all balance entries made it to the db
        '''
        ibs = IbStatement()
        ibdb = StatementDB()
        ibdb.reinitializeTradeTables()

        # These two files are a multiday flex and an activity.
        # Find a way to find recent files to test
        # trades1, meta1 = ibs.openIBStatement(self.testfile1)
        trades2, meta2 = ibs.openIBStatement(self.testfile2)
        len2 = len(trades2['TRNT']) if 'TRNT' in trades2.keys() else len(
            trades2['Trades'])

        ModelBase.connect(new_session=True)
        session = ModelBase.session
        q = session.query(Trade).all()
        q2 = session.query(Trade).filter(Trade.balance is None).all()
        self.assertEqual(len2, len(q))
        self.assertEqual(len(q2), 0)

        bu = Backup()
        bu.restore()
Example #4
0
    def test_openIbStatement(self):

        ibs = IbStatement(db=self.db)
        ibdb = StatementDB(db=self.db)
        ibdb.reinitializeTradeTables()
        x = ibs.openIBStatement(self.testfile1)
        self.assertIsNotNone(x)
        self.assertIsInstance(x[0], dict)
        self.assertTrue('TRNT' in x[0].keys() or 'Trades' in x[0].keys())
        st = ibdb.getStatement(self.theDate)
        self.assertIsInstance(st, pd.DataFrame)
        self.assertGreater(len(st), 0)
Example #5
0
    def test_ibstatement(self):
        '''Test basic usage loading an ib statement and getting a statement'''

        infile = "data/flex.369463.ActivityFlexMonth.20191008.20191106.csv"
        theDate = pd.Timestamp('2019-10-16')

        # Create these two objects
        ibs = IbStatement()
        ibdb = StatementDB()
        ibdb.reinitializeTradeTables()

        # This call loads the statement into the db
        ibs.openIBStatementCSV(infile)

        # This call will then retrieve one day of trades as a dataframe. theDate is string or timestamp
        df2 = ibdb.getStatement(theDate)
        self.assertIsInstance(df2, pd.DataFrame)
        self.assertFalse(df2.empty)
        bu = Backup()
        bu.restore()
Example #6
0
 def test_openIbStatement_notcsv(self):
     ibs = IbStatement(db=self.db)
     ibdb = StatementDB(db=self.db)
     ibdb.reinitializeTradeTables()
     x = ibs.openIBStatement('data\alittleOrgTODO.txt')
     self.assertEqual(len(x[0]), 0)