Advertisement
Guest User

CETWKBC Code v1

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