HellAmbro / Trading212API

Unofficial Trading212 API
MIT License
57 stars 27 forks source link

Cannot run #32

Closed dalmeida80 closed 11 months ago

dalmeida80 commented 11 months ago

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')

initialize Trading212 api

trading212 = Trading212(username=email, password=password, driver=driver)

iterate indefinitely

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?

HellAmbro commented 11 months ago

Change pytrading212.trading212 to pytrading212 in the second import, remove the first one. From

import time
import yfinance as yf
from selenium import webdriver
from pytrading212.trading212 import Trading212
from pytrading212.trading212 import MarketOrder

to

import time
import yfinance as yf
from selenium import webdriver
from pytrading212 import MarketOrder