A simple Python API for Investopedia's stock simulator games. This programmatically logs into Investopedia and can retrieve portfolio summary, get stock quotes & option chain lookups, execute trades - buy & sell shares, puts, calls, sell short, etc.

dchrostowski dchrostowski Last update: Aug 08, 2022

Description

A simple Python API for Investopedia's stock simulator games.

Features

Currently you can:

  • Read all positions in your option, stock, and short portfolios
  • Buy/Sell long positions
  • Short sell/cover short positions
  • Perform option chain lookups
  • Buy/sell options
  • Read pending/open trades

Todo:

  • Setting the default game, changing games
  • Whatever else I can think of

Authentication

Simply pass a dict to InvetopediaAPI constructor with a username and password.

from investopedia_api import InvestopediaApicredentials = {"username" "[email protected]", "password": "your password"}client = InvestopediaAPI(credentials)p = client.portfolioprint("account value: %s" % p.account_value)

Environment

Python 3.6.7. I just use a virtualenv and install using pip from requirements.txt. If you don't know how to do that:

git clone https://github.com/dchrostowski/investopedia_simulator_api.gitcd investopedia_simulator_apipip install virtualenvvirtualenv -p /path/to/python3 ./venvsource venv/bin/activatepip install -r requirements.txtpython exmaple.py

Example

code

from investopedia_api import InvestopediaApiimport jsoncredentials = {}with open('credentials.json') as ifh:    credentials = json.load(ifh)# look at credentials_example.json# credentials = {"username": "[email protected]", "password": "yourpassword"}client = InvestopediaApi(credentials)p = client.portfolioprint("account value: %s" % p.account_value)print("cash: %s" % p.cash)print("buying power: %s" % p.buying_power)print("annual return pct: %s" % p.annual_return_pct)# get a quotequote = client.get_stock_quote('GOOG')print(quote.__dict__)# Read your portfoliolong_positions = client.portfolio.stock_portfolioshort_positions = client.portfolio.short_portfoliomy_options = client.portfolio.option_portfolio# Place a buy order for 10 shares of Google with a limit of $1000/share# shorthand for client.TradeProperties.TradeType.BUY()trade_type = 'buy'#shorthand for client.TradeProperties.OrderType.LIMIT(1000)limit_1000 = 'limit 1000'trade = client.StockTrade('GOOG',10,trade_type,order_type=limit_1000)trade_info = trade.validate()if trade.validated:    print(trade_info)    trade.execute()# See example.py for more examples.

More Info / Documentation

This is a work in progress. I'll add more documentation as I continue developing. I also plan on making this a module and publishing to pip.

PRAGMA foreign_keys = off; BEGIN TRANSACTION; COMMIT TRANSACTION; PRAGMA foreign_keys = on;

Subscribe to our newsletter