def create_runner_info(self): get_logger().info("checking for runner under market", event_id=self.event_id) markets = get_api().get_markets(self.event_id, "MATCH_ODDS") get_logger().info("got markets", number_markets=len(markets), event_id=self.event_id) return get_api().get_runners(markets)
def create_runner_info(self): get_logger().info("checking for runner under market", event_id=self.event_id) markets = get_api().get_markets(self.event_id, text_query="OVER_UNDER_25") get_logger().info("got markets", number_markets=len(markets), event_id=self.event_id) return get_api().get_runners(markets)
def ask_for_price(self): self.current_back, self.current_lay, self.current_size, self.status, self.current_orders = \ get_api().get_price_market_selection(self.market_id, self.selection_id) while self.status == "SUSPENDED": sleep(10) self.current_back, self.lay, self.current_size, self.status, self.current_orders = \ get_api().get_price_market_selection(self.market_id, self.selection_id) if self.status == "ACTIVE": return True else: return False
def quote(self, price, size, side): tradable = self.ask_for_price() if not tradable: get_logger("was not able to retrieve prices", status=self.status) return False betfair_position = self.get_betfair_matches(side) get_logger().info("position_betfair", betfair_position=betfair_position) betting_size = max(size - betfair_position, 0) well_priced_orders = [] well_priced_position = 0 for order in self.unmatched_order: if order["price"] == price and well_priced_position < betting_size: well_priced_orders.append(order) well_priced_position += order["size"] else: cancellation_report = get_api().cancel_order( self.market_id, order["bet_id"]) difference_position = well_priced_position - betting_size if difference_position > 0: get_api().cancel_order(self.market_id, well_priced_orders[-1]["bet_id"], difference_position) elif difference_position < 0: remaining_size = -difference_position get_logger().info("placing bet", current_price=self.current_back, current_size=self.current_size, price=price, size=size) ref = self.generate_oder_id(self.selection_id) match = get_api().place_bet(price, remaining_size, side.name, self.market_id, self.selection_id, customer_order_ref=ref) bet_id = match["bet_id"] if bet_id is None: get_logger().info("order refused") return False self.add_order_to_db(bet_id, size, price, side.name, match["size"], match["price"], ref, "active") return True
def get_betfair_matches(self, side=None): orders = get_api().get_placed_orders(market_ids=[self.market_id]) matches = [] non_matches = [] market_position = 0 active_bet_ids = [] average_price = 0 for order in orders: if order.selection_id != self.selection_id: continue if (order.customer_order_ref is None) or ( self.customer_ref not in order.customer_order_ref): continue if side is not None: if order.side != side.name: continue match = {} non_match = {} match["bet_id"] = order.bet_id match["price"] = order.average_price_matched match["size"] = order.size_matched match["side"] = order.side market_position += order.size_matched average_price += order.average_price_matched matches.append(match) if order.status == "EXECUTABLE": non_match["bet_id"] = order.bet_id non_match["price"] = order.price_size.price non_match["size"] = order.size_remaining non_match["side"] = order.side non_matches.append(non_match) active_bet_ids.append(order.bet_id) self.add_order_to_db(order.bet_id, order.price_size.size, order.price_size.price, order.side, order.size_matched, order.average_price_matched, order.customer_order_ref, order.status) self.matched_order = matches self.unmatched_order = non_matches self.update_cancelled_orders(active_bet_ids) if side == Side.BACK: self.position_back = market_position self.price_back = average_price if side == Side.LAY: self.position_lay = market_position self.price_lay = average_price return market_position
def update_runner_current_price(self): get_logger().info("retriving prices", event_id = self.event_id) self.prices = get_api().get_runner_prices(self.list_runner) for p in self.prices.values(): if p["lay"] is not None and p["back"] is not None: p["spread"] = 2 * (p["lay"] - p["back"]) / (p["lay"] + p["back"]) * 100 else: p["spread"] = None get_logger().info("updated the prices", event_id = self.event_id) return self.prices
def retrieve_events(self): get_logger().info("fetching events") actual_time = datetime.utcnow() time_from = (actual_time + timedelta(minutes=self.time_filter_from)).strftime('%Y-%m-%dT%H:%M:%S.%f%z') time_to = (actual_time + timedelta(minutes=self.time_filter_to)).strftime('%Y-%m-%dT%H:%M:%S.%f%z') events = get_api().get_events(self.event_id, self.type_ids, self.inplay_only, time_from, time_to ,market_countries=self.market_countries) return events
def place_order(self, price, size, side): ref = self.generate_oder_id(self.selection_id) match = get_api().place_bet(price, size, side, self.market_id, self.selection_id, customer_order_ref=ref) bet_id = match["bet_id"] if bet_id is None: get_logger().info("order refused") return False self.add_order_to_db(bet_id, size, price, side, match["size"], match["price"], ref, "active")
def cancel_all_pending_orders(self): self.get_betfair_matches() for order in self.unmatched_order: get_api().cancel_order(self.market_id, order["bet_id"])