示例#1
0
    def get_odds(self, sport_id, leagues_ids, oddsFormat="Decimal"):
        req = URL(self.odds_v1.format(sport_id, leagues_ids, oddsFormat))
        since = self.last_since_id.get('last_odds')
        if since: req['since'] = since

        response = self.http.get(req.request_uri, headers=self.auth_headers)
        data = self.read_json(response)

        odds_list = []
        if not data: return odds_list

        try:
            for (_, ev) in self.get_event_pairs(data, "leagues"):
                event = Event(**ev)
                period = self.get_full_game_period(event)
                if not period: continue
                period = self.modify_odds_moneyline(period)
                # ignore special odds
                if not all([period.spreads, period.moneyline, period.totals]):
                    continue

                document = Odds.get_document(event.id, datetime.utcnow(),
                                             period.spreads, period.moneyline,
                                             period.totals)

                odds_list.append(document)
        except KeyError:
            raise Exception("Error occured during processing odds." +
                            " Pinnacle response: {}".format(data))
        except TypeError:
            self.logger.error("Invalid Event: {}".format(ev))

        # save since ID
        self.last_since_id['last_odds'] = data['last']
        return odds_list
示例#2
0
    def get_event_odds(self, markets: List[BetfairMarket]):
        """ Recieve list of books for a single event and return odds object """
        moneyline, spreads, totals = None, None, None
        event_id = markets[0].catalogue.event.id

        for m in markets:
            if m.catalogue.market_name == self.SPREAD_MARKET:
                spreads = self.parse_spreads(m.book)
            elif m.catalogue.market_name == self.TOTALS_MARKET:
                totals = self.parse_totals(m.book)
            elif m.catalogue.market_name == self.MONEYLINE_MARKET:
                moneyline = self.parse_moneyline(m.book)

        return Odds.get_document(event_id, datetime.utcnow(), spreads,
                                 moneyline, totals)
示例#3
0
def get_event_odds(db_session, db_event):

    # Get event info from OddsChecker web site
    oc = OddsChecker()
    info = oc.get_racing_info(event.course.lower().replace(' ', '-'),
                              event.date_time.strftime('%H:%M'), 'winner')

    # Create checkpoint
    db_checkpoint = Checkpoint(event=event,
                               date_time=dt.datetime.now(),
                               source='OddsChecker')
    db_session.add(db_checkpoint)
    for bookie in info['bookies']:
        db_bookie = db_session.query(Bookie).filter_by(
            name=bookie.name).one_or_none()
        if not db_bookie:
            db_bookie = Bookie(name=bookie.name)
            db_session.add(db_bookie)
        db_bookie_event = db_session.query(BookieEvent).filter_by(
            event=db_event, bookie=db_bookie).one_or_none()
        if not db_bookie_event:
            db_bookie_event = BookieEvent(event=db_event,
                                          bookie=db_bookie,
                                          ew_places=bookie.ew_places or 0,
                                          ew_divider=bookie.ew_divider or 1)
            db_session.add(db_bookie_event)
    for horse in info['horses']:
        db_runner = db_session.query(Runner).filter_by(
            event=db_event, name=horse.name).one_or_none()
        if not db_runner:
            db_runner = Runner(event=db_event, name=horse.name)
            db_session.add(db_runner)
        for bookie, odds in horse.odds.items():
            db_bookie = db_session.query(Bookie).filter_by(name=bookie).one()
            db_odds = Odds(checkpoint=db_checkpoint,
                           runner=db_runner,
                           bookie=db_bookie,
                           win=odds,
                           place=0)
            db_session.add(db_odds)
    db_session.commit()
    db_event.last_checkpoint = db_checkpoint
    db_session.commit()
示例#4
0
def fixture_stats():
    """
    Calculate the predicted home and away goals for a fixture
    and store those values in the database.
    """
    fixtures = db.session.query(Fixture).filter_by(completed=False)
    for fixture in fixtures:
        league_stats = db.session.query(League_Stats).filter_by(
            league_id=fixture.league_id).one()

        home_stats = db.session.query(Team_Stats).filter_by(
            team_id=fixture.home_team_id).one()

        away_stats = db.session.query(Team_Stats).filter_by(
            team_id=fixture.away_team_id).one()

        home_goals = (home_stats.home_attack_strength) * (
            away_stats.away_defense_strength) * (league_stats.avg_home_goals)

        away_goals = (away_stats.away_attack_strength) * (
            home_stats.home_defense_strength) * (league_stats.avg_away_goals)

        prediction = db.session.query(Prediction).filter_by(
            fixture_id=fixture.id).first()

        if not prediction:
            prediction = Prediction(fixture)

        prediction.home_goals = home_goals
        prediction.away_goals = away_goals

        prediction = _fixture_probabilities(fixture, prediction)

        odds = db.session.query(Odds).filter_by(fixture_id=fixture.id).first()

        if not odds:
            odds = Odds(fixture)

    db.session.commit()
示例#5
0
                        help='Remove odds for fixtures to this date')
    parser.add_argument('-safe', action='store_true',
                        help='Remove odds only if open/close stats collected')
    return parser.parse_args()


if __name__ == '__main__':
    start_at = time.time()

    args = parse_args()
    fixtures = FixtureModel.get_in_range(args.start,
                                         args.end,
                                         is_lite_output=False)

    ext_ids, not_remove_odds = set(), set()
    for f in fixtures:
        open_line = f.get('open', {}).get('_id')
        close_line = f.get('close', {}).get('_id')

        if args.safe and not (open_line and close_line):
            continue

        if open_line and close_line:
            not_remove_odds.update([str(open_line), str(close_line)])

        ext_ids.update(f['external_ids'])

    res = Odds.remove(list(ext_ids), not_remove_odds)
    print("Cleanup finsihed {}. Execution time: {}"
        .format(res, time.time() - start_at))
示例#6
0
    return parser.parse_args()


if __name__ == '__main__':
    args = parse_args()
    pinnacle = PinnacleApi(args.u, args.p, args.incr)
    pool = gevent.pool.Pool(20)
    new_fixtures = []

    fixtures = pool.spawn(pinnacle.get_fixture, pinnacle.SOCCER_ID,
                          pinnacle.SOCCER_LEAGUES)

    odds = pool.spawn(pinnacle.get_odds, pinnacle.SOCCER_ID,
                      pinnacle.SOCCER_LEAGUES)
    pool.join()

    for fixture in fixtures.value:
        res = FixtureModel.add(fixture)
        if res.upserted_id: new_fixtures.append(fixture)

    if odds.value:
        Odds.insert_many(odds.value)

    pinnacle.close()

    if args.o:
        output = SharedDataObj(odds.value, new_fixtures, None)
        serialized = pickle.dumps(output)
        pinnacle.logger.debug(serialized)
        sys.stdout.buffer.write(serialized)
示例#7
0
    # This will store all the games we create
    games_added = []
    odds_added = []
    pull_id = arrow.now().timestamp

    for event in events:

        game_id = event.rundown_id

        try:
            game = Games.query.get(game_id)
        except Exception:
            print(f'game not found:{game_id}')
            continue

        if not game:
            print(f'game not found:{game_id}')
            continue

        odds = Odds()
        odds.pull_id = pull_id
        odds.game_id = event.rundown_id
        odds.moneyline_id = event.moneyline_id
        odds.moneyline_away = event.moneyline_odds_away
        odds.moneyline_home = event.moneyline_odds_home
        odds_added.append(odds)

    db.session.add_all(games_added)
    db.session.add_all(odds_added)
    db.session.commit()
示例#8
0
 def get_odds_diff(self):
     """ Find open/close line for fixtures which odds are moved """
     ext_ids = [f['external_ids'] for f in self.fixtures]
     flatten_ids = flatten_list(ext_ids)
     self.odds_diff = Odds.get_line_diff(flatten_ids)