Skip to content

Commit

Permalink
Merge pull request #293 from liampauling/task/example-improvements
Browse files Browse the repository at this point in the history
historical streaming example updated to use generator
  • Loading branch information
liampauling committed Apr 23, 2020
2 parents ec132f0 + a2c3245 commit 7875604
Showing 1 changed file with 56 additions and 49 deletions.
105 changes: 56 additions & 49 deletions examples/examplestreaminghistorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import betfairlightweight
from betfairlightweight import StreamListener
from betfairlightweight.streaming.stream import MarketStream

"""
Data needs to be downloaded from:
Expand All @@ -15,57 +14,65 @@
# create trading instance (don't need username/password)
trading = betfairlightweight.APIClient("username", "password")

# create listener
listener = StreamListener(max_latency=None)

class HistoricalStream(MarketStream):
# create custom listener and stream

def __init__(self, listener):
super(HistoricalStream, self).__init__(listener)
with open("output.txt", "w") as output:
output.write("Time,MarketId,Status,Inplay,SelectionId,LastPriceTraded\n")

def on_process(self, market_books):
with open("output.txt", "a") as output:
for market_book in market_books:
for runner in market_book.runners:

# how to get runner details from the market definition
market_def = market_book.market_definition
runners_dict = {
(runner.selection_id, runner.handicap): runner
for runner in market_def.runners
}
runner_def = runners_dict.get(
(runner.selection_id, runner.handicap)
)
# create historical stream (update directory to your file location)
stream = trading.streaming.create_historical_generator_stream(
directory="/tmp/BASIC-1.132153978", listener=listener,
)

output.write(
"%s,%s,%s,%s,%s,%s\n"
% (
market_book.publish_time,
market_book.market_id,
market_book.status,
market_book.inplay,
runner.selection_id,
runner.last_price_traded or "",
# create generator
gen = stream.get_generator()

# print marketBooks
for market_books in gen():
for market_book in market_books:
print(market_book)

# print based on seconds to start
for market_books in gen():
for market_book in market_books:
seconds_to_start = (
market_book.market_definition.market_time - market_book.publish_time
).total_seconds()
if seconds_to_start < 100:
print(market_book.market_id, seconds_to_start, market_book.total_matched)

# print winner details once market is closed
if market_book.status == "CLOSED":
for runner in market_book.runners:
if runner.status == "WINNER":
print(
"{0}: {1} with sp of {2}".format(
runner.status, runner.selection_id, runner.sp.actual_sp
)
)

# record prices to a file
with open("output.txt", "w") as output:
output.write("Time,MarketId,Status,Inplay,SelectionId,LastPriceTraded\n")

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


# create listener
listener = HistoricalListener(max_latency=None)

# create historical stream, update directory to file location
stream = trading.streaming.create_historical_stream(
directory="/Users/liampauling/Downloads/Sites 3/xdw/api/c0a022d4-3460-41f1-af12-a0b68b136898/BASIC-1.132153978",
listener=listener,
)

# start stream
stream.start()
for market_books in gen():
for market_book in market_books:
with open("output.txt", "a") as output:
for runner in market_book.runners:
# how to get runner details from the market definition
market_def = market_book.market_definition
runners_dict = {
(runner.selection_id, runner.handicap): runner
for runner in market_def.runners
}
runner_def = runners_dict.get((runner.selection_id, runner.handicap))

output.write(
"%s,%s,%s,%s,%s,%s\n"
% (
market_book.publish_time,
market_book.market_id,
market_book.status,
market_book.inplay,
runner.selection_id,
runner.last_price_traded or "",
)
)

0 comments on commit 7875604

Please sign in to comment.