THE REINSURANCE ACTUARY
  • Blog
  • Project Euler
  • Category Theory
  • Disclaimer

Python script - PredictIt API

11/3/2022

 

I wrote a quick Python script to download the latest odds from PredictIt, and then output to an Excel file. I've pasted it below as an extract from a Jupyter notebook:

PredictIt is an online prediction website, mainly focused on Political events:
www.predictit.org/

I think it's great that PredictIt allow access like this, before I realised the API exists I was using Selenium to scrape the info through Chrome, which was much slower to run, and also occasionally buggy.

In [ ]:
import pandas as pd
import time
import requests
import json
import xlsxwriter
In [ ]:
#%% predictit API download

# The input file is a csv with a list of PredictIt API pages, one row per page, such as the following:
# https://www.predictit.org/api/marketdata/markets/7198

PredictitDown = pd.read_csv(r"C:\Users\Admin\Documents\Predictit - 2022 03 11.csv")

Rows = PredictitDown [PredictitDown .columns[0]].count()

AllQuestions = []
Allnames = []
Allodds = []

for i in range(Rows):

    link = PredictitDown["Market URL"][i]
    response = requests.get(link)
    
    PredictitData = json.loads(response.text)
    my_list = PredictitData ['contracts']

    listnames = []
    listodds = []
    
    for item in my_list:
        listnames.append(item['name'])
        listodds.append(item['bestBuyNoCost'])

    AllQuestions.append(PredictitData['name'])
    Allnames.append(listnames)
    Allodds.append(listodds)

df = pd.DataFrame(AllQuestions)
df2 = pd.DataFrame(Allnames)
df3 = pd.DataFrame(Allodds)

dflist=[df,df2,df3]

Excelwriter = pd.ExcelWriter(r"C:\Users\Admin\Documents\PredictitDownloads\PredictitOutput.xlsx",engine="xlsxwriter")

for i, dataf in enumerate (dflist):
    dataf.to_excel(Excelwriter, sheet_name="Sheet" + str(i+1),index=False)
    
#

Excelwriter.save()

Your comment will be posted after it is approved.


Leave a Reply.

    Author

    ​​I work as an actuary and underwriter at a global reinsurer in London.

    I mainly write about Maths, Finance, and Technology.
    ​
    If you would like to get in touch, then feel free to send me an email at:

    ​LewisWalshActuary@gmail.com

      Sign up to get updates when new posts are added​

    Subscribe

    RSS Feed

    Categories

    All
    Actuarial Careers/Exams
    Actuarial Modelling
    Bitcoin/Blockchain
    Book Reviews
    Economics
    Finance
    Forecasting
    Insurance
    Law
    Machine Learning
    Maths
    Misc
    Physics/Chemistry
    Poker
    Puzzles/Problems
    Statistics
    VBA

    Archives

    March 2023
    February 2023
    October 2022
    July 2022
    June 2022
    May 2022
    April 2022
    March 2022
    October 2021
    September 2021
    August 2021
    July 2021
    April 2021
    March 2021
    February 2021
    January 2021
    December 2020
    November 2020
    October 2020
    September 2020
    August 2020
    May 2020
    March 2020
    February 2020
    January 2020
    December 2019
    November 2019
    October 2019
    September 2019
    April 2019
    March 2019
    August 2018
    July 2018
    June 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    April 2016
    January 2016

  • Blog
  • Project Euler
  • Category Theory
  • Disclaimer