Skip to main content

Unofficial Trading212 API

Project description

Trading212 API

Unofficial API for Trading212 broker.

CFD are not suppoerted yet (only experimental for now). Investing only.

Support this project

Buy Me A Coffee

Disclaimer

Nor me or Trading212 are responsible for the use of this API, first make sure that everything works well through the use of a **

DEMO** account, then switch to REAL mode.

Prerequisites

WebDriver Getting started with WebDriver

Troubleshooting

Driver requirements

Install

pip3 install pytrading212

Usage

Refer here: example.py

import sys

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

from pytrading212 import *  # just for simplicity, not recommended, import only what you use
from pytrading212.trading212 import Period

# Use your preferred web driver with your custom options
# options = Options()
# headless (optional)
# options.add_argument('--headless')
# options.add_argument('--disable-gpu')
# Chrome
driver = webdriver.Chrome()
# or Firefox
# driver = webdriver.Firefox()

trading212 = Trading212('myemail', 'mypass', driver, mode=Mode.DEMO)

market_order = trading212.execute_order(
    MarketOrder(instrument_code="AMZN_US_EQ", quantity=2)
)

limit_order = trading212.execute_order(
    LimitOrder(
        instrument_code="AMZN_US_EQ",
        quantity=2,
        limit_price=3000,
        time_validity=TimeValidity.DAY,
    )
)

stop_order = trading212.execute_order(
    StopOrder(
        instrument_code="AMZN_US_EQ",
        quantity=2,
        stop_price=4000,
        time_validity=TimeValidity.GOOD_TILL_CANCEL,
    )
)

stop_limit = trading212.execute_order(
    StopLimitOrder(
        instrument_code="AMZN_US_EQ",
        quantity=2,
        limit_price=3000,
        stop_price=4000,
        time_validity=TimeValidity.GOOD_TILL_CANCEL,
    )
)

quantity_order = trading212.execute_order(
    EquityOrder(
        "AMZN_US_EQ",
        quantity=2,
        limit_price=3000,
        stop_price=4000,
        time_validity=TimeValidity.GOOD_TILL_CANCEL,
    )
)

value_order = trading212.execute_value_order(ValueOrder("AMZN_US_EQ", value=100))

# sell an equity that you own

value_sell_order = trading212.execute_value_order(
    ValueOrder("AMZN_US_EQ", value=-100)
)

sell_limit = trading212.execute_order(
    LimitOrder(
        instrument_code="AMZN_US_EQ",
        quantity=-2,
        limit_price=4000,
        time_validity=TimeValidity.GOOD_TILL_CANCEL,
    )
)

print(quantity_order)
print(value_order)

funds = trading212.get_funds()
orders = trading212.get_orders()

print(funds)
print(orders)

portfolio = trading212.get_portfolio_composition()
performance = trading212.get_portfolio_performance(Period.LAST_DAY)

print(portfolio)
print(performance)
# Pandas integration examples
funds_df = pd.DataFrame(funds)
orders_df = pd.DataFrame(orders)
portfolio_df = pd.DataFrame(portfolio)
performance_df = pd.DataFrame(performance)

# close webdriver 
trading212.close()

Funds (as DataFrame)

accountId                                            XXXXXXX                                   XXXXXXX
tradingType                                           EQUITY                                       CFD
currency                                                 EUR                                       EUR
freeForWithdraw                                      2784.75                                  47680.45
freeForCfdTransfer                                   2784.75                                         0
total                                               10075.08                                  47680.45
lockedCash          {'totalLockedCash': 0, 'lockedCash': []}  {'totalLockedCash': 0, 'lockedCash': []}

Portfolio

portfolio = trading212.get_portfolio_composition()
[
  {
    'logo_url': 'https://trading212equities.s3.eu-central-1.amazonaws.com/BABA_US_EQ.png',
    'instrument_code': 'BABA_US_EQ',
    'value': '€9.28',
    'quantity': '0.0479677 shares',
    'total_return': '€0.28 (3.11%)'
  },
  {
    'logo_url': 'https://trading212equities.s3.eu-central-1.amazonaws.com/AMZN_US_EQ.png',
    'instrument_code': 'AMZN_US_EQ',
    'value': '€15,625.60',
    'quantity': '6.00353784 shares',
    'total_return': '€75.57 (0.49%)'
  },
]

Performance

trading212.get_portfolio_performance(Period.LAST_DAY)
{
  'snapshots': [
    {
      'investment': 16438.3,
      'ppl': -159.81,
      'result': -17.92,
      'pieCash': 0.05,
      'time': '2021-01-13T11:00:00Z'
    },
    {
      'investment': 16438.3,
      'ppl': -158.06,
      'result': -17.92,
      'pieCash': 0.05,
      'time': '2021-01-13T11:15:00Z'
    },
    -- snip --

How can I get instrument code?

Lookup in companies.json, key "ticker"

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

pytrading212-0.1.7.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

pytrading212-0.1.7-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file pytrading212-0.1.7.tar.gz.

File metadata

  • Download URL: pytrading212-0.1.7.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for pytrading212-0.1.7.tar.gz
Algorithm Hash digest
SHA256 ef85688b5233d052bad6810daf505508a7d2fdfb6abfb53c0db9b35e58285e2c
MD5 407eb34b028e4faab982f3b26df98af8
BLAKE2b-256 e702c60cf392d3a03ca53b50044f8571f971d3b1869a35653dab1bcab22ad4f7

See more details on using hashes here.

File details

Details for the file pytrading212-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pytrading212-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.4.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for pytrading212-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3c112000e909836180252fb41daac5f56f2fbd1e2e8c6c63e73f2ef10aa4a6fe
MD5 6829462d0a53c461a09f874cc5dfa4a8
BLAKE2b-256 cb257989ddf54ca587a0d7351670508b9ff6cac7c1070c6ac6adef33ecd0e109

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