Guest User

ETWKBC Code v1

a guest
Dec 1st, 2023
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. import urllib.request
  2. import json
  3. import time
  4. import math
  5.  
  6. BASE_URL = "https://manifold.markets/api"
  7. HEADERS = {
  8.     # "Authorization": f"Key {API_KEY}",
  9.     "Content-Type": "application/json"
  10. }
  11.  
  12. KBC_SLUG = "exponential-time-weighted-keynesian"
  13.  
  14. # Fetch market info
  15. def get_market(slug):
  16.     req = urllib.request.Request(f"{BASE_URL}/v0/slug/{slug}", headers=HEADERS)
  17.     with urllib.request.urlopen(req) as response:
  18.         return json.load(response)
  19.  
  20. def get_current_weighted_sum(mkt):
  21.     weighted_sum = 0
  22.     for a in mkt['answers']:
  23.         weighted_sum += a['probability'] * int(a['text'])
  24.  
  25.     return weighted_sum
  26.  
  27. # Fetch all bets for market, paginating through recursively until nothing comes back
  28. def get_market_bets(slug, before_id=None):
  29.     url = f"{BASE_URL}/v0/bets?contractSlug={slug}"
  30.     if before_id is not None:
  31.         url += f"&before={before_id}"
  32.  
  33.     try:
  34.         req = urllib.request.Request(url, headers=HEADERS)
  35.     except e:
  36.         print(f"HTTP Error code: {e.code}")
  37.         print(f"Reason: {e.reason}")
  38.         print(f"Headers: {e.hdrs}")
  39.         print(f"Error content: {e.fp.read()}")
  40.         raise e
  41.  
  42.     with urllib.request.urlopen(req) as response:
  43.         bets = json.load(response)
  44.  
  45.     # base case, just return
  46.     if len(bets) == 0:
  47.         return []
  48.  
  49.     return bets + get_market_bets(slug, bets[-1]["id"])
  50.  
  51.  
  52. bets = get_market_bets(KBC_SLUG)
  53.  
  54. print(f"Total Bets: {len(bets)}")
  55. market_info = get_market(KBC_SLUG)
  56.  
  57. current_weighted_sum = get_current_weighted_sum(market_info)
  58. print(f"Current Weighted Sum: {current_weighted_sum}")
  59.  
  60. market_creation_time = market_info["createdTime"]
  61. current_or_close_time = min(market_info["closeTime"], round(time.time() * 1000))
  62. total_time = current_or_close_time - market_creation_time
  63.  
  64. for a in market_info["answers"]:
  65.     # Bets for that answer (including implicit ones from bets on other answers)
  66.     answer_bets = sorted(
  67.         [b for b in bets if b["answerId"] == a["id"]], key=lambda a: a["createdTime"]
  68.     )
  69.     a['num_bets'] = len(answer_bets)
  70.     # Numerical value of the answer
  71.     answer_val = int(a["text"])
  72.  
  73.     # val * prob * duration for each bet
  74.     weighted_sum = (
  75.         answer_val
  76.         * answer_bets[0]["probBefore"]
  77.         * (answer_bets[0]["createdTime"] - market_creation_time)
  78.     )
  79.  
  80.     for i, b in enumerate(answer_bets):
  81.         bet_duration = (
  82.             answer_bets[i + 1]["createdTime"] - b["createdTime"]
  83.             if i + 1 < len(answer_bets)
  84.             else current_or_close_time - b["createdTime"]
  85.         )
  86.         weighted_sum += answer_val * b["probAfter"] * bet_duration
  87.  
  88.     a["weighted_sum"] = weighted_sum
  89.  
  90. # Sum all the answer's weighted sums, divide by total market time
  91. final_sum = sum(a["weighted_sum"] for a in market_info["answers"]) / total_time
  92.  
  93. # Divide by 2
  94. final_answer = final_sum / 2
  95.  
  96. print("")
  97. print(f"Time Weighted Sum: {final_sum}")
  98. print(f"Final Answer: {final_answer}")
  99.  
  100. # Find linear weighting for resolution
  101.  
  102. # Find the closest options
  103. lower = 0
  104. upper = 99999999999999999999
  105. exact = None
  106.  
  107. for a in market_info["answers"]:
  108.     val = int(a["text"])
  109.     if val < final_answer and val > lower:
  110.         lower = val
  111.     if val > final_answer and val < upper:
  112.         upper = val
  113.     if val == final_answer:
  114.         exact = val
  115.  
  116. if exact is not None:
  117.     print(f"Resolution: 100% {exact}")
  118. else:
  119.     lower_percent = int(round((upper - final_answer) / (upper - lower) * 100))
  120.     upper_percent = 100 - lower_percent
  121.     print(f"Resolution: {lower_percent}% {lower}, {upper_percent}% {upper}")
  122.  
Advertisement
Add Comment
Please, Sign In to add comment