def delta_update_statistics(self, cleared_orders): self.logger.info('Doing a delta strategy statistics update.') strategy_pnls = {} for cleared_order in cleared_orders: strategy_ref = cleared_order['customerStrategyRef'] if strategy_ref in strategy_pnls: strategy_pnls[strategy_ref] += cleared_order['profit'] else: strategy_pnls[strategy_ref] = cleared_order['profit'] total_statistic = betbot_db.statistic_repo.get_by_reference('TOTALS') for strategy_ref, pnl in strategy_pnls.items(): statistics = betbot_db.statistic_repo.get_by_reference( strategy_ref) if statistics['updatedDate'] < helpers.get_start_of_day(): statistics['dailyPnL'] = 0.0 statistics['dailyPnL'] += pnl total_statistic['dailyPnL'] += pnl statistics['weeklyPnL'] += pnl total_statistic['weeklyPnL'] += pnl statistics['monthlyPnL'] += pnl total_statistic['monthlyPnL'] += pnl statistics['yearlyPnL'] += pnl total_statistic['yearlyPnL'] += pnl statistics['lifetimePnL'] += pnl total_statistic['lifetimePnL'] += pnl betbot_db.statistic_repo.upsert(statistics) betbot_db.statistic_repo.upsert(total_statistic)
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)
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)
def get_daily_pnls(self): return self.get_pnls(helpers.get_start_of_day())
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)