Ejemplo n.º 1
0
def handle_IndexError(betfairClient, exchangeId, marketId):
    """IndexError caught check the market status is active.
    Return false if it is not active, else true
    """
    logger.warning('IndexError caught, check marketStatus is ACTIVE')
    market_price = qwertymarket.getMarketPricesCompressed(betfairClient,
                                                        exchangeId,
                                                        marketId)
    market_status = qwertymarket.marketStatus(market_price)
    if market_status != 'ACTIVE':
        time.sleep(5)
        market_price = qwertymarket.getMarketPricesCompressed(betfairClient,
                                                            exchangeId,
                                                            marketId)
        market_status = qwertymarket.marketStatus(market_price)
        if market_status != 'ACTIVE':
            cleanUp.clean_up(betfairClient, marketId)
            return False
        else:
            return True
Ejemplo n.º 2
0
def main():
    """The brains of the bot
    Finds all the races
    """
    betfairClient = bfclient.BfClient()
    eventResponse = None
    race_list = None
    cardGenTime = 0
    login_timer = 0

    while True:
        login_rsp = auth.login(betfairClient)
        # Generate the list of available races. Limit the result to 100
        (race_list, cardGenTime) = gen_race_list(cardGenTime, race_list, betfairClient)
        # get the exchangeId, marketId for the next race and race_queue
        # refactor - turn into class, return object.
        (exchangeId, marketId, race_queue, race_list) = get_next_race(race_list)
        # determine race status, proceed if it is active.
        market_price = qwertymarket.getMarketPricesCompressed(betfairClient,
                                                            exchangeId,
                                                            marketId)
        market_status = qwertymarket.marketStatus(market_price)
        if market_status == 'CLOSED':
            logger.debug('Oops market is closed, finding the next race/market')
            time.sleep(2)
            continue
        elif market_status == 'SUSPENDED':
            logger.debug('Market is now suspended, finding the next race/market')
            time.sleep(5)
            continue
        # Great the next race is active, lets find out when it starts
        market_info = qwertymarket.getMarketInfo(betfairClient,
                                                exchangeId,
                                                marketId)
        TTL = qwertymarket.timeTillRaceStarts(market_info)
        logger.info('Time till live: %s', TTL)
        if TTL > 240: #if time till race is more than 5 minutes away
            handle_long_ttl(cardGenTime, race_list, market_info)
        elif market_price.marketPrices.delay > 0:
            logger.info("-------------Market is in-play---------------")
            time.sleep(10)
            continue
        # nested while true, why? Because we don't want it to change races if it's active.
        while True:
            try:
                collectData.collectData(betfairClient, exchangeId,
                                        marketId)
                timeId = collectData.Datastore[marketId]['timeId']
                IDgap = collectData.Datastore[marketId]['IDgap']
                if IDgap == 0:
                    time.sleep(2)
                    logger.debug('IDgap is zero')
                    continue #don't run anything else
            except IndexError:
                if handle_IndexError(betfairClient, exchangeId, marketId):
                    continue
                else:
                    break
            except ZeroDivisionError:
                logger.warning('Zero Division Exception caught.')
                break
            except bfpy.bferror.BfNetworkError:
                logger.error("Network timed out...retrying...")
                continue
            except Exception, e:
                logger.exception('Collecting data at timeId: %s', timeId)
                raise e
            try:
                play.inPlay(betfairClient, exchangeId, marketId, timeId)
                play.pastPlay(betfairClient, exchangeId, marketId, timeId)
            except play.InPlayClause as e:
                logger.debug('In Play is greater than 30 seconds, look for the next race')
                break
            except Exception as e:
                logger.exception('Running inPlay or pastPlay')
                raise e
            try:
                for fillInId in range(timeId-IDgap+1, timeId+1):
                    for selectionId in collectData.Datastore[marketId]['runnerKeys']:
                        if fillInId != timeId:
                            collectData.fillInMissingData(marketId, selectionId,
                                                        fillInId, timeId)
                        if fillInId >= 30:
                            selection.calculateTPS(marketId, selectionId, fillInId)
            except IndexError:
                if handle_IndexError(betfairClient, exchangeId, marketId):
                    continue
                else:
                    break
            except ZeroDivisionError:
                logger.warning('Zero Division Exception caught.')
                break
            except Exception, e:
                logger.exception('Filling missing data or calculating TPS at timeId: %s', timeId)
                raise e
            try:
                if timeId > 1: selection.selectionCriteria(marketId, timeId)
                for selectionId in collectData.Datastore[marketId]['selectedRunnerIds']:
                    if 'max30BP' not in collectData.Datastore[marketId][selectionId][0].keys():
                        for backCalc in range(0, timeId - IDgap+1):
                            analysis.analyse(marketId, selectionId, backCalc)
                            if backCalc == timeId - IDgap:
                                logger.info('Successful back analysis of %s to timeId %s',
                                            collectData.Datastore[marketId][selectionId]['HorseName'],
                                            backCalc)
                                collectData.Datastore[marketId][selectionId]['backCalc'] = backCalc
                    for fillInId in range(timeId-IDgap+1, timeId+1):
                        analysis.analyse(marketId, selectionId, fillInId)
            except IndexError:
                if handle_IndexError(betfairClient, exchangeId, marketId):
                    continue
                else:
                    break
            except ZeroDivisionError:
                logger.warning('Zero Division Exception caught.')
                break
            except Exception, e:
                logger.exception('Error: Running Analysis or selecting horses at timeId: %s',
                                timeId)
                logger.exception("timeId: %s", timeId)
                raise e
Ejemplo n.º 3
0
     if handle_IndexError(betfairClient, exchangeId, marketId):
         continue
     else:
         break
 except ZeroDivisionError:
     logger.warning('Zero Division Exception caught.')
     break
 except Exception, e:
     logger.exception('Error: Running Analysis or selecting horses at timeId: %s',
                     timeId)
     logger.exception("timeId: %s", timeId)
     raise e
 market_price = qwertymarket.getMarketPricesCompressed(betfairClient,
                                                         exchangeId,
                                                         marketId)
 market_status = qwertymarket.marketStatus(market_price)
 if market_status != 'ACTIVE':
     time.sleep(10)
     market_price = qwertymarket.getMarketPricesCompressed(betfairClient,
                                                             exchangeId,
                                                             marketId)
     market_status = qwertymarket.marketStatus(market_price)
     if market_status != 'ACTIVE':
         cleanUp.clean_up(betfairClient, marketId)
         break
     else:
         continue
 if collectData.Datastore[marketId]['selectedRunnerIds']:
     logger.debug('Selected RunnerIds: %s',
                 collectData.Datastore[marketId]['selectedRunnerIds'])
     if timeId >= 30:
Ejemplo n.º 4
0
 def test_market_status(self):
     self.status = ['ACTIVE','SUSPENDED','CLOSED']
     self.market_price = getMarketPricesCompressed(self.bf_client, self.exchangeId, self.marketId)
     self.market_status = marketStatus(self.market_price)
     self.assertIn(self.market_status, self.status)