-
Notifications
You must be signed in to change notification settings - Fork 18
/
market_arbitrage_with_foil_cards.py
248 lines (203 loc) · 9.33 KB
/
market_arbitrage_with_foil_cards.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# Objective: find market arbitrages with foil cards, e.g. buy a foil card and turn it into more gems than its cost.
#
# Caveat: the goal is not to make a profit by selling the gems afterwards! The goal is to acquire gems, this is why
# ALL the computations are performed with fee included: opportunities are compared from the perspective of the buyer.
#
# In summary, we do not care about buy orders here! We only care about sell orders!
from src.market_foil_utils import (
build_dictionary_of_representative_listing_hashes,
determine_whether_an_arbitrage_might_exist_for_foil_cards,
discard_necessarily_unrewarding_app_ids,
download_missing_goo_details,
filter_listings_with_arbitrary_price_threshold,
filter_out_listing_hashes_if_goo_details_are_already_known_for_app_id,
find_app_ids_with_unknown_item_type_for_their_representatives,
find_cheapest_listing_hashes,
find_eligible_listing_hashes,
find_listing_hashes_with_unknown_goo_value,
find_representative_listing_hashes,
get_listings_for_foil_cards,
group_listing_hashes_by_app_id,
print_arbitrages_for_foil_cards,
propagate_filter_to_representative_listing_hashes,
try_again_to_download_goo_value,
try_again_to_download_item_type,
)
from src.market_listing import get_item_nameid_batch, load_all_listing_details
from src.sack_of_gems import load_sack_of_gems_price
from src.utils import (
get_goo_details_file_nam_for_for_foil_cards,
get_listing_details_output_file_name_for_foil_cards,
get_listing_output_file_name_for_foil_cards,
)
def apply_workflow_for_foil_cards(
retrieve_listings_from_scratch: bool = False,
price_threshold_in_cents_for_a_foil_card: float | None = None,
retrieve_gem_price_from_scratch: bool = False,
enforced_sack_of_gems_price: float | None = None, # price in euros
start_index: int = 0,
verbose: bool = True,
) -> bool:
listing_output_file_name = get_listing_output_file_name_for_foil_cards()
listing_details_output_file_name = (
get_listing_details_output_file_name_for_foil_cards()
)
goo_details_file_name_for_for_foil_cards = (
get_goo_details_file_nam_for_for_foil_cards()
)
# Fetch all the listings of foil cards
all_listings = get_listings_for_foil_cards(
retrieve_listings_from_scratch=retrieve_listings_from_scratch,
listing_output_file_name=listing_output_file_name,
start_index=start_index,
verbose=verbose,
)
# Group listings by appID
groups_by_app_id = group_listing_hashes_by_app_id(
all_listings,
verbose=verbose,
)
# Find the cheapest listing in each group
cheapest_listing_hashes = find_cheapest_listing_hashes(
all_listings,
groups_by_app_id,
)
# Find the representative listing in each group
# For retro-compatibility, we try to use representative for which we previously downloaded item name ids
dictionary_of_representative_listing_hashes = (
build_dictionary_of_representative_listing_hashes(
listing_details_output_file_name=listing_details_output_file_name,
)
)
representative_listing_hashes = find_representative_listing_hashes(
groups_by_app_id,
dictionary_of_representative_listing_hashes,
)
# List eligible listing hashes (positive ask volume, and positive ask price)
eligible_listing_hashes = find_eligible_listing_hashes(all_listings)
# Filter listings with an arbitrary price threshold
# NB: This is only useful to speed up the pre-retrieval below, by focusing on the most interesting listings.
filtered_cheapest_listing_hashes = filter_listings_with_arbitrary_price_threshold(
all_listings=all_listings,
listing_hashes_to_filter_from=cheapest_listing_hashes,
price_threshold_in_cents=price_threshold_in_cents_for_a_foil_card,
verbose=verbose,
)
filtered_representative_listing_hashes = (
propagate_filter_to_representative_listing_hashes(
listing_hashes_to_propagate_to=representative_listing_hashes,
listing_hashes_to_propagate_from=filtered_cheapest_listing_hashes,
)
)
# Filter out listings associated with an appID for which we already know the goo details.
filtered_representative_listing_hashes_with_missing_goo_details = filter_out_listing_hashes_if_goo_details_are_already_known_for_app_id(
filtered_representative_listing_hashes,
goo_details_file_name_for_for_foil_cards=goo_details_file_name_for_for_foil_cards,
verbose=verbose,
)
# Pre-retrieval of item name ids (and item types at the same time)
get_item_nameid_batch(
filtered_representative_listing_hashes_with_missing_goo_details,
listing_details_output_file_name=listing_details_output_file_name,
)
# Load the price of a sack of 1000 gems
if enforced_sack_of_gems_price is None:
sack_of_gems_price_in_euros = load_sack_of_gems_price(
retrieve_gem_price_from_scratch=retrieve_gem_price_from_scratch,
verbose=verbose,
)
else:
sack_of_gems_price_in_euros = enforced_sack_of_gems_price
# Fetch goo values
all_listing_details = load_all_listing_details(
listing_details_output_file_name=listing_details_output_file_name,
)
all_goo_details = download_missing_goo_details(
groups_by_app_id=groups_by_app_id,
listing_candidates=filtered_representative_listing_hashes_with_missing_goo_details,
all_listing_details=all_listing_details,
listing_details_output_file_name=listing_details_output_file_name,
goo_details_file_name_for_for_foil_cards=goo_details_file_name_for_for_foil_cards,
verbose=verbose,
)
# List unknown item types
try_again_to_find_item_type = False
app_ids_with_unreliable_goo_details = (
find_app_ids_with_unknown_item_type_for_their_representatives(
groups_by_app_id=groups_by_app_id,
listing_candidates=filtered_representative_listing_hashes,
all_listing_details=all_listing_details,
listing_details_output_file_name=listing_details_output_file_name,
verbose=verbose,
)
)
if try_again_to_find_item_type:
try_again_to_download_item_type(
app_ids_with_unreliable_goo_details,
filtered_representative_listing_hashes,
listing_details_output_file_name,
)
# List unknown goo values
try_again_to_find_goo_value = False
app_ids_with_unknown_goo_value = find_listing_hashes_with_unknown_goo_value(
listing_candidates=filtered_representative_listing_hashes,
app_ids_with_unreliable_goo_details=app_ids_with_unreliable_goo_details,
all_goo_details=all_goo_details,
verbose=verbose,
)
if try_again_to_find_goo_value:
try_again_to_download_goo_value(
app_ids_with_unknown_goo_value,
filtered_representative_listing_hashes,
groups_by_app_id,
)
# Solely for information purpose, count the number of potentially rewarding appIDs.
#
# NB: this information is not used, but one could imagine only retrieving the ask price of potentially rewarding
# appIDs, so that time is not lost retrieving the ask price of necessarily unrewarding appIDs.
# Indeed, this could halve the number of queries, e.g. as of January 2020, there are:
# - 8872 appIDs in total,
# - out of which 4213 to 4257 are potentially rewarding appIDs, so 47% - 48% of the appIDs.
discard_necessarily_unrewarding_app_ids(
all_goo_details=all_goo_details,
app_ids_with_unreliable_goo_details=app_ids_with_unreliable_goo_details,
app_ids_with_unknown_goo_value=app_ids_with_unknown_goo_value,
sack_of_gems_price_in_euros=sack_of_gems_price_in_euros,
retrieve_gem_price_from_scratch=retrieve_gem_price_from_scratch,
verbose=verbose,
)
# Find market arbitrages
arbitrages = determine_whether_an_arbitrage_might_exist_for_foil_cards(
eligible_listing_hashes,
all_goo_details=all_goo_details,
app_ids_with_unreliable_goo_details=app_ids_with_unreliable_goo_details,
app_ids_with_unknown_goo_value=app_ids_with_unknown_goo_value,
all_listings=all_listings,
listing_output_file_name=listing_output_file_name,
sack_of_gems_price_in_euros=sack_of_gems_price_in_euros,
retrieve_gem_price_from_scratch=retrieve_gem_price_from_scratch,
verbose=verbose,
)
print_arbitrages_for_foil_cards(
arbitrages,
use_numbered_bullet_points=True,
)
return True
def main() -> bool:
retrieve_listings_from_scratch = True
price_threshold_in_cents_for_a_foil_card = None
retrieve_gem_price_from_scratch = True
enforced_sack_of_gems_price = None # price in euros
start_index = 0 # to resume the update in case of wrong status code
verbose = True
apply_workflow_for_foil_cards(
retrieve_listings_from_scratch=retrieve_listings_from_scratch,
price_threshold_in_cents_for_a_foil_card=price_threshold_in_cents_for_a_foil_card,
retrieve_gem_price_from_scratch=retrieve_gem_price_from_scratch,
enforced_sack_of_gems_price=enforced_sack_of_gems_price,
start_index=start_index,
verbose=verbose,
)
return True
if __name__ == "__main__":
main()