HellAmbro / Trading212API

Unofficial Trading212 API
MIT License
57 stars 27 forks source link

[Feature request] Be able to put trough a CFD order. #21

Closed bjornstevens closed 1 year ago

bjornstevens commented 1 year ago

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: afbeelding

Do you have any suggestions what to use for the 'trading212 = ' line and the 'buy_order = ' line?

Thanks!

HellAmbro commented 1 year ago

Yes, the CFD are not supported yet but I work on it asap. As a first suggestion, try to add '#' before the instrument_code, so it will become instrument_code='#EURUSD'

bjornstevens commented 1 year ago

afbeelding

Sadly didn't work out. Thanks for the suggestion though, I look forward to when it is fixed! Do note that I removed the ', trading=CFD' line in line20

HellAmbro commented 1 year ago

From a quick look it seems to me that you are using the API incorrectly, as soon as I can I will try to post a snippet of a working CFD order. Unfortunately this was my shortcoming as when I started writing the API the CFD support was not considered as I did not need it. Now that interest in CFD support has increased I am planning to rewrite some of the code to make it clearer with almost full CFD support. Probably in a new major release.

bjornstevens commented 1 year ago

From a quick look it seems to me that you are using the API incorrectly, as soon as I can I will try to post a snippet of a working CFD order. Unfortunately this was my shortcoming as when I started writing the API the CFD support was not considered as I did not need it. Now that interest in CFD support has increased I am planning to rewrite some of the code to make it clearer with almost full CFD support. Probably in a new major release.

On the first line: yes, you are probably right that I am not using it the right way!

On the rest: No poblem, I really like the work you're doing and hope you can continue to improve it forward with more CFD support. Can't wait for the new major release 💯

HellAmbro commented 1 year ago

Hi, sorry for the late response but i found a bug and fixed it, i've also added an example of CFD order execution. As i just told you, unfortunately the API usage is slightly different in respect to EQUITY trading, but i'm working to fix it in a future release, for now you can use this snippet. I wrote this snippet very quickly so maybe rename the variable's names for better reading.

import sys

from selenium import webdriver

from pytrading212 import *

if __name__ == "__main__":
    email = sys.argv[1]
    password = sys.argv[2]
    driver = webdriver.Chrome(executable_path='chromedriver.exe')

    cfd = CFD(email, password, driver, mode=Mode.DEMO)
    order = CFDMarketOrder(instrument_code='EURGBP', target_price=10.0, quantity=500)
    cfd_order_outcome = cfd.execute_order(order)
    print(cfd_order_outcome)

I've received this message, so the order worked for me, even if it was not executed because i exceeded the max buy of 500 units {'code': 'BusinessException', 'context': {'max': 0, 'type': 'MaxBuyQuantityExceeded'}, 'message': 'can open at most0'} As a last remainder, but i think that you already now, i'm not able to get the current price of CFD, so if you want to buy in target_price you have to set a bigger value than the current price, the same for sell but with a lower value.

bjornstevens commented 1 year ago

Hi, sorry for the late response but i found a bug and fixed it, i've also added an example of CFD order execution. As i just told you, unfortunately the API usage is slightly different in respect to EQUITY trading, but i'm working to fix it in a future release, for now you can use this snippet. I wrote this snippet very quickly so maybe rename the variable's names for better reading.

Thanks for the reply!

I tried this out but sadly got this error now using your code (filename is different because I tried changing things in the original to make it work, but I am using your latest update): afbeelding

If i try to run example.py (while being in CFD) I get this output (ofcourse I do not expect it to work, but I get more output than on the cfd_example.py: afbeelding

(i forgot to hide some id's, but this is a testing account which has no REAL money account linked to it.

HellAmbro commented 1 year ago

Thanks for the feedback, i need to add some checks when the page is fully loaded, maybe sometimes selenium doesn't found the element because T212 is still loading. I will fix it asap maybe tomorrow, I don't want to put a lot of effort because I'm planning to rewrite better (also with CFD support) a big part of the code but unfortunately in this period the time is not my friend. I will work on a solution for you asap, but look at it as a temporary solution, waiting for the full support. Maybe you should try to open the code with an IDE, put some breakpoints inside Trading212 class in the init method, in the cookie section and wait until the trading212 page is fully loaded

bjornstevens commented 1 year ago

Maybe you should try to open the code with an IDE, put some breakpoints inside Trading212 class in the init method, in the cookie section and wait until the trading212 page is fully loaded

That worked! I just added 2 time.sleep(10) (so sleep 10 seconds) into the code before the cookie, which made the order go trough! also added import time, because trading212.py didn't have that imported yet. afbeelding

afbeelding

edit: 3 seconds also worked

bjornstevens commented 1 year ago

It might be that, because I am using a VM, my system is just a little too slow

bjornstevens commented 1 year ago

As a last remainder, but i think that you already now, i'm not able to get the current price of CFD, so if you want to buy in target_price you have to set a bigger value than the current price, the same for sell but with a lower value.

This part doesn't seem to work for me. I only am able to buy right now: (the argument is the target_price) afbeelding

HellAmbro commented 1 year ago

Maybe you should try to open the code with an IDE, put some breakpoints inside Trading212 class in the init method, in the cookie section and wait until the trading212 page is fully loaded

That worked! I just added 2 time.sleep(10) (so sleep 10 seconds) into the code before the cookie, which made the order go trough! also added import time, because trading212.py didn't have that imported yet. afbeelding

afbeelding

edit: 3 seconds also worked

I'm happy that worked and you fixed it yourself, there a lot of better solution but for now keep that as a workaround waiting for the new API. Sometimes T212 is very slow to load.

HellAmbro commented 1 year ago

As a last remainder, but i think that you already now, i'm not able to get the current price of CFD, so if you want to buy in target_price you have to set a bigger value than the current price, the same for sell but with a lower value.

This part doesn't seem to work for me. I only am able to buy right now: (the argument is the target_price) afbeelding

Because you put a MARKET BUY order but with a lower price than the current, as you can see "operation=buy".

bjornstevens commented 1 year ago

As a last remainder, but i think that you already now, i'm not able to get the current price of CFD, so if you want to buy in target_price you have to set a bigger value than the current price, the same for sell but with a lower value.

This part doesn't seem to work for me. I only am able to buy right now: (the argument is the target_price) afbeelding

Because you put a MARKET BUY order but with a lower price than the current, as you can see "operation=buy".

Yeah I noticed, but I can't find a place where it should be changed. I couldn't find a hard-coded place where it says: BUY or MARKET BUY order.py: afbeelding

HellAmbro commented 1 year ago

Maybe the T212 Post request is changed since i wrote the API a lot of time ago. I will investigate on this and let you now asap. Thanks for you patience and for the support to improve this API.

bjornstevens commented 1 year ago

Maybe the T212 Post request is changed since i wrote the API a lot of time ago. I will investigate on this and let you now asap. Thanks for you patience and for the support to improve this API.

Sounds good! Let me know when you've got an update :D

shaunandgemma commented 1 year ago

I'd love to know if there is a working API, is there any update?

HellAmbro commented 1 year ago

For CFD not yet, i was very busy. I will try to work on CFD in the next weeks.

I'd love to know if there is a working API, is there any update?

shaunandgemma commented 1 year ago

Thanks for the reply! Ill be on the lookout

HellAmbro commented 1 year ago

I'm working right now on it. Look at dev branch to know the progress