Пример #1
0
 def update_state(self):
     self.previous_state = deepcopy(self.state)
     if self.state['updatedDate'] < helpers.get_start_of_day():
         self.logger.info('Updating state at beginning of new day.')
         if helpers.strategy_won_yesterday(self.reference):
             self.logger.info('Won yesterday.')
             if self.state['weightLadderPosition'] > 0:
                 self.state['weightLadderPosition'] -= 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Reduced weighting to %sx.' % weight)
             self.state['daysAtMaxWeight'] = 0
             self.logger.info('Reset days at maximum weight to 0.')
         else:
             self.logger.info('Lost yesterday.')
             if self.state['weightLadderPosition'] < (
                     len(settings.weight_ladder) - 1):
                 self.state['weightLadderPosition'] += 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Increased weighting to %sx.' % weight)
             else:
                 self.state['daysAtMaxWeight'] += 1
                 self.logger.info(
                     'Incremented days at maximum weight to %s.' %
                     self.state['daysAtMaxWeight'])
         betbot_db.strategy_repo.upsert(self.state)
Пример #2
0
 def create_bets(self, market=None, market_book=None):
     bets = []
     if market and market_book:
         if not self.state['active']:
             self.logger.info('Strategy is not active, no bets generated.')
             return bets
         if self.state['stopLoss']:
             self.logger.info('Stop loss triggered, no more bets today.')
             return bets
         self.update_state()
         runner = helpers.get_favourite(market_book)
         if runner:
             adjusted_last_price = runner['lastPriceTraded'] - 1
             if self.state['lostStakeSum'] == 0:
                 stake = settings.minimum_stake
             else:
                 stake = (self.state['lostStakeSum'] +
                          adjusted_last_price) / adjusted_last_price
             weight = helpers.get_weight_by_ladder_position(
                 self.state['weightLadderPosition'])
             price = helpers.get_back_limit_price(runner, stake * weight)
             if price < 2:
                 new_bet = {
                     'customerOrderRef':
                     helpers.get_unique_ref(self.reference),
                     'selectionId': runner['selectionId'],
                     'handicap': 0,
                     'side': 'BACK',
                     'orderType': 'LIMIT',
                     'limitOrder': {
                         'size': stake * weight,
                         'price': price,
                         'persistenceType': 'LAPSE',
                         'timeInForce': 'FILL_OR_KILL'
                     }
                 }
                 bets.append(new_bet)
             else:
                 self.state = deepcopy(self.previous_state)
                 betbot_db.strategy_repo.upsert(self.state)
                 self.logger.info(
                     "No bet generated, favourite is not odds-on (<2 on Betfair)."
                 )
                 self.logger.info("Reverted to previous strategy state.")
         else:
             self.state = deepcopy(self.previous_state)
             betbot_db.strategy_repo.upsert(self.state)
             self.logger.info("No bet generated, no favourite identified.")
             self.logger.info("Reverted to previous strategy state.")
     else:
         msg = 'Failed to create bets for strategy %s, no market/book provided' % self.reference
         raise Exception(msg)
     return bets
Пример #3
0
 def create_bets(self, market=None, market_book=None):
     bets = []
     if market and market_book:
         if not self.state['active']:
             self.logger.info('Strategy is not active, no bets generated.')
         else:
             self.update_state()
             stake = helpers.get_stake_by_ladder_position(
                 0)  # fixed staking plan
             weight = helpers.get_weight_by_ladder_position(
                 self.state['weightLadderPosition'])
             runner = helpers.get_favourite(market_book)
             if runner:
                 new_bet = {
                     'customerOrderRef':
                     helpers.get_unique_ref(self.reference),
                     'selectionId': runner['selectionId'],
                     'handicap': 0,
                     'side': 'LAY',
                     'orderType': 'LIMIT',
                     'limitOrder': {
                         'size':
                         stake * weight,
                         'price':
                         helpers.get_lay_limit_price(
                             runner, stake * weight),
                         'persistenceType':
                         'LAPSE',
                         'timeInForce':
                         'FILL_OR_KILL'
                     }
                 }
                 bets.append(new_bet)
             else:
                 self.state = deepcopy(self.previous_state)
                 betbot_db.strategy_repo.upsert(self.state)
                 self.logger.info(
                     "No bet generated, no favourite identified.")
                 self.logger.info("Reverted to previous strategy state.")
     else:
         msg = 'Failed to create bets for strategy %s, no market/book provided' % self.reference
         raise Exception(msg)
     return bets
Пример #4
0
 def update_state(self):
     self.previous_state = deepcopy(self.state)
     if self.state['updatedDate'] < helpers.get_start_of_day(
     ):  # Once a day
         self.logger.info('Updating state at beginning of new day.')
         if helpers.strategy_won_yesterday(self.reference):
             self.logger.info('Won yesterday.')
             if self.state['weightLadderPosition'] > 0:
                 self.state['weightLadderPosition'] -= 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Reduced weighting to %sx.' % weight)
             self.state['daysAtMaxWeight'] = 0
             self.logger.info('Reset days at maximum weight to 0.')
         else:
             self.logger.info('Lost yesterday.')
             if self.state['weightLadderPosition'] < (
                     len(settings.weight_ladder) - 1):
                 self.state['weightLadderPosition'] += 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Increased weighting to %sx.' % weight)
             else:
                 self.state['daysAtMaxWeight'] += 1
                 self.logger.info(
                     'Incremented days at maximum weight to %s.' %
                     self.state['daysAtMaxWeight'])
         # Regardless of win or loss yesterday...
         self.state['stakeLadderPosition'] = 0
         self.logger.info('Reset stake ladder.')
         self.state['betsAtMaxStake'] = 0
         self.logger.info('Reset bets at maximum stake to 0.')
         self.state['stopLoss'] = False
         self.logger.info('Removed any stop loss from the previous day.')
     else:  # Once a race
         if helpers.strategy_won_last_market_today(self.reference):
             self.logger.info('Won last race.')
             self.state['stakeLadderPosition'] = 0
             self.logger.info('Reset stake ladder.')
             self.state['betsAtMaxStake'] = 0
             self.logger.info('Reset bets at maximum stake to 0.')
         else:
             self.logger.info('Lost last race.')
             if self.state['stakeLadderPosition'] < (
                     len(settings.stake_ladder) - 1):
                 self.state['stakeLadderPosition'] += 1
                 stake_multiplier = settings.stake_ladder[
                     self.state['stakeLadderPosition']]
                 self.logger.info('Incremented stake ladder to %sx.' %
                                  stake_multiplier)
             else:
                 self.state['betsAtMaxStake'] += 1
                 self.logger.info(
                     'Incremented bets at maximum stake to %s.' %
                     self.state['betsAtMaxStake'])
                 if self.state['betsAtMaxStake'] == 1:
                     self.state['stakeLadderPosition'] = 0
                     self.state['betsAtMaxStake'] = 0
                     self.logger.info(
                         'Previous bet was at maximum stake, reset stake ladder.'
                     )
     betbot_db.strategy_repo.upsert(self.state)
Пример #5
0
 def update_state(self):
     self.previous_state = deepcopy(self.state)
     if self.state['updatedDate'] < helpers.get_start_of_day(
     ):  # Once a day
         self.logger.info('Updating state at beginning of new day.')
         if helpers.strategy_won_yesterday(self.reference):
             self.logger.info('Won yesterday.')
             if self.state['weightLadderPosition'] > 0:
                 self.state['weightLadderPosition'] -= 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Reduced weighting to %sx.' % weight)
             self.state['daysAtMaxWeight'] = 0
             self.logger.info('Reset days at maximum weight to 0.')
         else:
             self.logger.info('Lost yesterday.')
             if self.state['weightLadderPosition'] < (
                     len(settings.weight_ladder) - 1):
                 self.state['weightLadderPosition'] += 1
                 weight = helpers.get_weight_by_ladder_position(
                     self.state['weightLadderPosition'])
                 self.logger.info('Increased weighting to %sx.' % weight)
             else:
                 self.state['daysAtMaxWeight'] += 1
                 self.logger.info(
                     'Incremented days at maximum weight to %s.' %
                     self.state['daysAtMaxWeight'])
         # Regardless of win or loss yesterday...
         self.state['sequentialLosses'] = 0
         self.logger.info('Reset sequential losses to 0.')
         self.state['lostStakeSum'] = 0
         self.logger.info('Reset lost stake sum to 0.')
         self.state['stopLoss'] = False
         self.logger.info('Removed any stop loss from the previous day.')
     else:  # Once a race
         if helpers.strategy_won_last_market_today(self.reference):
             self.logger.info('Won last race.')
             self.state['sequentialLosses'] = 0
             self.logger.info('Reset sequential losses to 0.')
             self.state['lostStakeSum'] = 0
             self.logger.info('Reset lost stake sum to 0.')
         else:
             self.logger.info('Lost last race.')
             self.state['sequentialLosses'] += 1
             self.logger.info('Incremented sequential losses by 1.')
             if self.state['sequentialLosses'] == 3:
                 self.state['stopLoss'] = True
                 self.logger.info(
                     '5 races lost in a row, triggering stop loss.')
             last_order = betbot_db.order_repo.get_latest_today_by_strategy(
                 self.reference)
             if last_order:
                 last_instruction = betbot_db.instruction_repo.get_by_id(
                     last_order['betId'])
                 if last_instruction:
                     last_stake = last_instruction['instruction'][
                         'limitOrder']['size']
                     self.state['lostStakeSum'] += last_stake
                     self.logger.info('Incremented lost stake sum by £%s' %
                                      last_stake)
                 else:
                     self.logger.info(
                         'Failed to increment last stake sum, no previous instruction found.'
                     )
             else:
                 self.logger.info(
                     'Last stake sum not incremented, no previous order found.'
                 )
     betbot_db.strategy_repo.upsert(self.state)