Skip to main content

An unofficial API library for Trading212 based on Selenium

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trading212api-0.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trading212api-0.1.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file trading212api-0.1.1.tar.gz.

File metadata

  • Download URL: trading212api-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for trading212api-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8f24a8b439cd1502c0210aca1c786a5648af2395f682f51455f60a5a117e3f37
MD5 ea83ac780719db9d60f071a068a3d5aa
BLAKE2b-256 5206fbca92c832f3ff850179797775516d1a2d7c046832617227f3b91d43fc28

See more details on using hashes here.

File details

Details for the file trading212api-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: trading212api-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for trading212api-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f4e3e9c4ecddfb0a20bb30c575bfe9c91e9f169276e7699ff49aca5938f5ab9
MD5 29b9e26cbb158ed7d873f227fb7da186
BLAKE2b-256 8d413c823bf311d024ec123c27743c28491aa27ffc224aac532886e2e138536e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page