def process_time_range market pair range_start range_end frame_size mo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def process_time_range(market, pair, range_start, range_end, frame_size):
moment_time = range_end
book_qs = OrderBook.objects.filter(created__range=(range_start, range_end),
market=market, pair=pair).iterator()
range_books = list(book_qs)
while moment_time > range_start:
frame_end = moment_time
frame_start = moment_time - frame_size
books = [x for x in range_books if frame_start < x.created < frame_end]
books = sorted(books, key=operator.attrgetter('created'))
if not len(books):
print 'No books in range: %s -> %s' % (frame_start, frame_end)
else:
for book in books:
book.asks_norm = normalize_trades(book.data['asks'])
book.bids_norm = normalize_trades(book.data['bids'])
for trade_amount in (Decimal('0.1'), Decimal(1)):
points = []
for book in books:
points.append(dict(
ask_initial=get_min_ask(book.asks_norm),
ask_shifted=get_ask_shift(book.asks_norm, trade_amount),
bid_initial=get_max_bid(book.bids_norm),
bid_shifted=get_bid_shift(book.bids_norm, trade_amount),
))
frame_data = dict(
ask_initial_start=points[0]['ask_initial'],
ask_initial_high=max(x['ask_initial'] for x in points),
ask_initial_low=min(x['ask_initial'] for x in points),
ask_initial_end=points[-1]['ask_initial'],
ask_shifted_start=points[0]['ask_shifted'],
ask_shifted_high=max(x['ask_shifted'] for x in points),
ask_shifted_low=min(x['ask_shifted'] for x in points),
ask_shifted_end=points[-1]['ask_shifted'],
bid_initial_start=points[0]['bid_initial'],
bid_initial_high=max(x['bid_initial'] for x in points),
bid_initial_low=min(x['bid_initial'] for x in points),
bid_initial_end=points[-1]['bid_initial'],
bid_shifted_start=points[0]['bid_shifted'],
bid_shifted_high=max(x['bid_shifted'] for x in points),
bid_shifted_low=min(x['bid_shifted'] for x in points),
bid_shifted_end=points[-1]['bid_shifted'],
)
for key, val in frame_data.items():
frame_data[key] = str(val)
with transaction.atomic():
shift, created = Shift.objects.update_or_create(
created=moment_time,
market=market,
pair=pair,
trade_amount=str(trade_amount),
frame_size=get_frame_label(frame_size),
defaults=frame_data,
)
yield shift
moment_time -= frame_size