Exemplo n.º 1
0
def get_books(market_ids):
    # Get information for Markets
    return client.list_market_book(
        market_ids=market_ids,
        price_projection=PriceProjection(price_data=[PriceData.EX_ALL_OFFERS]),
        order_projection=OrderProjection.EXECUTABLE,
        match_projection=MatchProjection.ROLLED_UP_BY_AVG_PRICE)
 def test_update_prices(self):
     pp = PriceProjection(price_data=['EX_BEST_OFFERS', 'EX_TRADED'])
     markets = self.client.list_market_book(market_ids=[123.456],
                                            price_projection=pp)
     self.assertEqual(len(markets), 4)
     self.assertEqual(markets[0].runners[0].ex.traded_volume[7].size,
                      130.28)
     self.assertEqual(markets[2].runners[2].last_price_traded, 6.2)
     self.assertEqual(
         markets[3].runners[5].ex.available_to_back[1].values(),
         [16.5, 2.6])
    def get_price_market_selection(self, market_id, selection_id):
        price_projection = PriceProjection()
        price_projection.price_data = [PriceData.EX_BEST_OFFERS]
        price_projection.virtualise = True
        price_projection.rollover_stakes = True
        ex_best_offers_overrides = ExBestOffersOverrides()
        ex_best_offers_overrides.best_prices_depth = 3
        ex_best_offers_overrides.rollup_model = RollupModel.STAKE
        ex_best_offers_overrides.rollup_limit = 1
        price_projection.ex_best_offers_overrides = ex_best_offers_overrides
        books = self.client.list_market_book(market_ids=[market_id],
                                             price_projection=price_projection)

        get_logger().debug("asking for runners",
                           market_id=market_id,
                           selection_id=selection_id)
        for runner in books[0].runners:
            if runner.selection_id == selection_id:
                if len(runner.ex.available_to_back) == 0:
                    return None, None, None, None, None
                back = runner.ex.available_to_back[0].price
                lay = None
                if len(runner.ex.available_to_lay) != 0:
                    lay = runner.ex.available_to_lay[0].price
                size = runner.ex.available_to_back[0].size
                orders = runner.orders
                status = runner.status
                if books[0].status == "SUSPENDED":
                    status = "SUSPENDED"
                return back, lay, size, status, orders

        return None, None, None, None, None
Exemplo n.º 4
0
 def update_markets(self, events, markets):
     pp = PriceProjection(price_data=[
         'EX_BEST_OFFERS', 'EX_TRADED', 'SP_AVAILABLE', 'SP_TRADED'
     ])
     market_ids = []
     for m in markets.values():
         for r in m:
             market_ids.append(r[0])
     markets_resp = []
     for market_list in _split_list(market_ids):
         markets_resp.extend(
             self.client.list_market_book(market_ids=market_list,
                                          price_projection=pp))
     return markets_resp
    def get_runner_prices(self, runners):
        marketids = [market["market_id"] for market in runners.values()]
        price_projection = PriceProjection()
        price_projection.price_data = [v.name for v in PriceData]
        price_projection.rollover_stakes = False
        price_projection.virtualise = False
        ex_best_offers_overrides = ExBestOffersOverrides()
        ex_best_offers_overrides.best_prices_depth = 1
        ex_best_offers_overrides.rollup_model = RollupModel.STAKE
        ex_best_offers_overrides.rollup_limit = 0
        price_projection.ex_best_offers_overrides = ex_best_offers_overrides
        books = self.client.list_market_book(market_ids=marketids,
                                             price_projection=price_projection)
        get_logger().debug("asking for prices", market_id=marketids)

        selection_ids = {
            market["selection_id"]: s
            for s, market in runners.items()
        }
        for book in books:
            for runner in book.runners:
                if runner.selection_id in selection_ids:
                    if runner.status is not "ACTIVE":
                        continue

                    runners[selection_ids[
                        runner.selection_id]]["stats"] = runner.status
                    runners[selection_ids[
                        runner.selection_id]]["inplay"] = book.inplay

                    if len(runner.ex.available_to_lay) > 0:
                        runners[selection_ids[runner.selection_id]][
                            "lay"] = runner.ex.available_to_lay[0].price
                        runners[selection_ids[runner.selection_id]][
                            "lay_size"] = runner.ex.available_to_lay[0].size
                    else:
                        runners[selection_ids[
                            runner.selection_id]]["lay"] = None
                        runners[selection_ids[
                            runner.selection_id]]["lay_size"] = None
                    if len(runner.ex.available_to_back) > 0:
                        runners[selection_ids[runner.selection_id]][
                            "back"] = runner.ex.available_to_back[0].price
                        runners[selection_ids[runner.selection_id]][
                            "back_size"] = runner.ex.available_to_back[0].size
                    else:
                        runners[selection_ids[
                            runner.selection_id]]["back"] = None
                        runners[selection_ids[
                            runner.selection_id]]["back_size"] = None

        return runners
Exemplo n.º 6
0
#Gets the list of market IDs in the Betting Market
sports_market_ids = []
for eachMarket in betting_markets:
    sports_market_ids = sports_market_ids + [eachMarket.market_id]

#Get the MarketBookResults
maxpullsize = 100
index = 0
print len(sports_market_ids)
marketbook_result = []
while index + maxpullsize < len(sports_market_ids):
    marketbook_result = marketbook_result + client.list_market_book(
        [sports_market_ids[index:index + maxpullsize]],
        PriceProjection(
            price_data=['EX_BEST_OFFERS'],
            exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))
    index = index + maxpullsize
marketbook_result = marketbook_result + client.list_market_book(
    [sports_market_ids[index:len(sports_market_ids)]],
    PriceProjection(
        price_data=['EX_BEST_OFFERS'],
        exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))

print 'Compiling Dataset'
market_data = []
for market in betting_markets:
    next_market_data = MarketData(
        market,
        next((x for x in marketbook_result if x.market_id == market.market_id),
             None))
Exemplo n.º 7
0
def betfair_get(self, is_ip, counter, sport='Tennis'):
    start = timezone.now()
    ip = 'ip' if is_ip else 'ni'

    client = Betfair('GkhXY3b3KmncnwLe', 'certs/bf.pem')
    # client = Betfair('uv9hV1EjlFimHmgr', 'certs/bf.pem')
    client.login('sherst', 'osT1G11nRe35')
    client.keep_alive()
    tennis_event_type = self.sports[sport]
    markets = client.list_market_catalogue(
        MarketFilter(event_type_ids=[tennis_event_type],
                     market_type_codes=["MATCH_ODDS"],
                     in_play_only=is_ip),
        market_projection=["EVENT", "RUNNER_METADATA", "COMPETITION"],
        max_results=200)
    mids = [m.market_id for m in markets]
    mids_portions = [mids[x:x + 40] for x in xrange(0, len(mids), 40)]
    for mp in mids_portions:
        print(','.join(mp))
        books = client.list_market_book(
            mp,
            price_projection=PriceProjection(
                price_data=betfair.constants.PriceData.EX_BEST_OFFERS))
        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
            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.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
            if (self.is_debug):
                self.stdout.write('*** evid [%s] %s - %s at %s, changed %s\n' %
                                  (event.id, event.pid1.name, event.pid2.name,
                                   event.dt, event.dtc),
                                  ending='')
            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='')
Exemplo n.º 8
0
    def get_runner_prices_and_save(self):
        price_projection = PriceProjection()
        price_projection.price_data = [
            PriceData.EX_BEST_OFFERS, PriceData.EX_TRADED
        ]
        iter_books = self.client.iter_list_market_book(
            market_ids=self.market_ids,
            price_projection=price_projection,
            chunk_size=10)
        timestamp = datetime.utcnow()
        all_runners = []
        for book in iter_books:
            for runner in book.runners:
                runner_dict = {}

                runner_dict["market_id"] = book.market_id
                runner_dict["selection_id"] = runner.selection_id

                runner_dict["timestamp"] = timestamp
                runner_dict["status"] = runner.status
                if runner.total_matched is not None:
                    runner_dict["total_matched"] = runner.total_matched
                if runner.last_price_traded is not None:
                    runner_dict["last_price_traded"] = runner.last_price_traded
                runner_dict["inplay"] = book.inplay

                len_book_lay = len(runner.ex.available_to_lay)
                for i in range(len_book_lay):
                    runner_dict["lay_" +
                                str(i +
                                    1)] = runner.ex.available_to_lay[i].price
                    runner_dict["lay_size_" +
                                str(i +
                                    1)] = runner.ex.available_to_lay[i].size

                len_book_back = len(runner.ex.available_to_back)
                for i in range(len_book_back):
                    runner_dict["back_" +
                                str(i +
                                    1)] = runner.ex.available_to_back[i].price
                    runner_dict["back_size_" +
                                str(i +
                                    1)] = runner.ex.available_to_back[i].size

                # for i in range(len_book_lay, 3):
                #     runner_dict["lay_" + str(i + 1)] = None
                #     runner_dict["lay_size_" + str(i + 1)] = None

                len_book_back = len(runner.ex.available_to_back)
                for i in range(len_book_back):
                    runner_dict["back_" +
                                str(i +
                                    1)] = runner.ex.available_to_back[i].price
                    runner_dict["back_size_" +
                                str(i +
                                    1)] = runner.ex.available_to_back[i].size
                #
                # for i in range(len_book_back, 3):
                #     runner_dict["back_" + str(i + 1)] = None
                #     runner_dict["back_size_" + str(i + 1)] = None

                all_runners.append(runner_dict)

        self.record(all_runners)
Exemplo n.º 9
0
def getOdds(Sport, User = "******",input_time = "Null",Market = 'MATCH_ODDS'):

    import pandas as pd
    import login
    import pytz
    import datetime
    from time import gmtime, strftime
    from betfair import utils
    from betfair import models
    from betfair import exceptions
    from betfair.models import MarketFilter
    from betfair.models import PriceProjection
    from betfair.models import ExBestOffersOverrides
    from betfair.models import MarketData

    if User == "Tom":
        #Tom
        client = login.login('Np2HwoLYyAQk2X6s', 'tombish22','parksandrec19')
    else:
        # Cal
        client = login.login('eTnX7n6jsiaoGA9g', '*****@*****.**','wyeslsc10')

    # Grab the sports that match the string provided as an argument
    event_types = client.list_event_types(MarketFilter(text_query=Sport))

    # Grab the ID of the requested sport
    sport_id = event_types[0].event_type.id

    # Pull the event details data for the sport and market specified as an argument
    betting_markets = client.list_market_catalogue(MarketFilter(event_type_ids=[sport_id], market_type_codes=[Market]), market_projection =  ['COMPETITION','EVENT','EVENT_TYPE','RUNNER_DESCRIPTION','MARKET_START_TIME'])

    # #Gets the list of market IDs in the Betting Market
    sports_market_ids = []
    for eachMarket in betting_markets:
        sports_market_ids = sports_market_ids + [eachMarket.market_id]

    # #Get the MarketBookResults
    maxpullsize = 100
    index=0
    # print len(sports_market_ids)
    marketbook_result = []
    while index+maxpullsize < len(sports_market_ids):
        marketbook_result = marketbook_result + client.list_market_book([sports_market_ids[index:index+maxpullsize]],PriceProjection(price_data=['EX_BEST_OFFERS'],exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))
        index=index+maxpullsize
    marketbook_result = marketbook_result + client.list_market_book([sports_market_ids[index:len(sports_market_ids)]],PriceProjection(price_data=['EX_BEST_OFFERS'],exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))  


    print 'Compiling Dataset'
    market_data = []
    for market in betting_markets:
        next_market_data = MarketData(market,next((x for x in marketbook_result if x.market_id == market.market_id), None))
        market_data.append(next_market_data)


    print 'Creating individual runner data'
    local = pytz.timezone ("Australia/Melbourne")
    runner_data = []
    for mindex, market in enumerate(market_data):
        for rindex, runner in enumerate(market.runners):
            runner_to_add = {'market_id': market.market_id, 
                                'market_name': market.market_name, 
                                'market_start_time': market.market_start_time.replace(tzinfo=pytz.utc).astimezone(local).strftime("%Y-%m-%d %H:%M:%S"),
                                'market_start_time_dt': market.market_start_time.replace(tzinfo=pytz.utc).astimezone(local),
                                #'total_matched':market.total_matched,
                                #'active_runners':market.number_of_active_runners,
                                'status':market.status,
                                #'total_available':market.total_available,
                                #'total_matched':market.total_matched,
                                #'selection_id':market.runner_catalog[rindex].selection_id,
                                'runner_name':market.runner_catalog[rindex].runner_name,
                                'price_selection_id':runner.selection_id,
                                'runner_status':runner.status
                                }
            
            if len(runner.ex.available_to_back)>0:
                runner_to_add.update({                         
                                'available_to_back_price':runner.ex.available_to_back[0].price,
                                'available_to_back_market':runner.ex.available_to_back[0].size
                                })
            if len(runner.ex.available_to_lay)>0:
                runner_to_add.update({                         
                                'available_to_lay_price':runner.ex.available_to_lay[0].price,
                                'available_to_lay_market':runner.ex.available_to_lay[0].size
                                })
            runner_data.append(runner_to_add)


    MarketDf = pd.DataFrame(runner_data)
    
    dt_cutoff = datetime.datetime.now().replace(tzinfo = local) + datetime.timedelta(days=2)

    # Only interested in open markets
    MarketDf.query("status == 'OPEN'", inplace=True)
    
    # Filter markets for those starting in the next 2 days
    MarketDf = MarketDf[(MarketDf.market_start_time_dt <= dt_cutoff)]
    
    #Add a logged time
    MarketDf.loc[:,"log_time"] = input_time
        
    #Reorder the columns and select
    cols = ["log_time","market_start_time","market_id","market_name","runner_name","price_selection_id","available_to_back_price","available_to_back_market","available_to_lay_price","available_to_lay_market"]
    MarketDf = MarketDf.loc[:,cols]
    
    return MarketDf
Exemplo n.º 10
0
 def get_single_market(self, market_id):
     pp = PriceProjection(
         price_data=['EX_BEST_OFFERS', 'SP_AVAILABLE', 'SP_TRADED'])
     markets = self.client.list_market_book(market_ids=[market_id],
                                            price_projection=pp)
     return markets
    if eachMarket.total_matched > 0:
	    key = key+1                 
print 'Non-Zero Markets on Sport: ' + str(key)

#Gets the list of market IDs in the Betting Market
sports_market_ids = []
for eachMarket in betting_markets:
    sports_market_ids = sports_market_ids + [eachMarket.market_id]

#Get the MarketBookResults
maxpullsize = 100
index=0
print len(sports_market_ids)
marketbook_result = []
while index+maxpullsize < len(sports_market_ids):
	marketbook_result = marketbook_result + client.list_market_book([sports_market_ids[index:index+maxpullsize]],PriceProjection(price_data=['EX_BEST_OFFERS'],exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))
	index=index+maxpullsize
marketbook_result = marketbook_result + client.list_market_book([sports_market_ids[index:len(sports_market_ids)]],PriceProjection(price_data=['EX_BEST_OFFERS'],exBestOffersOverrides=ExBestOffersOverrides(best_prices_depth=1)))	

print 'Compiling Dataset'
market_data = []
for market in betting_markets:
    next_market_data = MarketData(market,next((x for x in marketbook_result if x.market_id == market.market_id), None))
    market_data.append(next_market_data)


print 'Creating individual runner data'
runner_data = []
for mindex, market in enumerate(market_data):
    print mindex
    for rindex, runner in enumerate(market.runners):