예제 #1
0
 def add_if_not_when_issued_or_dup(self, security):
     '''
     Add securities that satisfy specs to self.value, self.list_CUSIPs.
     '''
     if len(security["interestRate"]) > 0 and not (security["cusip"] in self.list_CUSIPs):
         ust_mat_date = self.get_maturity_date(security)
         time_to_maturity = (ust_mat_date - date.today()).days / 365.25
         time_to_maturity = str('%.2f' % time_to_maturity)
         price = self.get_bond_prices(security['cusip'])
         new_bond = bond.Bond(ust_mat_date, self.settle_date, float(security['interestRate']) / 100, 2)
         self.value.append({'mat_date': ''.join([security["maturityDate"][0:10]]), 'int_rate': round(float(security["interestRate"]), 3), \
              'cpns_per_yr': 2, 'ttm': time_to_maturity, 'cusip': security["cusip"], 'issue_date': security['issueDate'][0:10], \
              'yield': new_bond.bond_yield(price), 'price': price})
         self.list_CUSIPs.append(security["cusip"])
예제 #2
0
    def percieveBonds(self):
        """ This method attempts to percieve bonds

            It works by comparing atom distances to covalent radii of the atoms.
            It is not optimized in any way.
        """

        for iat, atom1 in enumerate(self.getAtoms()):
            for jat, atom2 in enumerate(self.getAtoms()):
                if iat <= jat: continue
                dr = atom2.getCoordinate() - atom1.getCoordinate()
                R2 = dr.dot(dr)

                dr_cov = atom1.getCovalentRadius() + atom2.getCovalentRadius(
                ) + self._bond_threshold
                R2_cov = dr_cov**2
                if R2 < R2_cov:
                    yield bond.Bond(id1=iat, id2=jat)
예제 #3
0
def main():
    exchange = connect()
    write_to_exchange(exchange, {"type": "hello", "team": team_name.upper()})
    hello_from_exchange = read_from_exchange(exchange)

    # A common mistake people make is to call write_to_exchange() > 1
    # time for every read_from_exchange() response.
    # Since many write messages generate marketdata, this will cause an
    # exponential explosion in pending messages. Please, don't do that!

    cash = [0]
    positions = {
        'BOND': 0,
        'BABZ': 0,
        'BABA': 0,
        'AAPL': 0,
        'MSFT': 0,
        'GOOG': 0,
        'XLK': 0
    }
    book = {}
    all_trades = {}
    order_obj = Order()

    # print("The exchange replied:", hello_from_exchange, file=sys.stderr)
    # print(type(hello_from_exchange))
    # print(hello_from_exchange.keys())

    for item in hello_from_exchange["symbols"]:
        positions[item['symbol']] = item['position']
    # print(positions)

    bond_obj = bond.Bond()

    # bond_obj.hello()

    trades = bond_obj.trade(order_obj, positions, all_trades)
    for trade in trades:
        all_trades[trade['order_id']] = trade
        write_to_exchange(exchange, trade)

    baaz_obj = baaz.Baaz()
    trades = baaz_obj.trade(book, order_obj, positions, all_trades)
    for trade in trades:
        all_trades[trade['order_id']] = trade
        write_to_exchange(exchange, trade)

    trades = baaz_obj.baba2baaz(book, order_obj, positions)
    for trade in trades:
        all_trades[trade['order_id']] = trade
        write_to_exchange(exchange, trade)

    trades = baaz_obj.baaz2baba(book, order_obj, positions)
    for trade in trades:
        all_trades[trade['order_id']] = trade
        write_to_exchange(exchange, trade)

    shoe_obj = shoe.Shoe()
    trades = shoe_obj.trade(book, order_obj, positions, all_trades)
    for trade in trades:
        all_trades[trade['order_id']] = trade
        write_to_exchange(exchange, trade)

    counter = 0

    reply = read_from_exchange(exchange)
    if (reply['type'] == 'reject'):
        return

    book_ctr = 0
    trade_ctr = 0
    OK_ctr = 0
    while (1):
        # book_ctr += 1
        if (book_ctr % 100 == 0):
            if 'SHOE' in book and 'BAAZ' in book and 'NIKE' in book and 'ADID' in book and 'FYUE' in book and 'BOND' in book and \
              len(book['BOND']['sell']) > 0 and len(book['BOND']['buy']) > 0 and \
              len(book['SHOE']['sell']) > 0 and len(book['SHOE']['buy']) > 0 and \
              len(book['BAAZ']['sell']) > 0 and len(book['BAAZ']['buy']) > 0 and \
              len(book['NIKE']['sell']) > 0 and len(book['NIKE']['buy']) > 0 and \
              len(book['ADID']['sell']) > 0 and len(book['ADID']['buy']) > 0 and \
              len(book['FYUE']['sell']) > 0 and len(book['FYUE']['buy']) > 0:
                shoe_sell = 10 * book['SHOE']['sell'][0][0]
                other_sell = 3 * book['BOND']['sell'][0][0] + 2 * book['NIKE']['sell'][0][0] + \
                       3 * book['ADID']['sell'][0][0] + 2 * book['FYUE']['sell'][0][0]
                shoe_buy = 10 * book['SHOE']['buy'][0][0]
                other_buy = 3 * book['BOND']['buy'][0][0] + 2 * book['NIKE']['buy'][0][0] + \
                      3 * book['ADID']['buy'][0][0] + 2 * book['FYUE']['buy'][0][0]
                # trade_ctr += 1
                # if shoe_sell < other_buy or other_sell < shoe_buy:
                # 	OK_ctr += 1

                # print("SHOE: ", 'sell:', 10 * book['SHOE']['sell'][0][0], ' buy:', 10 * book['SHOE']['buy'][0][0], ' ', end="")
                # print("OTHERS: ", 'sell:', 3 * book['BOND']['sell'][0][0] + 2 * book['NIKE']['sell'][0][0] +\
                # 			3 * book['ADID']['sell'][0][0] + 2 * book['FYUE']['sell'][0][0],
                # 			' buy:', 3 * book['BOND']['buy'][0][0] + 2 * book['NIKE']['buy'][0][0] +\
                # 			3 * book['ADID']['buy'][0][0] + 2 * book['FYUE']['buy'][0][0])
                # print("OK percent: ", float(OK_ctr) / trade_ctr, "OK: ", OK_ctr)
            book_ctr = 0

        reply = read_from_exchange(exchange)

        if (reply['type'] == 'book'):
            counter += 1
            if counter % 100 == 0:
                print("im running ", counter)
                counter = 0
            # print(1)
            # print("book: ", reply)
            book[reply['symbol']] = {
                'buy': reply['buy'],
                'sell': reply['sell']
            }
            trades = baaz_obj.baba2baaz(book, order_obj, positions)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

            trades = baaz_obj.baaz2baba(book, order_obj, positions)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

            trades = baaz_obj.trade(book, order_obj, positions, all_trades)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

            trades = shoe_obj.shoe2other(book, order_obj, positions)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

            trades = shoe_obj.other2shoe(book, order_obj, positions)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

            trades = shoe_obj.trade(book, order_obj, positions, all_trades)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

        elif (reply['type'] == 'fill'):
            # print(2)
            if (reply['dir'] == 'buy'):
                positions[reply['symbol']] += reply['size']
                cash -= reply['size'] * reply['price']
                all_trades[reply['order_id']] -= reply['size']
                if (all_trades[reply['order_id']] == 0):
                    all_trades.pop(reply['order_id'])

            elif (reply['dir'] == 'sell'):
                # print(3)
                positions[reply['symbol']] -= reply['size']
                cash += reply['size'] * reply['price']
                all_trades[reply['order_id']] -= reply['size']
                if (all_trades[reply['order_id']] == 0):
                    all_trades.pop(reply['order_id'])
            # print("log, fill-reply received: ", reply)

        elif (reply['type'] == 'out'):
            # print(4)
            trades = bond_obj.trade(order_obj, positions, all_trades)
            for trade in trades:
                all_trades[trade['order_id']] = trade
                write_to_exchange(exchange, trade)

        elif (reply['type'] == 'ack'):
            # print("ack", reply)
            pass
        elif (reply['type'] == 'reject'):
            # print("reject", reply)
            pass
예제 #4
0
def main():
    settle_date = date(2018, 7, 5)
    b1 = bond.Bond(date(2020, 6, 30), settle_date, 0.025, 2)
    return b1.bond_yield()
    '''
예제 #5
0
 def test_yield6(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 9, 30)
     b1 = bond.Bond(expiration_date, settle_date, 0.01375, 2)
     self.assertEqual(round(b1.bond_yield(97.375), 3), 2.595)
예제 #6
0
 def test_yield5(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 6, 15)
     b1 = bond.Bond(expiration_date, settle_date, 0.015, 2)
     self.assertEqual(round(b1.bond_yield(98), 3), 2.566)
예제 #7
0
 def test_yield2(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 9, 15)
     b2 = bond.Bond(expiration_date, settle_date, 0.01375, 2)
     self.assertEqual(round(b2.bond_yield(97.406), 3), 2.604)
예제 #8
0
 def test_yield7(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 8, 31)
     b1 = bond.Bond(expiration_date, settle_date, 0.01375, 2)
     self.assertEqual(round(b1.bond_yield(97.469), 3), 2.596)
예제 #9
0
 def test_yield8(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 7, 31)
     b1 = bond.Bond(expiration_date, settle_date, 0.01625, 2)
     self.assertEqual(round(b1.bond_yield(98.094), 3), 2.58)
예제 #10
0
 def test_yield9(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 6, 30)
     b1 = bond.Bond(expiration_date, settle_date, 0.01625, 2)
     self.assertEqual(round(b1.bond_yield(98.188), 3), 2.571)
예제 #11
0
 def test_yield1(self):
     settle_date = date(2018, 7, 9)
     expiration_date = date(2020, 6, 30)
     b1 = bond.Bond(expiration_date, settle_date, 0.025, 2)
     self.assertEqual(round(b1.bond_yield(99.875), 3), 2.565)