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 setUp(self) -> None: logging.disable(logging.CRITICAL) mock_client = mock.Mock(paper_trade=False) self.mock_strategy = mock.Mock(client=mock_client) self.notes = collections.OrderedDict({"trigger": 123}) self.trade = Trade( "1.234", 567, 1.0, self.mock_strategy, self.notes, 12, 34, )
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): # process marketBook object for runner in market_book.runners: if (runner.status == "ACTIVE" and runner.last_price_traded and runner.selection_id == 11982403): trade = Trade( market_id=market_book.market_id, selection_id=runner.selection_id, handicap=runner.handicap, strategy=self, ) order = trade.create_order(side="LAY", order_type=LimitOrder(price=1.01, size=2.00)) self.place_order(market, order)
def process_market_book(self, market, market_book): for runner in market_book.runners: 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="LAY", order_type=MarketOnCloseOrder(100.00), ) self.place_order(market, order)
def setUp(self) -> None: logging.disable(logging.CRITICAL) self.mock_strategy = mock.Mock() self.mock_fill_kill = mock.Mock() self.mock_offset = mock.Mock() self.mock_green = mock.Mock() self.notes = collections.OrderedDict({"trigger": 123}) self.trade = Trade( "1.234", 567, 1.0, self.mock_strategy, self.notes, self.mock_fill_kill, self.mock_offset, self.mock_green, )
def _create_order_from_current(self, client, current_order, market): strategy_name_hash = current_order.customer_order_ref[: STRATEGY_NAME_HASH_LENGTH] order_id = current_order.customer_order_ref[STRATEGY_NAME_HASH_LENGTH + 1:] # get strategy strategy = self.flumine.strategies.hashes.get(strategy_name_hash) if strategy is None: logger.warning( "OrdersMiddleware: Strategy not available to create order {0}". format(order_id), extra={ "bet_id": current_order.bet_id, "market_id": current_order.market_id, "customer_strategy_ref": current_order.customer_strategy_ref, "customer_order_ref": current_order.customer_order_ref, "strategy_name": str(strategy), "client_username": client.username, }, ) return # add trade/order trade = Trade( market.market_id, current_order.selection_id, current_order.handicap, strategy, ) order = trade.create_order_from_current(client, current_order, order_id) market.blotter[order.id] = order runner_context = strategy.get_runner_context(*order.lookup) runner_context.place(trade.id) logger.info( "OrdersMiddleware: New order trade created", extra={ "bet_id": current_order.bet_id, "market_id": current_order.market_id, "customer_strategy_ref": current_order.customer_strategy_ref, "customer_order_ref": current_order.customer_order_ref, "strategy_name": str(strategy), "client_username": client.username, }, ) return order
def process_market_book(self, market, market_book): with market.transaction() as t: for runner in market_book.runners: if runner.status == "ACTIVE": 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(1000, 2.00), ) t.place_order(order)
def process_market_book(self, market, market_book): with market.transaction(client=self.context["client"]) 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): 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, 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 process_market_book(self, market, market_book): # get context selections = self.context["selections"] for selection in selections: if market_book.market_id == selection["market_id"]: for runner in market_book.runners: runner_context = self.get_runner_context( market.market_id, runner.selection_id, runner.handicap) if runner_context.trade_count > 0: continue if (runner.status == "ACTIVE" and runner.selection_id == selection["selection_id"]): trade = Trade( market_id=market_book.market_id, selection_id=runner.selection_id, handicap=runner.handicap, strategy=self, ) order = trade.create_order( side=selection["side"], order_type=LimitOrder(price=1.01, size=selection["liability"]), ) market.place_order(order)