hellambro / trading212api Goto Github PK
View Code? Open in Web Editor NEWUnofficial Trading212 API
License: MIT License
Unofficial Trading212 API
License: MIT License
New issue when logging in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
(Session info: headless chrome=100.0.4896.88)
Downloaded the latest code from the repo, seems like it's having issues getting the session cookie, this had also happened in the previous revision occasionally, but now it seems to be persistent.
trading212 = Trading212(email, password, driver, mode=Mode.DEMO, trading=Trading.CFD)
File "C:\Python310\lib\site-packages\pytrading212\trading212.py", line 115, in init
"Cookie": self.cookie,
AttributeError: 'Trading212' object has no attribute 'cookie'
im getting this error: AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
apperently 'find_ellement' was removed in version 4,3
Selenium 4.3.0
I've fixed some issues in the last pull request, but there is still an issue somewhere with cookies that I can't resolve. The error displays the following message shortly after successful login:
trading212 = Trading212(email, password, driver, mode=Mode.DEMO, trading=Trading.CFD)
File "C:\Python310\lib\site-packages\pytrading212\trading212.py", line 113, in init
"Cookie": self.cookie,
AttributeError: 'Trading212' object has no attribute 'cookie'
Hi,
I am having issues finding out a way to do an order on CFD. I know it's not fully worked out, but I am trying to get it to work just for testing purposes.
Right now I have the following, which doesn't work:
Do you have any suggestions what to use for the 'trading212 = ' line and the 'buy_order = ' line?
Thanks!
Hi,
Please could you update the portfolio function to include the (weighted) average purchase price of the share?
Currently you can work out the average with the quantity of the shares purchased (y) and the total value invested (x), but x/y does not give you the weighted average purchase price. This data is provided by T212, so hopefully it can be incorporated into your scraping.
portfolio = trading212.get_portfolio_composition()
Thanks,
Adam
im getting this error. i dont know whats cousing it or how to fix it.
once in a blue moon it works though
Traceback (most recent call last):
File "c:\Users\flori\OneDrive\stock\playground.py", line 242, in
portfolio = trading212.get_portfolio_composition()
File "C:\Users\flori\AppData\Local\Programs\Python\Python310\lib\site-packages\pytrading212\trading212.py", line 190, in get_portfolio_composition
self.driver.find_element_by_class_name(right_sidepanel_portfolio_class).click()
File "C:\Users\flori\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 89, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\flori\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webelement.py", line 773, in _execute
return self._parent.execute(command, params)
File "C:\Users\flori\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "C:\Users\flori\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
An error is thrown when attempting to import CFDOrder from pytrading212
init.py is missing the CFDOrder class module from .order
Trialled locally adding the class into the init.py file with success:
from .order import EquityOrder, MarketOrder, LimitOrder, StopOrder, StopLimitOrder, ValueOrder, CFDMarketOrder, \ CFDLimitStopOrder, CFDOCOOrder, CFDOrder
Hello,
I'm with some dificulties to run the bot.
I've install trading212 and all the requirements but it keeps giving the below error:
Traceback (most recent call last):
File "trading_bot1.py", line 5, in
from pytrading212.trading212 import MarketOrder
ImportError: cannot import name 'MarketOrder' from 'pytrading212.trading212' (/home/ubuntu/.pyenv/versions/3.7.12/lib/python3.7/site-packages/pytrading212/trading212.py)
Here is my trading_bot.py code:
` import time
import yfinance as yf
from selenium import webdriver
from pytrading212.trading212 import Trading212
from pytrading212.trading212 import MarketOrder
email = 'xxxxxxxxxxxxxxxxxxxx'
password = 'xxxxxxxxxxxxxxx'
driver = webdriver.Chrome(executable_path='chromedriver.exe')
trading212 = Trading212(username=email, password=password, driver=driver)
while True:
df = yf.Ticker('AMZN').history(period="1d", interval="1m").dropna() # drop NaN values
# get the last close price (1 minute candle)
last_price = df.iloc[-1]['Close']
# Write your trading logic here
# ...
if last_price < 3000:
# BUY
buy_order = trading212.execute_order(MarketOrder(instrument_code="AMZN_US_EQ", quantity=1))
print(buy_order)
elif last_price > 4000:
# SELL (note 'quantity=-1')
sell_order = trading212.execute_order(MarketOrder(instrument_code="AMZN_US_EQ", quantity=-1))
print(sell_order)
# wait 1 minute for the next candle
time.sleep(60)
`
Can you please help?
Hi,
I am trying to use the order extract to create a time series dataset of my portfolio, however I have come across an issue when stocks undergo a stock split. If I want to calculate the rolling weighted average of any stocks which I have purchased, it fails on any stocks which have had a stock split.
In the order dataframe, the values are not corrected retrospectively (e.g. Tesla stock went from ~$800 Aug 24 to ~$250 on Aug 25th, but the share/purchase price just jumps to the lower value from the stock split date). Is there any way to correct for this on your end when scraping the data? On the T212 portfolio, it is obviously corrected for, so I am wondering at what point this is done and why the output is not retrospectively corrected in the order sheet.
Thanks,
Adam
Trading 212 is returning a 404 when sending a "Market Order". I'm going to assume that the API has changed in some way. Does anyone have any info on where the addresses/API code for the PUTs came from? I can see it in the site code, and there appears to be no API documents.
Hello,
I am trying to use your trading212 API on mac but I keep getting this error related to the mode argument. I am not sure what's going on here, because the Mode.DEMO argument seems to be returning the correct results.
Would really appreciate any help.
Regards
In the GUI you just slide the slider all the way to the right, is there a way to send a market order to buy the max possible n. of shares/value if the stock can do fractional shares?
I set about making the changes but my js/html is too poor. Most of the Selenium strategies used to interact with the legacy app don't work at all with the new app.
Traceback (most recent call last):
File "Trading212.py", line 32, in
orders = trading212.get_orders()
^^^^^^^^^^^^^^^^^^^^^^^
File "pytrading212\trading212.py", line 149, in get_orders
params = {'olderThan': strftime(older_than.isoformat()),
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'isoformat'
not able to retrieve orders
Running the example I get the following error:
[01:17:50] Authenticating console.py:12
Traceback (most recent call last):
File "C:\Users\tachs\Desktop\trading212\trading212_v2.py", line 9, in <module>
equity = Equity(email='[email protected]', password='xxxxxx',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tachs\Desktop\trading212\Lib\site-packages\pytrading212\trading212.py", line 239, in __init__
super().__init__(email, password, driver, mode, constants.Trading.EQUITY)
File "C:\Users\tachs\Desktop\trading212\Lib\site-packages\pytrading212\trading212.py", line 50, in __init__
self.driver.find_element(By.NAME, "email").send_keys(email)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tachs\Desktop\trading212\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 831, in find_element
return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\tachs\Desktop\trading212\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\Users\tachs\Desktop\trading212\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
(Session info: chrome=111.0.5563.147)
Hi,
Is there a way to extract all orders via the "trading212.get_orders()" function?
Currently in this format it only shows the most recent orders.
Thanks
Check the applicable API documentation here.
They rolled out a beta version of there trading API in April 2023.
You can try it Out in Practice Mode.
I found this community forum topic to exchange the DevOps experience.
The driver only opens the login page and then prints the error message:
[23:25:22] Closing 'Cookies Popup' console.py:12
Authenticating console.py:12
[33960:2360:0519/232524.809:ERROR:device_event_log_impl.cc(239)] [23:25:24.809] USB: usb_service_win.cc:105 SetupDiGetDeviceProperty({{A45C254E-DF1C-4EFD-8020-67D146A850E0}, 6}) failed: Element not found. (0x490)
It gets stuck on this page:
I'd like to use this API to extract the data in my live account and not my practice account - how can this be done?
I can see the 'Mode=Mode.DEMO' section of the Trading212 function but haven't been able to find what this should be for the live account instead.
Thanks
Hi @HellAmbro, thank you so much for publishing this... it helps a lot!
I was wondering if you are willing to take in some very easy PRs if I submit them,
mostly related to code styling (PEP8), the integration with Pandas and a simple
update to the README.
Thanks again
Hi,
The order extract is missing several details including the fill price, exchange rate and FX Fee. Would it be possible to include these in the dictionary you extract with trading212.get_orders(older_than=older_than, newer_than=newer_than please?
Thanks,
Adam
when i run my script i get this error messages:
ERROR: Couldn't read tbsCertificate as SEQUENCE
ERROR: Failed parsing Certificate
....\site-packages\pytrading212\trading212.py", line 113, in init
"Cookie": self.cookie,
AttributeError: 'Trading212' object has no attribute 'cookie'
edit: did some changes and in LIVE mode it does work, but DEMO still doesnt
Logs in fine but then im slapped with this error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element
When executing trading212.get_portfolio_composition()
I get the following error:
selenium.common.exceptions.TimeoutException: Message: Stacktrace: #0 0x55bb3ede4ad3 <unknown> #1 0x55bb3eb44568 <unknown> #2 0x55bb3eb7ac46 <unknown> #3 0x55bb3eb7ae01 <unknown> #4 0x55bb3ebada64 <unknown> #5 0x55bb3eb9861d <unknown> #6 0x55bb3ebab824 <unknown> #7 0x55bb3eb984e3 <unknown> #8 0x55bb3eb6dd1a <unknown> #9 0x55bb3eb6ee75 <unknown> #10 0x55bb3ee12efd <unknown> #11 0x55bb3ee2c19b <unknown> #12 0x55bb3ee14c65 <unknown> #13 0x55bb3ee2cec8 <unknown> #14 0x55bb3ee08360 <unknown> #15 0x55bb3ee48a68 <unknown> #16 0x55bb3ee48be8 <unknown> #17 0x55bb3ee627fd <unknown> #18 0x7fcecebe9609 <unknown>
Any idea what is wrong? The driver is set up correctly and I can get into website and print the header etc, so I am guessing it's not related to driver setup. I can also login to my 212 account as I get the login notification.
while it does show the login screen, moves past it and shows my main page with CFDs, I cannot move past the CFD object initialization. this is what I see on the screen
"""
config = configparser.ConfigParser()
config.read('./config.ini')
print (config['ACCOUNT']['email'])
driver = webdriver.Chrome(service=Service())
cfd = CFD(email=config['ACCOUNT']['email'], password=config['ACCOUNT']['password'], driver=driver, mode=Mode.LIVE)
15:13:44] Starting PyTrading212 v0.2.8 console.py:12
Trading: CFD
Mode: LIVE
[15:13:45] Closing 'Cookies Popup' console.py:12
Authenticating console.py:12
ERROR:main:An error occurred: Message:
Stacktrace:
#0 0x61ec2bcddc7a
#1 0x61ec2b9c0e2c
#2 0x61ec2ba0d661
#3 0x61ec2ba0d751
#4 0x61ec2ba51f64
#5 0x61ec2ba305ed
#6 0x61ec2ba4f303
#7 0x61ec2ba30363
#8 0x61ec2ba00247
#9 0x61ec2ba00b9e
#10 0x61ec2bca424b
#11 0x61ec2bca82f1
#12 0x61ec2bc8fafe
#13 0x61ec2bca8e52
#14 0x61ec2bc7479f
#15 0x61ec2bccd638
#16 0x61ec2bccd810
#17 0x61ec2bcdcdac
#18 0x7ed989c9ca94
#19 0x7ed989d29c3c
ERROR:main:Traceback:
Traceback (most recent call last):
File "debug-script.py", line 25, in
cfd = CFD(
File "/home/kalabharath/anaconda3/envs/mljar/lib/python3.8/site-packages/pytrading212/trading212.py", line 491, in init
super().init(email, password, driver, mode, constants.Trading.CFD)
File "/home/kalabharath/anaconda3/envs/mljar/lib/python3.8/site-packages/pytrading212/trading212.py", line 72, in init
WebDriverWait(self.driver, 120).until(EC.presence_of_element_located((By.CLASS_NAME, "company-logo")))
File "/home/kalabharath/anaconda3/envs/mljar/lib/python3.8/site-packages/selenium/webdriver/support/wait.py", line 105, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
#0 0x61ec2bcddc7a
#1 0x61ec2b9c0e2c
#2 0x61ec2ba0d661
#3 0x61ec2ba0d751
#4 0x61ec2ba51f64
#5 0x61ec2ba305ed
#6 0x61ec2ba4f303
#7 0x61ec2ba30363
#8 0x61ec2ba00247
#9 0x61ec2ba00b9e
#10 0x61ec2bca424b
#11 0x61ec2bca82f1
#12 0x61ec2bc8fafe
#13 0x61ec2bca8e52
#14 0x61ec2bc7479f
#15 0x61ec2bccd638
#16 0x61ec2bccd810
#17 0x61ec2bcdcdac
#18 0x7ed989c9ca94
#19 0x7ed989d29c3c
INFO:main:Current URL: https://app.trading212.com/
I am running ubuntu 24.04
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.