Skip to content
← All articles
Article

MarketWatch API Python library

Listen to the audio story

A Python client for the MarketWatch game · 1:10 · AI-written script, synthetic voices

Transcript

Ava: MarketWatch runs a stock market game where you trade with pretend money. What if you could drive it from Python?

Andrew: As in, buy and sell from a script?

Ava: That's the idea behind Antoine's MarketWatch API library. It's a Python client for the MarketWatch stock market game, built on earlier community work credited in the repository.

Andrew: What can it do?

Ava: Log in and out, get a stock's current price, and fetch information about games. It can buy, sell, short and cover stocks in a game. It also handles watchlists, portfolios, positions and pending orders, and can check whether the game is down.

Andrew: And using it?

Ava: You install it with pip, create a MarketWatch object with your username and password, and call methods like get price, get games or get leaderboard. The example reads credentials from environment variables rather than hard-coding them.

Andrew: Is it maintained like a real project?

Ava: The page shows badges for the PyPI version, Python versions, license, downloads, a test workflow and coverage. It's under the MIT license and welcomes contributions.

Andrew: The full page has the usage examples.

Table of contents

Marketwatch API Python Library

PyPI PyPI - Python Version PyPI - License PyPI - Downloads GitHub last commit Python Test and Build Coverage

MarketWatch

Documentation

A Python library to interact with the MarketWatch Stock Market Game Based on code from

Feature

  • Logging in and out of the site
  • Getting the current price of a stock
  • Getting information about games on the site
  • Buying, selling, shorting, and covering stocks in a game
  • Creating, adding to, getting, and deleting watchlists
  • Getting, adding to, and deleting items from a portfolio
  • Getting and cancelling pending orders
  • Checking if the game is down

Installation

pip install marketwatch
pip install git+https://github.com/antoinebou12/marketwatch.git
git clone https://github.com/antoinebou12/marketwatch.git

Usage

Here are some examples of how you can use the MarketWatch class:

Import

First, import the MarketWatch class from the script:

from marketwatch import MarketWatch

Login

Then, create an instance of the MarketWatch class using your MarketWatch username and password:

marketwatch = MarketWatch(username, password)

Get Stock Price

To get the current price of a stock:

marketwatch.get_price("AAPL")

Interact with Games

https://www.marketwatch.com/games

To get information about games on the site:

marketwatch.get_games()

Get Game

marketwatch.get_game("game-name")

Get Game Settings

marketwatch.get_game_settings("game-name")

Get Leaderboard

marketwatch.get_leaderboard("game-name")

Get Portfolio

marketwatch.get_portfolio("game-name")

Get Positions

marketwatch.get_positions("game-name")

Get Pending Orders

marketwatch.get_pending_orders("game-name")

Buy Stock

marketwatch.buy(game_id, "AAPL", 100)

Sell Stock

marketwatch.sell("game-name", "AAPL", 100)

Create Watchlist

https://www.marketwatch.com/watchlist

To create a watchlist:

marketwatch.create_watchlist('My Watchlist')

Add Stock to Watchlist

To add stocks to a watchlist:

marketwatch.add_to_watchlist(watchlist_id, ['AAPL', 'GOOG'])

Get All Watchlists

To get all watchlists:

watchlists = marketwatch.get_watchlists()

Delete Watchlist

To delete a watchlist:

marketwatch.delete_watchlist(watchlist_id)

Example

import os
username = os.environ.get("MARKETWATCH_USERNAME")
password = os.environ.get("MARKETWATCH_PASSWORD")

marketwatch = MarketWatch(username, password)

print(f"Price: {marketwatch.get_price('AAPL')} \n")
print(f"Games: {marketwatch.get_games()} \n")

games1 = marketwatch.get_games()[0]["name"].lower().replace(" ", "-")

print(f"Game: {marketwatch.get_game(games1)} \n")
print(f"Game Settings: {marketwatch.get_game_settings(games1)} \n")

print(f"Leaderboard: {marketwatch.get_leaderboard(games1)} \n")
print(f"Porfolio: {marketwatch.get_portfolio(games1)} \n")

print(f"Position: {marketwatch.get_positions(games1)}")
print(f"Orders Pending: {marketwatch.get_pending_orders(games1)}")
marketwatch.buy(games1, "AAPL", 100)

print(f"Position diff: {marketwatch.get_positions(games1)}")

Contributing

Contributions are welcome. Please open an issue or submit a pull request.

License

This project is licensed under the MIT License.