def test_create_interest_weekend_event(self): blt = self.make_blotter() blt.connect_market_data() ts = pd.Timestamp('2015-08-06T00:00:00') blt._holdings.update_cash(ts, "AUD", 1000000) blt._holdings.update_cash(ts, "JPY", 1000000) ts = pd.Timestamp('2015-08-07T00:00:00') evs = blt.create_events(ts, "INTEREST") irates = pd.read_csv(self.rates, index_col=0, parse_dates=True) aud_int = irates.loc[ts, "AUD"] / 365 * 3 * 1000000 jpy_int = irates.loc[ts, "JPY"] / 365 * 3 * 1000000 evs_exp = [ blotter._Event("INTEREST", { "timestamp": ts, "ccy": "AUD", "quantity": aud_int }), blotter._Event("INTEREST", { "timestamp": ts, "ccy": "JPY", "quantity": jpy_int }) ] self.assertEventsEqual(evs, evs_exp)
def test_create_trade_fx_USDCAD(self): blt = blotter.Blotter(self.prices, self.rates, base_ccy="USD") blt.connect_market_data() blt.define_generic("USDCAD", "CAD", 0, 1, 0, True) blt.map_instrument("USDCAD", "USDCAD") ts = pd.Timestamp('2015-08-03T12:00:00') evs = blt._create_trade(ts, "USDCAD", quantity=1000, price=1.31) ev_exp = [ blotter._Event( "TRADE", { "timestamp": ts, "instrument": "USDCAD", "ccy": "CAD", "price": 1.31, "quantity": 1000, "multiplier": 1, "commission": 0 }), blotter._Event("CASH", { "timestamp": ts, "ccy": "CAD", "quantity": -1000 * 1.31 }), blotter._Event("CASH", { "timestamp": ts, "ccy": "USD", "quantity": 1000 }) ] self.assertEventsEqual(evs, ev_exp)
def test_create_read_log(self): blt = blotter.Blotter(self.prices, self.rates, base_ccy="USD") # test events can be properly read error free blt.read_log(self.log) evs = blt._create_log_events(self.log) ts1 = pd.Timestamp('2016-12-01T10:00:00') ts2 = pd.Timestamp('2016-12-02T10:00:00') exp_evs = [ blotter._Event( "TRADE", { "timestamp": ts1, "instrument": "CLZ16", "ccy": "USD", "price": 53.46, "quantity": 100, "multiplier": 1, "commission": 2.50 }), blotter._Event( "TRADE", { "timestamp": ts2, "instrument": "CLZ16", "ccy": "USD", "price": 55.32, "ntc_price": 55.32, "quantity": 100, "multiplier": 1, "commission": 2.50 }) ] self.assertEventsEqual(evs, exp_evs)
def test_create_short_margin_event(self): blt = blotter.Blotter(self.prices, self.rates, base_ccy="USD", margin_charge=0.015) blt.connect_market_data() ts = pd.Timestamp('2015-08-04T00:00:00') qty = -1 price = 0 blt.define_generic("ES", "USD", 0.05, 1, 2.5) blt.map_instrument("ES", "ESZ15") blt._trade(ts, "ESZ15", qty, price) ts = pd.Timestamp('2015-08-05T00:00:00') ev = blt.create_events(ts, "MARGIN") rates = pd.read_csv(self.rates, index_col=0, parse_dates=True) es_fp = os.path.join(self.prices, 'ESZ15.csv') es = pd.read_csv(es_fp, index_col=0, parse_dates=True) es_notional = float(es.loc[ts].values * np.abs(qty) * 0.05) quantity = es_notional * (rates.loc[ts, "USD"] + 0.015) / 365 ev_exp = [ blotter._Event("INTEREST", { "timestamp": ts, "ccy": "USD", "quantity": quantity }) ] self.assertEventsEqual(ev, ev_exp)
def test_pnl(self): blt = blotter.Blotter() data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'prices': pd.Series([53.46, 52], index=['CLZ6', 'COZ6']) } ev = [blotter._Event('PNL', data)] blt.dispatch_events(ev)
def test_cash(self): blt = blotter.Blotter() data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'ccy': 'USD', 'quantity': 1000 } ev = [blotter._Event('CASH', data)] blt.dispatch_events(ev)
def test_dispatch_unknown_event(self): blt = self.make_blotter() ev = blotter._Event("NotAnEvent", {"timestamp": pd.Timestamp('2015-01-01')}) def dispatch_unknown(): blt.dispatch_events([ev]) self.assertRaises(NotImplementedError, dispatch_unknown)
def test_sweep(self): blt = blotter.Blotter() data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'ccy1': 'USD', 'quantity1': 1000, 'ccy2': 'CAD', 'quantity2': -1300 } ev = [blotter._Event('PNL_SWEEP', data)] blt.dispatch_events(ev)
def test_pnl_fromstring(self): trd_str = ('PNL|{"timestamp":"2016-12-01 10:00:00",' '"prices":{"CLZ6": 53.46, "COZ6": 52}}') ev = blotter._Event.fromstring(trd_str) data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'prices': pd.Series([53.46, 52], index=['CLZ6', 'COZ6']) } ev_exp = blotter._Event('PNL', data) self.assertEventEqual(ev, ev_exp)
def test_tostr_valid_dict(self): data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'instrument': 'CLZ6', 'price': 53.46, 'quantity': 100, 'commission': 2.50, 'ccy': 'USD' } trd_str = str(blotter._Event('TRADE', data)) ast.literal_eval(trd_str.split('|')[1])
def test_trade(self): blt = blotter.Blotter() data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'instrument': 'CLZ6', 'price': 53.46, 'quantity': 100, 'multiplier': 1, 'commission': 2.50, 'ccy': 'USD' } ev = [blotter._Event('TRADE', data)] blt.dispatch_events(ev)
def test_trade_tostr(self): data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'instrument': 'CLZ6', 'price': 53.46, 'quantity': 100, 'commission': 2.50, 'ccy': 'USD' } trd_str = str(blotter._Event('TRADE', data)) exp_str = ('TRADE|{"timestamp": "2016-12-01 10:00:00", ' '"ccy": "USD", "commission": 2.5, "instrument": "CLZ6", ' '"price": 53.46, "quantity": 100}') self.assertEqual(trd_str, exp_str)
def test_trade_fromstring(self): trd_str = ('TRADE|{"timestamp":"2016-12-01 10:00:00",' '"instrument":"CLZ6","price":53.46,"quantity":100,' '"commission":2.50,"ccy":"USD"}') ev = blotter._Event.fromstring(trd_str) data = { 'timestamp': pd.Timestamp('2016-12-01T10:00:00'), 'instrument': 'CLZ6', 'price': 53.46, 'quantity': 100, 'commission': 2.50, 'ccy': 'USD' } ev_exp = blotter._Event('TRADE', data) self.assertEventEqual(ev, ev_exp)
def test_create_pnl_sweep_event_closed_pnl(self): blt = blotter.Blotter(self.prices, self.rates, base_ccy="USD") blt.connect_market_data() ts = pd.Timestamp('2015-08-03T12:00:00') blt._holdings.record_trade(ts, 'CLZ15', 50.50, 1, 1, 0, "CAD") ts = pd.Timestamp('2015-08-03T14:00:00') blt._holdings.record_trade(ts, 'CLZ15', 51.50, -1, 1, 0, "CAD") ts = pd.Timestamp('2015-08-04T00:00:00') evs = blt.create_events(ts, "PNL_SWEEP") evs_exp = [ blotter._Event( "PNL_SWEEP", { "timestamp": ts, "ccy1": "CAD", "quantity1": -1.00, "ccy2": "USD", "quantity2": 1 / 1.3125 }) ] self.assertEventsEqual(evs, evs_exp)
def test_closed_position_pnl_event(self): blt = self.make_blotter() blt.connect_market_data() ts = pd.Timestamp('2015-08-04T00:00:00') qty = 1 price = 0 blt.define_generic("ES", "USD", 0.05, 1, 2.5) blt.map_instrument("ES", "ESZ15") blt._trade(ts, "ESZ15", qty, price) ts = pd.Timestamp('2015-08-05T00:00:00') blt._trade(ts, "ESZ15", -qty, price) ts = pd.Timestamp('2015-08-06T00:00:00') ev = blt.create_events(ts, "PNL") ev_exp = [ blotter._Event("PNL", { "timestamp": ts, "prices": pd.Series([]) }) ] self.assertEventsEqual(ev, ev_exp)
def test_create_trade_future(self): blt = blotter.Blotter(self.prices, self.rates, base_ccy="USD") blt.connect_market_data() blt.define_generic("ES", "USD", 0, 1, 0, False) blt.map_instrument("ES", "ESZ15") ts = pd.Timestamp('2015-08-03T12:00:00') evs = blt._create_trade(ts, "ESZ15", quantity=1, price=1800) ev_exp = [ blotter._Event( "TRADE", { "timestamp": ts, "instrument": "ESZ15", "ccy": "USD", "price": 1800, "quantity": 1, "multiplier": 1, "commission": 0 }) ] self.assertEventsEqual(evs, ev_exp)
def test_create_pnl_event(self): blt = self.make_blotter() blt.connect_market_data() ts = pd.Timestamp('2015-08-04T00:00:00') qty = 1 price = 0 blt.define_generic("SXM", "CAD", 0.1, 1, 2.5) blt.map_instrument("SXM", "SXMZ15") blt.define_generic("ES", "USD", 0.05, 1, 2.5) blt.map_instrument("ES", "ESZ15") blt._trade(ts, 'SXMZ15', qty, price) blt._trade(ts, "ESZ15", qty, price) ts = pd.Timestamp('2015-08-05T00:00:00') ev = blt.create_events(ts, "PNL") es_fp = os.path.join(self.prices, 'ESZ15.csv') es = pd.read_csv(es_fp, index_col=0, parse_dates=True) sxm_fp = os.path.join(self.prices, 'SXMZ15.csv') sxm = pd.read_csv(sxm_fp, index_col=0, parse_dates=True) prices = pd.concat([es.loc[ts], sxm.loc[ts]], axis=0) ev_exp = [blotter._Event("PNL", {"timestamp": ts, "prices": prices})] self.assertEventsEqual(ev, ev_exp)
def make_ev(): blotter._Event('TRADE', data)