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 self.update_state() if self.state['stop']: self.logger.info( 'Group stop triggered, no more bets in this race grouping.' ) return bets runner = helpers.get_favourite(market_book) if runner: stake = self.state['startingStake'] * self.state[ 'stakeLadder'][self.state['groupPosition']] price = helpers.get_back_limit_price(runner, stake) if not self.state['stop']: if 2.0 <= price <= 3.0: new_bet = { 'customerOrderRef': helpers.get_unique_ref(self.reference), 'selectionId': runner['selectionId'], 'handicap': 0, 'side': 'BACK', 'orderType': 'LIMIT', 'limitOrder': { 'size': stake, '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 price is not between 1-2 (2-3 on Betfair)." ) self.logger.info( "Reverted to previous strategy state.") else: self.logger.info( "No bet generated, group stop in place due to group win." ) 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
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
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
def simulate_place_bets(self, market=None, strategy_bets=None, strategy_ref=''): self.logger.debug('Simulating receipt of instruction reports.') resp = {'status': 'SUCCESS', 'instructionReports': []} if strategy_bets: for strategy_bet in strategy_bets: instruction_report = { 'status': 'SUCCESS', 'instruction': strategy_bet, 'placedDate': datetime.utcnow(), 'betId': helpers.get_unique_ref(strategy_ref), 'averagePriceMatched': strategy_bet['limitOrder']['price'], 'sizeMatched': strategy_bet['limitOrder']['size'], 'marketId': market['marketId'], 'marketStartTime': market['marketStartTime'], 'strategyRef': strategy_ref, 'orderStatus': 'EXECUTION_COMPLETE' if random.randrange(1, 10) <= 8 else 'EXPIRED', 'customerOrderRef': strategy_bet['customerOrderRef'], 'live': False } resp['instructionReports'].append(instruction_report) return resp