def test_get_price(self): self.assertEqual( utils.get_price([{ "price": 12, "size": 120 }, { "price": 34, "size": 120 }], 0), 12, ) self.assertEqual( utils.get_price([{ "price": 12, "size": 120 }, { "price": 34, "size": 120 }], 1), 34, ) self.assertIsNone( utils.get_price([{ "price": 12, "size": 120 }, { "price": 34, "size": 120 }], 3)) self.assertIsNone(utils.get_price([], 3))
def process_market_book(self, market, market_book): # get lowest price runner prices = [(r.selection_id, r.last_price_traded) for r in market_book.runners if r.status == "ACTIVE"] prices.sort(key=lambda tup: tup[1]) selection_id = prices[0][0] if prices[0][1] > 3: return for runner in market_book.runners: if runner.selection_id == selection_id: # lay at current best lay price lay = get_price(runner.ex.available_to_lay, 0) trade = Trade( market_book.market_id, runner.selection_id, runner.handicap, self, ) order = trade.create_order( side="LAY", order_type=LimitOrder(lay, self.context["stake"]), ) self.place_order(market, order)
def process_market_book(self, market, market_book): # betconnect_client = self.clients.get_client( # clients.ExchangeType.BETCONNECT, username="******" # ) fixture_id = self.context["betconnect"]["active_fixtures_lookup"][( market.venue, market.market_start_datetime)] market_id = self.context["betconnect"]["active_markets"][fixture_id][ 0].source_market_id # todo selections_lookup = { s.name: s for s in self.context["betconnect"]["active_selections"][market_id] } runner_names = { r.selection_id: r.runner_name for r in market.market_catalogue.runners } for runner in market_book.runners: if runner.status == "ACTIVE": # betfair runner_name = runner_names[runner.selection_id] best_back_price = utils.get_price(runner.ex.available_to_back, 0) # betconnect selection = selections_lookup[runner_name] max_price = selection.max_price diff = (1 / max_price) - (1 / best_back_price) print(runner_name, best_back_price, max_price, round(diff, 3))
def process_market_book(self, market, market_book): seconds_since_start = (market_book.publish_time_epoch - market.context["start_time"]) / 1e3 # get price dict from market context if "price" not in market.context: market.context["price"] = defaultdict(list) price_dict = market.context["price"] for runner in market_book.runners: # store latest prices/sizes back_price = get_price(runner.ex.available_to_back, 0) back_size = get_size(runner.ex.available_to_back, 0) if back_price: price_dict[runner.selection_id].append( (market_book.publish_time_epoch, back_price, back_size)) # check trigger if trigger(price_dict[runner.selection_id]): runner_context = self.get_runner_context( market.market_id, runner.selection_id, runner.handicap) if runner_context.live_trade_count == 0: # back at current best back price back = get_price(runner.ex.available_to_back, 0) if back is None: continue # create trade trade = Trade( market_book.market_id, runner.selection_id, runner.handicap, self, ) # create order order = trade.create_order( side="BACK", order_type=LimitOrder(back, self.context["stake"]), notes=OrderedDict( seconds_since_start=round(seconds_since_start, 2), back_size=back_size, ), ) # place order for execution market.place_order(order)
def process_market_book(self, live_market, market_book): with open(self.file_directory, "a") as f: writer = csv.DictWriter(f, fieldnames=HEADERS) for runner in market_book.runners: writer.writerow({ "market_id": market_book.market_id, "publish_time": market_book.publish_time, "status": market_book.status, "inplay": market_book.inplay, "selection_id": runner.selection_id, "last_price_traded": runner.last_price_traded, "back": get_price(runner.ex.available_to_back, 0), "lay": get_price(runner.ex.available_to_lay, 0), })
def process_market_book(self, market, market_book): for runner in market_book.runners: if runner.last_price_traded < 2: lay = get_price(runner.ex.available_to_lay, 0) trade = Trade( market_book.market_id, runner.selection_id, runner.handicap, self, ) order = trade.create_order( side="LAY", order_type=LimitOrder(lay, 2.00), ) self.place_order(market, order)
def process_market_book(self, market, market_book): with market.transaction() as t: for runner in market_book.runners: if runner.status == "ACTIVE": back = get_price(runner.ex.available_to_back, 0) runner_context = self.get_runner_context( market.market_id, runner.selection_id) if runner_context.trade_count == 0: trade = Trade( market_book.market_id, runner.selection_id, runner.handicap, self, ) order = trade.create_order( side="BACK", order_type=LimitOrder(back, 2.00), ) t.place_order(order)
def process_market_book(self, market, market_book): # get lowest price runner prices = [(r.selection_id, r.last_price_traded) for r in market_book.runners if r.status == "ACTIVE" and r.last_price_traded] if not prices: return prices.sort(key=lambda tup: tup[1]) selection_id = prices[0][0] if prices[0][1] > 3: return # calculate market underround for later analysis underround = _calculate_underround(market_book.runners) for runner in market_book.runners: if runner.selection_id == selection_id: runner_context = self.get_runner_context( market.market_id, runner.selection_id, runner.handicap) if runner_context.live_trade_count == 0: # lay at current best lay price lay = get_price(runner.ex.available_to_lay, 0) # create trade trade = Trade( market_book.market_id, runner.selection_id, runner.handicap, self, ) # create order order = trade.create_order( side="LAY", order_type=LimitOrder(lay, self.context["stake"]), notes=OrderedDict(underround=round(underround, 4)), ) # place order for execution market.place_order(order)
def _calculate_underround(runners: list) -> float: return sum([ 1 / get_price(r.ex.available_to_lay, 0) for r in runners if r.ex.available_to_lay ])