Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request
- import json
- import time
- import math
- from datetime import datetime
- import pytz
- BASE_URL = "https://manifold.markets/api"
- HEADERS = {
- # "Authorization": f"Key {API_KEY}",
- "Content-Type": "application/json"
- }
- KBC_SLUG = "centered-exponential-time-weighted"
- # Fetch market info
- def get_market(slug):
- req = urllib.request.Request(f"{BASE_URL}/v0/slug/{slug}", headers=HEADERS)
- with urllib.request.urlopen(req) as response:
- return json.load(response)
- def get_current_weighted_sum(mkt):
- weighted_sum = 0
- for a in mkt['answers']:
- weighted_sum += a['probability'] * int(a['text'])
- return weighted_sum
- # Fetch all bets for market, paginating through recursively until nothing comes back
- def get_market_bets(slug, before_id=None):
- url = f"{BASE_URL}/v0/bets?contractSlug={slug}"
- if before_id is not None:
- url += f"&before={before_id}"
- try:
- req = urllib.request.Request(url, headers=HEADERS)
- except e:
- print(f"HTTP Error code: {e.code}")
- print(f"Reason: {e.reason}")
- print(f"Headers: {e.hdrs}")
- print(f"Error content: {e.fp.read()}")
- raise e
- with urllib.request.urlopen(req) as response:
- bets = json.load(response)
- # base case, just return
- if len(bets) == 0:
- return []
- return bets + get_market_bets(slug, bets[-1]["id"])
- # Define the time zone for Pacific Standard Time (PST)
- pst = pytz.timezone('America/Los_Angeles')
- # Get the current date and time in PST
- current_datetime_pst = datetime.now(pst)
- # Format the date and time as requested: "Output: MM/DD HH:MMPM PST"
- formatted_datetime = f"Output {current_datetime_pst.strftime('%m/%d %I:%M%p')} PST\n"
- print(formatted_datetime)
- bets = get_market_bets(KBC_SLUG)
- print(f"Total Bets: {len(bets)}")
- market_info = get_market(KBC_SLUG)
- current_weighted_sum = get_current_weighted_sum(market_info)
- print(f"Current Weighted Sum: {current_weighted_sum}")
- market_creation_time = market_info["createdTime"]
- current_or_close_time = min(market_info["closeTime"], round(time.time() * 1000))
- total_time = current_or_close_time - market_creation_time
- for a in market_info["answers"]:
- # Bets for that answer (including implicit ones from bets on other answers)
- answer_bets = sorted(
- [b for b in bets if b["answerId"] == a["id"]], key=lambda a: a["createdTime"]
- )
- a['num_bets'] = len(answer_bets)
- # Numerical value of the answer
- answer_val = int(a["text"])
- # val * prob * duration for each bet
- weighted_sum = (
- answer_val
- * answer_bets[0]["probBefore"]
- * (answer_bets[0]["createdTime"] - market_creation_time)
- )
- for i, b in enumerate(answer_bets):
- bet_duration = (
- answer_bets[i + 1]["createdTime"] - b["createdTime"]
- if i + 1 < len(answer_bets)
- else current_or_close_time - b["createdTime"]
- )
- weighted_sum += answer_val * b["probAfter"] * bet_duration
- a["weighted_sum"] = weighted_sum
- # Sum all the answer's weighted sums, divide by total market time
- final_sum = sum(a["weighted_sum"] for a in market_info["answers"]) / total_time
- # Divide by 2
- final_answer = final_sum / 2
- print("")
- print(f"Time Weighted Sum: {final_sum}")
- print(f"Final Answer: {final_answer}")
- # Find linear weighting for resolution
- # Find the closest options
- lower = -9999999999999999999
- upper = 99999999999999999999
- exact = None
- for a in market_info["answers"]:
- val = int(a["text"])
- if val < final_answer and val > lower:
- lower = val
- if val > final_answer and val < upper:
- upper = val
- if val == final_answer:
- exact = val
- if exact is not None:
- print(f"Resolution: 100% {exact}")
- else:
- lower_percent = int(round((upper - final_answer) / (upper - lower) * 100))
- upper_percent = 100 - lower_percent
- print(f"Resolution: {lower_percent}% {lower}, {upper_percent}% {upper}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement