Esempio n. 1
0
 def test_price_projection(self):
     response = price_projection()
     assert response == {
         'rolloverStakes': False,
         'priceData': [],
         'exBestOffersOverrides': {},
         'virtualise': True
     }
Esempio n. 2
0
    def get_match_odds(self, market_id):

        # Get market catalogues
        market_catalogue_filter = filters.market_filter(
            market_ids=[market_id],
            # market_countries=['GB' 'ES', 'IT', 'DE']
        )
        market_catalogue = self.trading.betting.list_market_catalogue(
            filter=market_catalogue_filter,
            market_projection=['RUNNER_DESCRIPTION'],
            max_results='100'
        )
        if len(market_catalogue) == 0:
            print('EMPTY! ', market_id, market_catalogue)
            return None

        market_catalogue = market_catalogue[0]
        runner_names = {
            r.selection_id: r.runner_name for r in market_catalogue.runners
        }

        # Request market books
        market_books = self.trading.betting.list_market_book(
            market_ids=[market_id],
            price_projection=filters.price_projection(
                price_data=['EX_BEST_OFFERS']
            )
        )

        assert len(market_books) == 1

        book = market_books[0]
        assert book.number_of_winners == 1

        runners = book.runners

        # Extract prices
        best_back_prices = [self.get_price(runner, kind='back') for runner in runners]
        best_lay_prices = [self.get_price(runner, kind='lay') for runner in runners]

        selection_ids = [runner.selection_id for runner in runners]
        statuses = [runner.status for runner in runners]

        runner_names = [runner_names[selection_id] for selection_id in selection_ids]

        return pd.DataFrame({
            'MarketID': [market_id]*len(selection_ids),
            'SelectionID': selection_ids,
            'SelectionName': runner_names,
            'BestBackPrice': best_back_prices,
            'BestLayPrice': best_lay_prices,
            'Status': statuses,
        })
Esempio n. 3
0
def get_runners_df(trading, market_id):
    # Create a price filter. Get all traded and offer data
    price_filter = filters.price_projection(price_data=['EX_BEST_OFFERS'])

    # Request market books
    market_books = trading.betting.list_market_book(
        market_ids=[market_id], price_projection=price_filter)

    # Grab the first market book from the returned list as we only requested one market
    market_book = market_books[0]
    runners_df = _process_runner_books(market_book.runners)
    return runners_df
Esempio n. 4
0
 def test_price_projection(self):
     response = price_projection()
     assert response == {
         "rolloverStakes": False,
         "priceData": [],
         "exBestOffersOverrides": {},
         "virtualise": True,
     }
     response = price_projection(price_data=price_data(sp_available=True))
     assert response == {
         "rolloverStakes": False,
         "priceData": ["SP_AVAILABLE"],
         "exBestOffersOverrides": {},
         "virtualise": True,
     }
     response = price_projection()
     assert response == {
         "rolloverStakes": False,
         "priceData": [],
         "exBestOffersOverrides": {},
         "virtualise": True,
     }
Esempio n. 5
0
 def test_price_projection(self):
     response = price_projection()
     assert response == {
         'rolloverStakes': False,
         'priceData': [],
         'exBestOffersOverrides': {},
         'virtualise': True
     }
     response = price_projection(price_data=price_data(sp_available=True))
     assert response == {
         'rolloverStakes': False,
         'priceData': ["SP_AVAILABLE"],
         'exBestOffersOverrides': {},
         'virtualise': True
     }
     response = price_projection()
     assert response == {
         'rolloverStakes': False,
         'priceData': [],
         'exBestOffersOverrides': {},
         'virtualise': True
     }
Esempio n. 6
0
def list_market_book(market_id):
    trading = get_betfair_client()
    res = trading.betting.list_market_book(
        market_ids=[market_id],
        price_projection=price_projection(
            price_data(
                sp_available=False,
                sp_traded=False,
                ex_best_offers=True,
                ex_all_offers=False,
                ex_traded=True,
            )),
        order_projection='ALL',
        match_projection='ROLLED_UP_BY_PRICE',
        lightweight=True)
    print(f'res length {len(res)}')
    print(json.dumps(res, indent=3))
Esempio n. 7
0
    def request_market_books(self, market_catalogue: List[MarketCatalogue]):
        """
        Fetch list of market books by a given market catalogue(s)
        Rate Limits:
        sum(Weight) * number market ids must not exceed 200 points per requests
        EX_BEST_OFFERS equals to 5 points
        """
        market_ids_block = self.grouper(
            [i.market_id for i in market_catalogue], 40)

        market_books = []
        for market_ids in market_ids_block:
            market_books.extend(
                self.api.betting.list_market_book(
                    market_ids=market_ids,
                    price_projection=filters.price_projection(
                        price_data=filters.price_data(ex_best_offers=True)),
                ))

        return market_books
Esempio n. 8
0
def monitor_market(pk):
    """monitor market"""
    market = Market.objects.get(id=pk)
    trading = get_betfair_client()
    res = trading.betting.list_market_book(
        market_ids=[market.market_id],
        price_projection=price_projection(
            price_data(
                sp_available=False,
                sp_traded=False,
                ex_best_offers=True,
                ex_all_offers=True,
                ex_traded=True,
            )),
        order_projection='ALL',
        match_projection='ROLLED_UP_BY_PRICE',
        lightweight=True)

    if len(res) != 1:
        raise Exception(f'MarketBook not found for {market}')
    item = res[0]

    # check that book is open and not inplay
    if item['status'] != 'OPEN' or item['inplay']:
        logger.error(f'Book for market is not open/inplay: {market}')
        return

    book = upsert_market_book(market, item)
    rbooks = upsert_runner_book(book, item)
    if book.number_of_active_runners != len(rbooks):
        logger.error(
            f'Missing runners {book.number_of_active_runners} vs {len(rbooks)} in {book}'
        )
        logger.error(
            f'Market has {market.runner_set.count()} runners (expecting {book.number_of_runners} from book)'
        )
    logger.warning(f'Finished monitored market {market}')
Esempio n. 9
0
# race/market info
race_info = {}
for m in market_catalogues:
    race_info['id'] = m.market_id
    # race_info['start'] = m.market_start_time
    # race_info['runners'] = [(r.selection_id, r.runner_name) for r in m.runners]

t_end = time.time() + 60 * 10  # 10 mins

# if start time is now then run for 10 mins...
while time.time() < t_end:
    # race/market price info
    market_books = trading.betting.list_market_book(
        market_ids=[race_info['id']],
        price_projection=filters.price_projection(
            price_data=filters.price_data(
                ex_all_offers=True)  # what other options?
        ),
    )

    market_info = {}
    for mb in market_books:
        market_info['id'] = mb.market_id
        market_info['inplay'] = mb.inplay
        market_info['status'] = mb.status
        # market_info['matched'] = mb.total_matched

        market_info['a2b_prices'] = [
            r.ex.available_to_back[0].price if r.ex.available_to_back else 1.01
            for r in mb.runners
        ]
        # market_info['a2b_sizes'] = [r.ex.available_to_back[0].size if r.ex.available_to_back else 0 for r in mb.runners]
Esempio n. 10
0
                id_away = market_catalogue.runners[1].selection_id

                # print(market_catalogue.market_id)
                # print(f'{market_catalogue.runners[0].runner_name}({id_home}) vs. {market_catalogue.runners[1].runner_name}({id_away})')

                for runner in market_catalogue.runners:
                    runners[runner.selection_id] = runner.runner_name
                    if runner.runner_name != "The Draw":
                        runner_dict[runner.runner_name] = runner.selection_id
                    # if runner.runner_name != 'The Draw':
                    #     print(runner.selection_id, runner.runner_name)

                # market book request
                market_books = trading.betting.list_market_book(
                    market_ids=[market_catalogue.market_id],
                    price_projection=filters.price_projection(
                        price_data=filters.price_data(ex_all_offers=True)),
                )
                for market_book in market_books:
                    print(
                        f"Checking: {market_catalogue.runners[0].runner_name}({id_home}) vs. {market_catalogue.runners[1].runner_name}({id_away})"
                    )
                    try:
                        if list(teams.keys())[list(
                                teams.values()).index(id_home)]:
                            team_name_home = list(teams.keys())[list(
                                teams.values()).index(id_home)]
                            team_name_away = list(teams.keys())[list(
                                teams.values()).index(id_away)]
                            time.sleep(random.randint(0, 0))
                            html = urlopen(rating_address +
                                           str(rating_teams[team_name_home]) +
Esempio n. 11
0
    def betfair_get(self, is_ip, counter, sport='Tennis'):
        start = timezone.now()
        ip = 'ip' if is_ip else 'ni'

        trading = get_betfair_client()

        trading.login()
        trading.keep_alive()
        tennis_event_type = self.sport[sport]
        markets = trading.betting.list_market_catalogue(
            market_filter(event_type_ids=[tennis_event_type],
                          market_type_codes=["MATCH_ODDS"],
                          in_play_only=is_ip),
            market_projection=[
                "MARKET_DESCRIPTION",
                "RUNNER_DESCRIPTION",
                "EVENT",
                "EVENT_TYPE",
                "COMPETITION",
                "RUNNER_METADATA",
                "MARKET_START_TIME",
            ],
            max_results=100)

        mids = [m.market_id for m in markets]
        mids_portions = [mids[x:x + 40] for x in range(0, len(mids), 40)]
        #print(mids[0])
        for mp in mids_portions:
            (','.join(mp))
            books = trading.betting.list_market_book(
                market_ids=mp,
                price_projection=price_projection(
                    price_data(ex_best_offers=True, )),
                lightweight=False)

            for b in books:

                m = next((x for x in markets if x.market_id == b.market_id),
                         None)

                if m.competition == None: continue

                bfcid = m.competition.id
                marids = m.market_id
                print(marids)
                champname = m.competition.name
                country = m.event.country_code
                event_id = m.event.id
                ds = timezone.make_aware(m.event.open_date)
                p1 = m.runners[0].runner_name

                if (p1 == None or '/' in p1):
                    continue

                rid1 = m.runners[0].selection_id
                p2 = m.runners[1].runner_name
                rid2 = m.runners[1].selection_id
                event, created = BFEvent.objects.get_or_create(bfid=event_id)

                if (created):
                    champ = self.save_champ(bfcid, champname,
                                            tennis_event_type, country)
                    player1 = self.save_player(p1)
                    player2 = self.save_player(p2)
                    event.cid = champ
                    event.marids = marids
                    event.rid1 = rid1
                    event.rid2 = rid2
                    event.pid1 = player1
                    event.pid2 = player2
                    event.dt = ds
                    event.status = 1 if is_ip else 0
                    event.save()
                p1b1_odds = b.runners[0].ex.available_to_back[0].price if len(
                    b.runners[0].ex.available_to_back) > 0 else 0
                p1b1_size = b.runners[0].ex.available_to_back[0].size if len(
                    b.runners[0].ex.available_to_back) > 0 else 0
                p1l1_odds = b.runners[0].ex.available_to_lay[0].price if len(
                    b.runners[0].ex.available_to_lay) > 0 else 0
                p1l1_size = b.runners[0].ex.available_to_lay[0].size if len(
                    b.runners[0].ex.available_to_lay) > 0 else 0
                p2b1_odds = b.runners[1].ex.available_to_back[0].price if len(
                    b.runners[1].ex.available_to_back) > 0 else 0
                p2b1_size = b.runners[1].ex.available_to_back[0].size if len(
                    b.runners[1].ex.available_to_back) > 0 else 0
                p2l1_odds = b.runners[1].ex.available_to_lay[0].price if len(
                    b.runners[1].ex.available_to_lay) > 0 else 0
                p2l1_size = b.runners[1].ex.available_to_lay[0].size if len(
                    b.runners[1].ex.available_to_lay) > 0 else 0

                try:
                    odds = BFOdds.objects.filter(eid=event).latest('dtc')
                    if (odds.b1odds != p1b1_odds or odds.b2odds != p2b1_odds
                            or odds.l1odds != p1l1_odds
                            or odds.l2odds != p2l1_odds):
                        self.save_odds(event, p1b1_odds, p2b1_odds, p1l1_odds,
                                       p2l1_odds, p1b1_size, p2b1_size,
                                       p1l1_size, p2l1_size, is_ip)
                        if (self.is_debug):
                            self.stdout.write(
                                'evid[%s], mid[%s] %s %s - %s in %s: %s/%s\n' %
                                (event_id, b.market_id, ds, p1.replace(
                                    '\\', ''), p2.replace('\\', ''), country,
                                 b.total_matched, b.total_available),
                                ending='')
                            self.stdout.write(
                                '[%s]%s:%s@%s\t|%s@%s\n' %
                                (rid1, p1.replace('\\', ''), p1b1_size,
                                 p1b1_odds, p1l1_size, p1l1_odds),
                                ending='')
                            self.stdout.write(
                                '[%s]%s:%s@%s\t|%s@%s\n' %
                                (rid2, p2.replace('\\', ''), p2b1_size,
                                 p2b1_odds, p2l1_size, p2l1_odds),
                                ending='')
                except:
                    self.save_odds(event, p1b1_odds, p2b1_odds, p1l1_odds,
                                   p2l1_odds, p1b1_size, p2b1_size, p1l1_size,
                                   p2l1_size, is_ip)
                    if (self.is_debug):
                        self.stdout.write(
                            'evid[%s], mid[%s] %s %s - %s in %s: %s/%s\n' %
                            (event_id, b.market_id, ds, p1.replace(
                                '\\', ''), p2.replace('\\', ''), country,
                             b.total_matched, b.total_available),
                            ending='')
                        self.stdout.write(
                            '[%s]%s:%s@%s\t|%s@%s\n' %
                            (rid1, p1.replace('\\', ''), p1b1_size, p1b1_odds,
                             p1l1_size, p1l1_odds),
                            ending='')
                        self.stdout.write(
                            '[%s]%s:%s@%s\t|%s@%s\n' %
                            (rid2, p2.replace('\\', ''), p2b1_size, p2b1_odds,
                             p2l1_size, p2l1_odds),
                            ending='')
        end = timezone.now()
        log = ALog()
        log.name = 'update_bf_%s_%s' % (sport, ip)
        log.dts = start
        log.counter = counter
        log.duration = (end - start).total_seconds()
        log.save()
        if (self.is_debug):
            self.stdout.write('total execution is %s seconds\n' %
                              (end - start),
                              ending='')