Skip to content

Examples

Example

This code snippet shows an example usage of the MarketWatch class. It first imports the os module and uses it to retrieve the user's MarketWatch username and password from the environment variables MARKETWATCH_USERNAME and MARKETWATCH_PASSWORD. It then creates a MarketWatch object with these credentials.

The example code then demonstrates the usage of several methods provided by the MarketWatch class, including:

  • get_price(ticker): This method takes a stock ticker as an argument and returns its current price.
  • get_games(): This method returns a list of games the user is currently playing on MarketWatch.
  • get_game(game_id): This method takes a game ID as an argument and returns details about the game, such as its name, start and end dates, and current ranking.
  • get_game_settings(game_id): This method takes a game ID as an argument and returns its settings, such as the starting cash balance and trading rules.
  • get_leaderboard(game_id): This method takes a game ID as an argument and returns the current leaderboard for that game.
  • get_portfolio(game_id): This method takes a game ID as an argument and returns the user's portfolio for that game, including information such as the quantity and value of each holding.
  • get_positions(game_id): This method takes a game ID as an argument and returns the user's current positions for that game.
  • get_pending_orders(game_id): This method takes a game ID as an argument and returns the user's pending orders for that game.
  • buy(game_id, ticker, quantity): This method takes a game ID, stock ticker, and quantity as arguments and places a buy order for the specified quantity of shares.
if __name__ == "__main__":
    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)}")
Example:
    >>> from marketwatch import MarketWatch
        >>> import os
    >>> mw = MarketWatch(os.environ["MARKETWATCH_USERNAME"], os.environ["MARKETWATCH_PASSWORD"])
    >>> mw.get_games()
    >>> mw.get_portfolio('algoets-h2023')
    >>> mw.buy('algoets-h2023', "AAPL", 1)
    >>> mw.get_leaderboard('algoets-h2023')
Results:
    >>> mw = MarketWatch(os.environ["MARKETWATCH_USERNAME"], os.environ["MARKETWATCH_PASSWORD"])
        Login successful
        Logged in as Antoine Boucher
    >>> mw.get_games()
        [{'name': 'ALGOETS-H2023', 'url': 'https://www.marketwatch.com/games/algoets-h2023', 'id': 'algoets-h2023',                 'return': '0.17%', 'total_return': '-$1,975.21', 'rank': '17', 'end': '3/31/23', 'players': '27'}]
    >>> mw.get_portfolio('algoets-h2023')
{'portfolio': [{'sign': '+', 'ticker': 'AAPD', 'quantity': '12 Shares', 'holding': '\nBuy\n', 'holding_percentage': '< 1%', 'price': '$24.63', 'price_gain': '0.33', 'price_gain_percentage': '1.36%', 'value': '$295.56', 'value_percentage': '1.19%', 'value_point': '$3.48'}, {'sign': '-', 'ticker': 'AAPL', 'quantity': '589 Shares', 'holding': '\nBuy\n', 'holding_percentage': '11%', 'price': '$158.28', 'price_gain': '-1.97', 'price_gain_percentage': '-1.23%', 'value': '$93,226.92', 'value_percentage': '-0.81%', 'value_point': '-$758.50'}, {'sign': '+', 'ticker': 'AAPL', 'quantity': '47 Shares', 'holding': '\nShort\n', 'holding_percentage': '< 1%', 'price': '$158.28', 'price_gain': '-1.97', 'price_gain_percentage': '-1.23%', 'value': '$7,439.16', 'value_percentage': '1.03%', 'value_point': '$77.43'}, {'sign': '+', 'ticker': 'ANCTF', 'quantity': '400 Shares', 'holding': '\nBuy\n', 'holding_percentage': '2%', 'price': '$47.80', 'price_gain': '0.75', 'price_gain_percentage': '1.59%', 'value': '$19,120.00', 'value_percentage': '1.58%', 'value_point': '$298.05'}, {'sign': '+', 'ticker': 'BAC', 'quantity': '100 Shares', 'holding': '\nBuy\n', 'holding_percentage': '< 1%', 'price': '$28.49', 'price_gain': '1.35', 'price_gain_percentage': '4.97%', 'value': '$2,849.00', 'value_percentage': '1.53%', 'value_point': '$43.00'}, {'sign': '-', 'ticker': 'BLK', 'quantity': '100 Shares', 'holding': '\nBuy\n', 'holding_percentage': '8%', 'price': '$648.32', 'price_gain': '3.44', 'price_gain_percentage': '0.53%', 'value': '$64,832.00', 'value_percentage': '-1.56%', 'value_point': '-$1,025.00'}, {'sign': '-', 'ticker': 'BMO', 'quantity': '250 Shares', 'holding': '\nBuy\n', 'holding_percentage': '3%', 'price': '$85.78', 'price_gain': '1.23', 'price_gain_percentage': '1.45%', 'value': '$21,445.00', 'value_percentage': '-0.69%', 'value_point': '-$148.75'}, {'sign': '-', 'ticker': 'COIN', 'quantity': '120 Shares', 'holding': '\nBuy\n', 'holding_percentage': '< 1%', 'price': '$62.54', 'price_gain': '-5.29', 'price_gain_percentage': '-7.80%', 'value': '$7,504.80', 'value_percentage': '-4.37%', 'value_point': '-$343.20'}, {'sign': '+', 'ticker': 'CPRT', 'quantity': '1 Shares', 'holding': '\nBuy\n', 'holding_percentage': '< 1%', 'price': '$72.73', 'price_gain': '1.30', 'price_gain_percentage': '1.82%', 'value': '$72.73', 'value_percentage': '1.41%', 'value_point': '$1.01'}, {'sign': '+', 'ticker': 'DLMAF', 'quantity': '500 Shares', 'holding': '\nBuy\n', 'holding_percentage': '3%', 'price': '$57.14', 'price_gain': '0.19', 'price_gain_percentage': '0.33%', 'value': '$28,570.00', 'value_percentage': '0.47%', 'value_point': '$132.30'}, {'sign': '+', 'ticker': 'GE', 'quantity': '500 Shares', 'holding': '\nBuy\n', 'holding_percentage': '6%', 'price': '$93.31', 'price_gain': '1.94', 'price_gain_percentage': '2.12%', 'value': '$46,655.00', 'value_percentage': '2.84%', 'value_point': '$1,290.00'}, {'sign': '+', 'ticker': 'MA', 'quantity': '700 Shares', 'holding': '\nBuy\n', 'holding_percentage': '30%', 'price': '$354.62', 'price_gain': '2.99', 'price_gain_percentage': '0.85%', 'value': '$248,234.00', 'value_percentage': '0.21%', 'value_point': '$511.00'}, {'sign': '-', 'ticker': 'META', 'quantity': '300 Shares', 'holding': '\nBuy\n', 'holding_percentage': '7%', 'price': '$202.84', 'price_gain': '-3.17', 'price_gain_percentage': '-1.54%', 'value': '$60,852.00', 'value_percentage': '-1.11%', 'value_point': '-$684.00'}, {'sign': '-', 'ticker': 'ROST', 'quantity': '400 Shares', 'holding': '\nBuy\n', 'holding_percentage': '5%', 'price': '$100.63', 'price_gain': '-0.70', 'price_gain_percentage': '-0.69%', 'value': '$40,252.00', 'value_percentage': '-1.89%', 'value_point': '-$777.50'}, {'sign': '-', 'ticker': 'RY', 'quantity': '500 Shares', 'holding': '\nBuy\n', 'holding_percentage': '6%', 'price': '$93.55', 'price_gain': '1.36', 'price_gain_percentage': '1.48%', 'value': '$46,775.00', 'value_percentage': '-0.78%', 'value_point': '-$365.55'}, {'sign': '+', 'ticker': 'TD', 'quantity': '250 Shares', 'holding': '\nBuy\n', 'holding_percentage': '2%', 'price': '$57.93', 'price_gain': '1.38', 'price_gain_percentage': '2.44%', 'value': '$14,482.50', 'value_percentage': '1.38%', 'value_point': '$197.50'}, {'sign': '+', 'ticker': 'TSN', 'quantity': '100 Shares', 'holding': '\nBuy\n', 'holding_percentage': '< 1%', 'price': '$57.99', 'price_gain': '0.93', 'price_gain_percentage': '1.63%', 'value': '$5,799.00', 'value_percentage': '1.44%', 'value_point': '$82.50'}, {'sign': '+', 'ticker': 'V', 'quantity': '500 Shares', 'holding': '\nBuy\n', 'holding_percentage': '14%', 'price': '$221.81', 'price_gain': '0.77', 'price_gain_percentage': '0.35%', 'value': '$110,905.00', 'value_percentage': '0.68%', 'value_point': '$750.00'}], 'portfolio_value': '$998,024.79', 'gain_percentage': '0.17%', 'gain': '-$1,975.21', 'return': '-0.20%', 'cash_remaining': '$186,076.85', 'buying_power': '$71,408.88', 'shorts_reserve': '$7,516.59', 'cash_borrowed': '$0.00', 'portfolio_allocation': []}
    >> mw.get_leaderboard('algoets-h2023')
    [{'rank': 'N/A', 'player': 'N/A', 'player_url': 'N/A', 'portfolio_value': 'N/A', 'gain_percentage': 'N/A', 'transactions': 'N/A', 'gain': 'N/A'}, {'rank': '1', 'player': 'Gabriel B', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=aRxpqLsZV3bh', 'portfolio_value': '$1,058,554.25', 'gain_percentage': '5.17%', 'transactions': '9', 'gain': '$58,554.25'}, {'rank': '2', 'player': 'Eric Li', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=AyX19mUAFYLl', 'portfolio_value': '$1,050,503.94', 'gain_percentage': '0.00%', 'transactions': '10', 'gain': '$50,503.94'}, {'rank': '3', 'player': 'Marcelo Guzman', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=UF59tiipo7SI', 'portfolio_value': '$1,027,631.05', 'gain_percentage': '0.41%', 'transactions': '12', 'gain': '$27,631.05'}, {'rank': '4', 'player': 'salim hmidi', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=OYkxYr5vOgZn', 'portfolio_value': '$1,024,083.28', 'gain_percentage': '0.73%', 'transactions': '23', 'gain': '$24,083.28'}, {'rank': '5', 'player': 'Belet Clément', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=wvxYWSVAE28A', 'portfolio_value': '$1,019,546.45', 'gain_percentage': '-0.59%', 'transactions': '25', 'gain': '$19,546.45'}, {'rank': '6', 'player': 'Dickson Chea', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=pbdnS0kYwMqt', 'portfolio_value': '$1,018,345.98', 'gain_percentage': '0.13%', 'transactions': '5', 'gain': '$18,345.98'}, {'rank': '7', 'player': 'Yannis Yapo', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=QpP_TrwKu562', 'portfolio_value': '$1,018,001.53', 'gain_percentage': '1.60%', 'transactions': '24', 'gain': '$18,001.53'}, {'rank': '8', 'player': 'Marius DELEUIL', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=5KAMIinx8frR', 'portfolio_value': '$1,016,723.61', 'gain_percentage': '3.92%', 'transactions': '37', 'gain': '$16,723.61'}, {'rank': '9', 'player': 'Anthony Gomes', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=e7PoS_mV73Vl', 'portfolio_value': '$1,016,281.94', 'gain_percentage': '-1.64%', 'transactions': '4', 'gain': '$16,281.94'}, {'rank': '10', 'player': 'Sourivong Keoborakoth', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=rlrO8fbS5XvG', 'portfolio_value': '$1,013,989.90', 'gain_percentage': '0.22%', 'transactions': '3', 'gain': '$13,989.90'}, {'rank': '11', 'player': 'Raphael Leblanc', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=RkriElAkDawr', 'portfolio_value': '$1,005,545.78', 'gain_percentage': '3.24%', 'transactions': '3', 'gain': '$5,545.78'}, {'rank': '12', 'player': 'Ariel Sashcov', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=lFPDAA1uqVwU', 'portfolio_value': '$1,002,891.74', 'gain_percentage': '-0.18%', 'transactions': '10', 'gain': '$2,891.74'}, {'rank': '13', 'player': 'oue oue', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=_fnpQ0VQWYw4', 'portfolio_value': '$1,001,443.57', 'gain_percentage': '0.19%', 'transactions': '1', 'gain': '$1,443.57'}, {'rank': '14', 'player': 'Alexis Macagno', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=fS8fkt43cXPF', 'portfolio_value': '$1,001,132.13', 'gain_percentage': '0.27%', 'transactions': '6', 'gain': '$1,132.13'}, {'rank': '15', 'player': 'Nadim Mejri', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=QC9NsO3vvpgO', 'portfolio_value': '$1,000,320.86', 'gain_percentage': '-0.15%', 'transactions': '22', 'gain': '$320.86'}, {'rank': '16', 'player': 'Wiamou Odilon Kohoun', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=nXN7msIbjGBw', 'portfolio_value': '$1,000,079.87', 'gain_percentage': '0.00%', 'transactions': '4', 'gain': '$79.87'}, {'rank': '17', 'player': 'Antoine Boucher', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=_7g9NEC9_Eqy', 'portfolio_value': '$998,024.79', 'gain_percentage': '0.17%', 'transactions': '256', 'gain': '-$1,975.21'}, {'rank': '18', 'player': 'Michel-Ange Rodriguez', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=WbjMeERMT_K9', 'portfolio_value': '$980,616.14', 'gain_percentage': '-1.37%', 'transactions': '1', 'gain': '-$19,383.86'}, {'rank': '19', 'player': 'Kevin D', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=L4yFHrYxv8Zt', 'portfolio_value': '$976,684.77', 'gain_percentage': '0.55%', 'transactions': '6', 'gain': '-$23,315.23'}, {'rank': '20', 'player': 'Mohamed Ilias', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=3OFiPTAQg0sW', 'portfolio_value': '$957,943.44', 'gain_percentage': '-2.79%', 'transactions': '8', 'gain': '-$42,056.57'}, {'rank': '21', 'player': 'Florent Duchesne', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=lBPD30iAGrV3', 'portfolio_value': '$879,949.53', 'gain_percentage': '-7.70%', 'transactions': '1', 'gain': '-$120,050.47'}, {'rank': 'N/A', 'player': 'Sadia Hussain', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=3eF9rBieBk_R', 'portfolio_value': '$1,000,000.00', 'gain_percentage': '0.00%', 'transactions': '0', 'gain': '$0.00'}, {'rank': 'N/A', 'player': 'Ilyes Essid', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=h56WzTZABaMW', 'portfolio_value': '$1,000,000.00', 'gain_percentage': '0.00%', 'transactions': '0', 'gain': '$0.00'}, {'rank': 'N/A', 'player': 'Ilyes Essid', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=uqVnT7cFiwFR', 'portfolio_value': '$1,000,000.00', 'gain_percentage': '0.00%', 'transactions': '0', 'gain': '$0.00'}, {'rank': 'N/A', 'player': 'mathieu rochat', 'player_url': 'https://www.marketwatch.com/games/algoets-h2023/portfolio?pub=ySePrgIvU7dE', 'portfolio_value': '$1,000,000.00', 'gain_percentage': '0.00%', 'transactions': '0', 'gain': '$0.00'}]