Beispiel #1
0
    def test_addTradeDuration(self):
        '''
        Test the method DefineTrades.addStartTime. Send some randomly generated trades excluding
        the start field and then test the results for the start field. Specifically 
        Test that Duration val is placed only when the trade has 0 Shares
        Test that the the original is the same dur as that created by addTradeDuration
        Test Duration is difference from each Last Trade - Start val
        '''
        NUMTRADES = 4
        trades = list()
        start = pd.Timestamp('2018-06-06 09:30:00')
        df = pd.DataFrame()
        exclude = []
        for i in range(NUMTRADES):
            tdf, start = randomTradeGenerator2(i + 1,
                                               earliest=start,
                                               pdbool=True,
                                               exclude=exclude)
            df = df.append(tdf)
            exclude.append(tdf.Symb.unique()[0])

        df.reset_index(drop=True, inplace=True)

        frc = FinReqCol()
        df2 = df.copy()
        df2[frc.dur] = None
        df3 = df2.copy()

        df3 = df3.sort_values(['Symb', 'Account', 'Time'])
        dtrades = DefineTrades()
        df3 = dtrades.addTradeDuration(df3)

        for i in range(NUMTRADES):
            tnum = 'Trade ' + str(i + 1)
            tdf = df3[df3.Tindex == tnum]
            tdf_gen = df[df.Tindex == tnum]
            xl = tdf.index[-1]
            xl_gen = tdf_gen.index[-1]

            assert len(tdf.Tindex.unique()) == 1

            for i, row in tdf.iterrows():
                if row.Balance:
                    assert not row.Duration
                else:
                    if not row.Duration:
                        assert len(tdf) == 2
                        assert tdf.loc[tdf.index[0]].Side.startswith('HOLD')
                    # assert row.Duration
                    diff = row.Time - row.Start
                    assert diff == row.Duration
                    assert row.Duration == tdf_gen.loc[xl_gen].Duration
Beispiel #2
0
    def test_addTradePL(self):
        '''
        Test the method DefineTrades.addTradePL. Create random trade and remove the sum val
        
        Call addtradePL and compare the origianl generation with the new one. 
        Specifically Test that Sum val is placed only when the trade has 0 Shares
        Test that the the original is the same PL as that created by addTradeDuration
        Test Sum is sum or the PL values from the trade.
        '''
        NUMTRADES = 4
        trades = list()
        start = pd.Timestamp('2018-06-06 09:30:00')
        df = pd.DataFrame()
        exclude = []
        for i in range(4):
            tdf, start = randomTradeGenerator2(i + 1,
                                               earliest=start,
                                               pdbool=True,
                                               exclude=exclude)
            df = df.append(tdf)
            exclude.append(tdf.Symb.unique()[0])

        df.reset_index(drop=True, inplace=True)

        frc = FinReqCol()
        df2 = df.copy()

        df2[frc.sum] = None

        df3 = df2.copy()

        df3 = df3.sort_values(['Symb', 'Account', 'Time'])
        dtrades = DefineTrades()
        df3 = dtrades.addTradePL(df3)

        for i in range(NUMTRADES):
            tnum = 'Trade ' + str(i + 1)
            tdf = df3[df3.Tindex == tnum]
            tdf_gen = df[df.Tindex == tnum]

            for i, row in tdf.iterrows():
                if row.Balance:
                    assert not row.Sum
                else:

                    assert isclose(tdf['P / L'].sum(), row.Sum, abs_tol=1e-7)
                    assert isclose(row.Sum,
                                   tdf_gen.loc[tdf_gen.index[-1]].Sum,
                                   abs_tol=1e-7)
Beispiel #3
0
    def test_addSummaryPL(self):
        '''
        Test the method DefineTrades.addStartTime. Send some randomly generated trades excluding
        the start field and then test the results for the start field.
        '''
        NUMTRADES = 4
        trades = list()
        start = pd.Timestamp('2018-06-06 09:30:00')
        df = pd.DataFrame()
        exclude = []
        for i in range(4):
            tdf, start = randomTradeGenerator2(i + 1,
                                               earliest=start,
                                               pdbool=True,
                                               exclude=exclude)
            df = df.append(tdf)
            exclude.append(tdf.Symb.unique()[0])

        df.reset_index(drop=True, inplace=True)

        frc = FinReqCol()
        df2 = df.copy()

        df2[frc.sum] = None

        df3 = df2.copy()

        df3 = df3.sort_values(['Symb', 'Account', 'Time'])
        dtrades = DefineTrades()
        df3 = dtrades.addTradePL(df3)

        for i in range(NUMTRADES):
            tnum = 'Trade ' + str(i + 1)
            tdf = df3[df3.Tindex == tnum]
            tdf_gen = df[df.Tindex == tnum]

            for i, row in tdf.iterrows():
                if row.Balance:
                    assert not row.Sum
                else:

                    assert isclose(tdf['P / L'].sum(), row.Sum, abs_tol=1e-7)
                    assert isclose(row.Sum,
                                   tdf_gen.loc[tdf_gen.index[-1]].Sum,
                                   abs_tol=1e-7)
Beispiel #4
0
    def test_addDateFieldx(self):
        '''
        Test the method writeShareBalance. Send some randomly generated trades side and qty and
        test the share balance that returns. Sort both and compare the results using the place
        index iloc
        '''
        NUMTRADES = 4
        start = pd.Timestamp('2018-06-06 09:30:00')
        df = pd.DataFrame()
        exclude = []
        for i in range(NUMTRADES):
            tdf, start = randomTradeGenerator2(i + 1,
                                               earliest=start,
                                               pdbool=True,
                                               exclude=exclude)
            df = df.append(tdf)
            exclude.append(tdf.Symb.unique()[0])

        df.reset_index(drop=True, inplace=True)
        rc = ReqCol()

        df2 = df[['Time', 'Symb', 'Side', 'Qty', 'Account', 'P / L']].copy()
        idf = InputDataFrame()
        df2 = idf.addDateField(df2, start)

        for i in range(len(df2)):
            rprev = rnext = ''
            row = df2.iloc[i]
            rprev = df2.iloc[i - 1] if i != 0 else ''
            rnext = df2.iloc[i + 1] if i < (len(df2) - 1) else ''
            daydelt = pd.Timedelta(days=1)
            # print(row.Side, type(rprev), type(rnext))
            rt = pd.Timestamp(row.Time)
            rd = pd.Timestamp(row.Date)
            assert rt.time() == rd.time()

            if row.Side == 'HOLD-B' or row.Side == 'HOLD+B':
                assert row.Date.date() == rnext.Date.date() - daydelt
            if row.Side == 'HOLD-' or row.Side == 'HOLD+':
                assert row.Date.date() == rprev.Date.date() + daydelt

        return df2
Beispiel #5
0
    def test_addStartTime(self):
        '''
        Test the method DefineTrades.addStartTime. Send some randomly generated trades excluding
        the start field and then test the results for the start field.
        '''
        NUMTRADES = 4
        trades = list()
        start = pd.Timestamp('2019-01-01 09:30:00')
        exclude = []
        for i in range(4):
            t, start = randomTradeGenerator2(i + 1,
                                             earliest=start,
                                             exclude=exclude)
            trades.extend(t)
            # print(t[0][3])
            exclude.append(t[0][3])

        frc = FinReqCol()
        df = pd.DataFrame(data=trades,
                          columns=[
                              frc.tix, frc.start, frc.time, frc.ticker,
                              frc.side, frc.shares, frc.bal, frc.acct, frc.PL,
                              frc.sum, frc.dur
                          ])
        df2 = df.copy()
        df2[frc.start] = None

        dtrades = DefineTrades()
        df3 = df2.copy()
        df3.sort_values(['Symb', 'Account', 'Time'], inplace=True)
        df3 = dtrades.addStartTime(df3)
        for i in range(len(df3)):
            # Tests that addtradeIndex recreated the same index locations that rtg did after sorting the trades
            if df3.loc[i][frc.start] != df.loc[i][frc.start]:
                print('Found and error at index', i)
                print(df3.loc[i][frc.start], df.loc[i][frc.start])
            self.assertEqual(df3.loc[i][frc.start], df.loc[i][frc.start])
        for i in range(NUMTRADES):
            # Test all trades have a single start time
            tradeNum = 'Trade ' + str(i + 1)
            tdf = df3[df3.Tindex == tradeNum].copy()
            self.assertEqual(len(tdf.Start.unique()), 1)
Beispiel #6
0
    def test_writeShareBalance(self):
        '''
        Test the method writeShareBalance. Send some randomly generated trades. Remove a bunch of
        columns and call writeShareBalance. Test that the share balance was recreated correctly
        test the share balance that returns. Sort both and compare the results using the place
        index iloc
        '''
        NUMTRADES = 4
        trades = list()
        start = pd.Timestamp('2018-06-06 09:30:00')
        df = pd.DataFrame()
        exclude = []
        for i in range(NUMTRADES):
            tdf, start = randomTradeGenerator2(i + 1,
                                               earliest=start,
                                               pdbool=True,
                                               exclude=exclude)
            df = df.append(tdf)
            exclude.append(tdf.Symb.unique()[0])

        df.reset_index(drop=True, inplace=True)

        frc = FinReqCol()
        df2 = df.copy()

        df2[frc.sum] = None
        df2[frc.start] = None
        df2[frc.tix] = None
        df2[frc.PL] = None
        df2[frc.dur] = None
        df2[frc.bal] = 0

        df3 = df2.copy()
        df3 = df3.sort_values(['Symb', 'Account', 'Time'])
        df3.reset_index(drop=True, inplace=True)
        df = df.sort_values(['Symb', 'Account', 'Time'])

        dtrades = DefineTrades()
        df3 = dtrades.writeShareBalance(df3)
        for i in range(len(df3)):
            assert df3.iloc[i][frc.bal] == df.iloc[i][frc.bal]
Beispiel #7
0
    def test_addTradeIndex(self):
        '''
        Test addTradeIndex
        '''
        NUMRUNS = 10
        NUMTRADES = 10  # Number of trades to aggregate into single statement
        for i in range(NUMRUNS):
            # if not i % 20:
            #     print(f'{i}/{NUMRUNS} ', end='')
            earliest = pd.Timestamp('2018-06-06 09:30:00')
            delt = pd.Timedelta(minutes=1)
            df = pd.DataFrame()
            exclude = []
            for j in range(NUMTRADES):
                tdf, earliest = randomTradeGenerator2(j + 1,
                                                      earliest=earliest,
                                                      pdbool=True,
                                                      exclude=exclude)
                exclude.append(tdf.iloc[0].Symb)
                df = df.append(tdf)
                earliest = earliest + delt

                df.reset_index(drop=True, inplace=True)
                df = df.sort_values(['Symb', 'Account', 'Time'])
            frc = FinReqCol()
            df2 = df.copy()
            df2[frc.tix] = ''
            df2.sort_values(['Symb', 'Account', 'Time'], inplace=True)

            dtrades = DefineTrades()
            ddf = dtrades.addTradeIndex(df2)

            for k in range(4):
                tnum = 'Trade ' + str(k + 1)
                tdf = ddf[ddf.Tindex == tnum].copy()
                xl = tdf.index[-1]
                lastd = None
                thisd = None
                for j, row in tdf.iterrows():
                    if j != xl:
                        if tdf.at[j, 'Balance'] == 0:
                            print('Found an error at index', j,
                                  'The Balance should not be 0')
                            print(tdf[[
                                'Symb', 'Tindex', 'Account', 'Time', 'Side',
                                'Qty', 'Balance'
                            ]])
                        self.assertNotEqual(tdf.at[j, 'Balance'], 0)
                    else:
                        if tdf.at[j, 'Balance'] != 0:
                            print('Found an error at index', xl,
                                  'The balance should be 0')
                            print(df[[
                                'Symb', 'Tindex', 'Account', 'Time', 'Side',
                                'Qty', 'Balance'
                            ]])
                        self.assertEqual(tdf.at[j, 'Balance'], 0)
                    if lastd:
                        if lastd > thisd:
                            print('Found an error in the Time sequencing of',
                                  tnum)
                            print(thisd, ' > ', lastd, ' = ', thisd > lastd)
                            print(df[[
                                'Symb', 'Tindex', 'Account', 'Time', 'Side',
                                'Qty', 'Balance'
                            ]])
                            self.assertGreater(thisd, lastd)