Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import requests
  2. import json
  3. import time
  4.  
  5. MAX_COUNT = 20 # number of pages of bets, counting by 1000
  6. # times retrieved from https://manifold.markets/api/v0/slug/will-the-average-probability-of-thi
  7. START_TIME = 1679884201443 # 7:30 PT on Sunday, March 26
  8. END_TIME = 1680489000000 # 7:30 PT on Sunday, April 2
  9. NEEDED = 0.5
  10.  
  11. def loadMarket():
  12. response = requests.get('https://manifold.markets/api/v0/market/will-the-average-probability-of-thi')
  13.  
  14. def loadBets():
  15. ret = []
  16.  
  17. i = 0
  18. lastId = None
  19.  
  20. while True:
  21. response = requests.get(
  22. 'https://manifold.markets/api/v0/bets',
  23. params={'limit': 1000, 'before': lastId, 'contractSlug': 'will-the-average-probability-of-thi'}
  24. )
  25. bets = json.loads(response.content)
  26. print('loaded a set of bets')
  27.  
  28. if len(bets) == 0 or i > MAX_COUNT:
  29. print('finished at', i)
  30. print('lastId', lastId)
  31. return ret
  32.  
  33. for bet in bets:
  34. if bet.get('isRedemption'):
  35. # this is a payout, not a bet
  36. continue
  37. ret.append(bet)
  38.  
  39. lastId = bets[-1]['id']
  40. i += 1
  41.  
  42. def round_manifold(percentage):
  43. if percentage < 0.02 or percentage > 0.98:
  44. return round(percentage, 3)
  45. else:
  46. return round(percentage, 2)
  47.  
  48. def calculateAverage(bets):
  49. currentSum = 0
  50. currentProb = 0.5
  51. currentTime = START_TIME
  52. for bet in bets:
  53. for fill in bet['fills']:
  54. if 'limitProb' in bet and fill.get('matchedBetId') != None:
  55. # we can ignore this fill, as it's a limit order fill that will be matched by another bet
  56. continue
  57. timeDiff = fill['timestamp'] - currentTime
  58. currentSum += currentProb * timeDiff
  59. currentProb = round_manifold(bet['probAfter'])
  60. currentTime = fill['timestamp']
  61.  
  62. # account for the time after the last bet
  63. timeDiff = END_TIME - currentTime
  64. currentSum += currentProb * timeDiff
  65.  
  66. average = currentSum / (END_TIME - START_TIME)
  67. return average
  68.  
  69. def main():
  70. global bets
  71. bets = loadBets()
  72. # we could extract fills and sort by timestamp but in practice this will be the same
  73. bets.sort(key=lambda bet: bet["createdTime"])
  74. global average
  75. average = calculateAverage(bets)
  76. print('current average', average)
  77.  
  78. main()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement