Skip to main content

trading212api

An unofficial API library for Trading212 based on Selenium

Description

This library allows you to interface with Trading212 through Selenium. Once you log in, the only operation requiring a human interation, you can buy and sell stocks, in both real and practice modes.

Installation

pip install trading212api

Install a Selenium web driver, e.g., the Chrome WebDriver

Available methods

  • enable_real_mode(): this method is used to enable real/live mode
  • enable_practice_mode(): this method is used to enable practice/demo mode
  • initiate_connection(webdriverfile, loginusername=None, loginpassword=None, headless=False): initializes the connection to Trading212 through Selenium, optionally asking for username and password to login; in such case, an automatic login will be performed; otherwise, user interaction will be required, for a manual login to occur; if headless is True, the browser will be executed in headless mode
  • get_stocks_list(brw, navigationpath=None): returns all the list of stocks available, optionally selecting a specific > separated list identifying the navigation path, in terms of content, to follow
  • get_stock_info(brw, ticker, backtohome_begin=True, backtohome_end=True): starting from a selenium.webdriver.chrome.webdriver.WebDriver object brw, this method retrieves information from a stock identified by a given ticker, optionally, returning home at the begin/end of the method
  • get_portfolio(brw): starting from a selenium.webdriver.chrome.webdriver.WebDriver object brw, this method returns information on the current portfolio
  • buy(brw, ticker, amount, max_price=None): starting from a selenium.webdriver.chrome.webdriver.WebDriver object brw, this method buys a stock identified by its own ticket, specifying the amount in terms of money to invest in the current valuta, plus an optional maximum price max_price; returns a boolean value indicating the success of the operation
  • sell(brw, ticker, amount=None, min_price=None): starting from a selenium.webdriver.chrome.webdriver.WebDriver object brw, this method sells a stock identified by its own ticket, specifying the amount in terms of number of stocks to sell and an optional minimum price min_price; returns a boolean value indicating the success of the operation

Sample usage

# import the library
import trading212api

# enable practice mode
trading212api.enable_practice_mode()

# initiate the connection
brw = trading212api.initiate_connection('./chromedriver')

# get and print portfolio information
portfolio = trading212api.get_portfolio(brw)
print(portfolio)

# get and print information on a given stock
stock_info = trading212api.get_stock_info(brw, 'AAPL')
print(stock_info)

# get all most popular stocks
popular_stocks = trading212api.get_stocks_list(brw, navigationpath='Stocks>Most Popular')

# get and print information on all most popular stocks
for s in popular_stocks:
	stock_info = trading212api.get_stock_info(brw, s.get('ticker'))
	print(stock_info)

# buy 10 USD of AAPL
trading212api.buy(brw, 'AAPL', 10)

# sell 1 stock of AAPL
trading212api.sell(brw, 'AAPL', 1)

TODO

  • Improve code readability

Contacts

You can find me on Twitter as @auino.

Release files for trading212api 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trading212api 0.1.1
File Size Uploaded
trading212api-0.1.1.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trading212api 0.1.1
File Interpreter ABI Platform
trading212api-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.5 kB

Release files / trading212api-0.1.1.tar.gz

Download URL trading212api-0.1.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8f24a8b439cd1502c0210aca1c786a5648af2395f682f51455f60a5a117e3f37
BLAKE2b-256 checksum
How to use checksums
5206fbca92c832f3ff850179797775516d1a2d7c046832617227f3b91d43fc28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Release files / trading212api-0.1.1-py3-none-any.whl

Download URL trading212api-0.1.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f4e3e9c4ecddfb0a20bb30c575bfe9c91e9f169276e7699ff49aca5938f5ab9
BLAKE2b-256 checksum
How to use checksums
8d413c823bf311d024ec123c27743c28491aa27ffc224aac532886e2e138536e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page