Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- import time
- MAX_COUNT = 20 # number of pages of bets, counting by 1000
- # times retrieved from https://manifold.markets/api/v0/slug/will-the-average-probability-of-thi
- START_TIME = 1679884201443 # 7:30 PT on Sunday, March 26
- END_TIME = 1680489000000 # 7:30 PT on Sunday, April 2
- NEEDED = 0.5
- def loadMarket():
- response = requests.get('https://manifold.markets/api/v0/market/will-the-average-probability-of-thi')
- def loadBets():
- ret = []
- i = 0
- lastId = None
- while True:
- response = requests.get(
- 'https://manifold.markets/api/v0/bets',
- params={'limit': 1000, 'before': lastId, 'contractSlug': 'will-the-average-probability-of-thi'}
- )
- bets = json.loads(response.content)
- print('loaded a set of bets')
- if len(bets) == 0 or i > MAX_COUNT:
- print('finished at', i)
- print('lastId', lastId)
- return ret
- for bet in bets:
- if bet.get('isRedemption'):
- # this is a payout, not a bet
- continue
- ret.append(bet)
- lastId = bets[-1]['id']
- i += 1
- def round_manifold(percentage):
- if percentage < 0.02 or percentage > 0.98:
- return round(percentage, 3)
- else:
- return round(percentage, 2)
- def calculateAverage(bets):
- currentSum = 0
- currentProb = 0.5
- currentTime = START_TIME
- for bet in bets:
- for fill in bet['fills']:
- if 'limitProb' in bet and fill.get('matchedBetId') != None:
- # we can ignore this fill, as it's a limit order fill that will be matched by another bet
- continue
- timeDiff = fill['timestamp'] - currentTime
- currentSum += currentProb * timeDiff
- currentProb = round_manifold(bet['probAfter'])
- currentTime = fill['timestamp']
- # account for the time after the last bet
- timeDiff = END_TIME - currentTime
- currentSum += currentProb * timeDiff
- average = currentSum / (END_TIME - START_TIME)
- return average
- def main():
- global bets
- bets = loadBets()
- # we could extract fills and sort by timestamp but in practice this will be the same
- bets.sort(key=lambda bet: bet["createdTime"])
- global average
- average = calculateAverage(bets)
- print('current average', average)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement