Beispiel #1
0
def lay_arb_info(sel1, sel2):
    """Get lay arb info"""
    # get the market name and print it out:
    mark1 = database.DBMaster().ReturnMarkets('SELECT * FROM markets WHERE market_id = ?',
                                             (s1.mid,))[0]
    mark2 = database.DBMaster().ReturnMarkets('SELECT * FROM markets WHERE market_id = ?',
                                             (s2.mid,))[0]
    print mark1.name
    print mark2.name
    print olay, s1.name, s1.exid, oback
Beispiel #2
0
    def __init__(self):

        # key to match_cache is event name, value is list of tuples
        # (m1, m2) where m1 and m2 are the matching markets (m1 is the
        # BDAQ market, m2 is the BF market.  The ordering of the
        # tuples is the same as the ordering that appears in the GUI.
        self._match_cache = {}

        # store a dict that maps BDAQ mids to BF mids
        self._BDAQmap_cache = {}

        # and a dict that maps BF mids to BDAQ mids
        self._BFmap_cache = {}

        # and a dict that maps BDAQ mids to their market objects
        self._BDAQ_cache = {}

        # and a dict that maps BF mids to their market objects
        self._BF_cache = {}

        # singleton that controls DB access
        self._dbman = database.DBMaster()

        if self.INITDB:
            self.init_caches_from_DB()
        else:
            self.init_caches_empty()
Beispiel #3
0
    def __init__(self):

        # matching selections keys are bdaqmid, values are
        # [(bdaq_sels, bf_sels)] where bdaq_sels and bf_sels are lists
        # of bdaq and bf selections, ordered as displayed on the BDAQ
        # website.  Instead of dumping everything from the DB into the
        # cache immediately, we add to the cache 'as needed'.
        self._match_cache = {}

        # map a BF sid to a BDAQ sid; note this has an additional
        # layer compared to the BDAQ cache since we must store the
        # market id.
        self._BFmap_cache = {}

        # map a BDAQ sid to selection object.  Note: we do not have an
        # equivalent cache for BF sid's since these do not map
        # uniquely onto selections (an annoying feature of the BF
        # API).  That is, we need both the BF mid and sid to map to a
        # BDAQ sid.
        self._BDAQ_cache = {}

        # we need to access the market store since when we add
        # matching selections, we need to find the BF mid that matches
        # the given BDAQ mid.
        self._mstore = MatchMarketStore.Instance()

        # singleton that controls DB access
        self._dbman = database.DBMaster()

        if self.INITDB:
            self.init_caches_from_DB()
Beispiel #4
0
def save_strategies(bdaqids=[], instantonly=True):
    """
    Using info in the DB, get strategies and save these to pickle
    file.

    bdaqids - list of BDAQ market ids allowed.  If empty, all market
              ids are allowed.
    """

    sgroup = strategy.StrategyGroup()

    # add strategies here
    msels = database.DBMaster().ReturnSelectionMatches(bdaqids)

    if bdaqids:
        # filter out any strategies not involving one of the markets
        # listed in bdaqids
        for ms in list(msels):
            if ms[0].mid not in bdaqids:
                msels.remove(ms)

    # alter the prices so that we get instant opp!!
    #msels[0][0].backprices[0] = (1.50, 10)
    #msels[0][1].layprices[0] = (1.01, 10)
    for m in msels:
        sgroup.add(cxstrategy.CXStrategy(m[0], m[1], instantonly))

    betlog.betlog.debug("Created {0} strategies from DB"\
                        .format(len(sgroup)))

    # pickle the strategy group - the main application will read this
    # pickle file.
    pickle_stratgroup(sgroup)
    return
Beispiel #5
0
    def __init__(self):
        """Class for storing latest prices."""

        # current state of prices
        self._prices = {const.BDAQID: {}, const.BFID: {}}

        # note newprices is not 'private' since it is accessed by the
        # engine; it will be fed to the strategies.
        self.newprices = {const.BDAQID: {}, const.BFID: {}}

        # for writing to database
        self._dbman = database.DBMaster()
Beispiel #6
0
def unpickle_stratgroup():
    """Return StrategyGroup object from pickle file."""

    f = open(PICKPATH, 'rb')
    sgroup = pickle.load(f)
    f.close()

    # this initialises cursor object
    for s in sgroup.strategies:
        if hasattr(s, 'dbman'):
            s.dbman = database.DBMaster()

    return sgroup
Beispiel #7
0
    def __init__(self):

        # the current state of all orders placed since the start of
        # the application.  The keys to each sub-dictionary are the
        # order ids, with values that are the order objects. Note each
        # order object will typically be updated multiple times.
        self._orders = {const.BDAQID: {}, const.BFID: {}}

        # the state of all orders immediately after they were placed.
        # Among other things, these order objects are useful since
        # they have a 'tplaced' attribute.
        self._neworders = {const.BDAQID: {}, const.BFID: {}}

        # store mapping of order ref to time placed.
        self._tplaced = {const.BDAQID: {}, const.BFID: {}}

        # the state of all orders immediately after they were placed.
        self._cancelorders = {const.BDAQID: {}, const.BFID: {}}

        # for writing to database
        self._dbman = database.DBMaster()

        # store dicts of cancelled, updated and new orders placed by
        # the APIs from the latest tick.
        self.latest = [{
            const.BDAQID: {},
            const.BFID: {}
        }, {
            const.BDAQID: {},
            const.BFID: {}
        }, {
            const.BDAQID: {},
            const.BFID: {}
        }]

        # store dict of updated orders from last tick (from querying
        # order status via the API).
        self.latest_updates = {const.BDAQID: {}, const.BFID: {}}

        # store dict used by strategies to seach for order references
        # immediately after order placed.  Since we get the order
        # reference when placing bets on BF, we write to this when
        # placing bets.  But when placing bets on BDAQ, we don't get
        # the order reference, so instead this is written when we call
        # ListOrdersChangedSince.
        self.orders_tosearch = {const.BDAQID: {}, const.BFID: {}}
Beispiel #8
0
    def __init__(self, ex1sel=None, ex2sel=None, instantonly=True):
        """
        ex1sel      - BDAQ selection
        ex2sel      - BF   selection
        instantonly - if True, only look for 'instant' opportunities.
                      This means only 'taking' and not 'making'. 
        """

        # this gives us self.brain, a StateMachine, and self.toplace,
        # a dictionary of orders waiting to be placed (see
        # strategy.py).
        super(CXStrategy, self).__init__()
        self.sel1 = ex1sel
        self.sel2 = ex2sel

        # if this is true, we will only look for 'instant'
        # opportunities, when we can back and lay simultaneously.
        self.ionly = instantonly

        # database interface
        self.dbman = database.DBMaster()

        # add states
        noopp_state = CXStateNoOpp(self)
        instantopp_state = CXStateInstantOpp(self)
        opp_state = CXStateOpp(self)
        lay_placed_state = CXStateLayPlaced(self)
        back_placed_state = CXStateLayPlaced(self)
        both_placed_state = CXStateBothPlaced(self)
        lay_matched_state = CXStateLayMatched(self)
        back_matched_state = CXStateBackMatched(self)
        both_matched_state = CXStateBothMatched(self)

        self.brain.add_state(noopp_state)
        self.brain.add_state(instantopp_state)
        self.brain.add_state(opp_state)
        self.brain.add_state(lay_placed_state)
        self.brain.add_state(back_placed_state)
        self.brain.add_state(both_placed_state)
        self.brain.add_state(lay_matched_state)
        self.brain.add_state(back_matched_state)
        self.brain.add_state(both_matched_state)

        # initialise into noopp state
        self.brain.set_state(noopp_state.name)
Beispiel #9
0
    def __init__(self, deltat):
        """deltat is time between price refreshes in seconds."""

        self.clock = betman.all.clock.Clock(deltat)
        self.dbman = database.DBMaster()

        self.load_strategies()

        # market ids for all strategies (for updating prices)
        self.marketids = self.stratgroup.get_marketids()

        # we store selection objects as a dictionary of dictionaries.
        # This contains the selection objects (e.g. a particular
        # horse), and the selection objects contain the current price,
        # hence the name.
        self.prices = {const.BDAQID: {}, const.BFID: {}}

        # orders for both exchanges
        self.orders = {const.BDAQID: {}, const.BFID: {}}

        # call the API functions to refresh prices etc.
        self.on_startup()
Beispiel #10
0
import matplotlib.pyplot as plt
from betman import database

dbman = database.DBMaster()

mms = dbman.return_market_matches()

bdaqmids = [m[0].id for m in mms]

# load all time series data for all runners in both markets (last pair only).
mymid = 4028996
# get all selection matches for this mid
smatches = dbman.return_selection_matches([mymid])

fig1 = plt.figure()
fig2 = plt.figure()
ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)

for (s1, s2) in zip(smatches[0], smatches[1]):
    # time series data from betdaq
    pdata = dbman.return_selections(
        'select * from histselections where exchange_id = ? '
        'and market_id = ? and selection_id = ?', (s2.exid, s2.mid, s2.id))
    # back price
    line, = ax1.plot_date([p.tstamp for p in pdata],
                          [p.padback[0][0] for p in pdata],
                          fmt='-')
    col = line.get_color()
    # lay price
    ax1.plot_date([p.tstamp for p in pdata], [p.padlay[0][0] for p in pdata],