Gathering data - Betfair only provides so much

proffs1
Posts: 2
Joined: Fri Jul 28, 2017 9:55 pm

LinusP wrote:
Tue Sep 19, 2017 6:18 am
Glad you got it working, the runners are stored in a list so you have to either create a lookup or loop through. Using the original code I would do the following:

Code: Select all

runner_dict = {runner.selection_id: runner for runner in market_book.market_definition.runners}
If you put that at the top under the for market book loop you can then do the following:

Code: Select all

for runner in market_book.runners:
    runner_def = runner_dict.get(runner.selection_id)
    sort = runner_def.sort_priority 
Wow, thank you very much. It works!
User avatar
1clutch1
Posts: 12
Joined: Sun Feb 12, 2017 2:35 am

Hi There,

Is there any chance you can post the whole code?

I tried to do the same thing but got an error.

I'm not sure what sort variable does as my interpreter is saying its unused?
Thanks,

Code: Select all


    def on_process(self, market_books):
        with open('output.txt', 'a') as output:
            for market_book in market_books:
                runner_dict = {runner.selection_id: runner for runner in market_book.market_definition.runners}
                for runner in market_book.runners:
                    runner_def = runner_dict.get(runner.selection_id)
                    sort = runner_def.sort_priority

                   for runner in market_book.runners:
                    output.write('%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (
                    market_book.publish_time, market_definition_runner.sort_priority, market_book.number_of_active_runners, market_book.market_id, market_book.status, market_book.inplay,
                    runner.selection_id, runner.total_matched, runner.last_price_traded or '',
                    ))


LinusP
Posts: 1871
Joined: Mon Jul 02, 2012 10:45 pm

What error are you getting? Looks like your indentation is off, python can be fussy.
User avatar
1clutch1
Posts: 12
Joined: Sun Feb 12, 2017 2:35 am

I checked the indentation and its fine. It's prob just the way I posted it in. I think I need to edit the base resource.py file to add the call into a dictionary

runner_def = market_def.runners_dict.get((runner.selection_id , runner.handicap, runner.event_name))
AttributeError: 'RunnerBook' object has no attribute 'event_name'

that's the error I'm getting with the following code

Code: Select all

from betfairlightweight import APIClient
from betfairlightweight.streaming import StreamListener, MarketStream
import os

class HistoricalStream(MarketStream):
    def __init__(self, listener):
        super(HistoricalStream, self).__init__(listener)
        print('Time,MarketId,Status,Inplay,sortPriority,runnerName,LastPriceTraded\n')



    def on_process(self, market_books):
        for market_book in market_books:
            for runner in market_book.runners:
                market_def = market_book.market_definition

                runner_def = market_def.runners_dict.get((runner.selection_id , runner.handicap, runner.event_name))
                print(runner_def.name , runner_def.handicap, runner.selection_id, runner.last_price_traded,
                      runner.event_name)


class HistoricalListener(StreamListener):
    def _add_stream(self, unique_id, stream_type):
        if stream_type == 'marketSubscription':
            return HistoricalStream(self)


apiclient = APIClient('aa', 'bb', 'cc')

stream = apiclient.streaming.create_historical_stream(
    directory='/Users/mac/PycharmProjects/xbot/sample2',
    listener=HistoricalListener(max_latency=1e100))
stream.start(async=False)



LinusP
Posts: 1871
Joined: Mon Jul 02, 2012 10:45 pm

Event name is in the market definition:

Code: Select all

market_book.market_definition.event_name 
Post Reply

Return to “Betfair Data”