Giter Site home page Giter Site logo

trading212api's Introduction

Trading212 API

Unofficial API for Trading212 broker.

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

Support project

Buy Me A Coffee

LTC: LbAzhtHBvQ2JCGhrcefUuvwNrv9VrQJoyJ

BTC: 1JWhMC3tiSjyR6t7BJuM7YRDg3xvPM2MDk

ETH: 0x51f1f0061eadc024ab4bd1f3658d249044160006

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.

Medium Article

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"

trading212api's People

Contributors

hellambro avatar alex1443 avatar phalanxguard avatar tbridel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.