Qluxzz / avanza

A Python library for the unofficial Avanza API
https://qluxzz.github.io/avanza/
MIT License
85 stars 40 forks source link

Add two functions to buy and sell investment funds #32

Closed tux2000 closed 3 years ago

tux2000 commented 3 years ago

This should fix #31

Adds two new functions:

Example code:

ed = avanza.place_order_buy_fund(
    account_id='<accountid>',
    order_book_id = "56127",
    amount = 1000)
print(json.dumps(ed, indent=2, sort_keys=True))

order = ed["orderId"]

time.sleep(1)

ed = avanza.delete_order(account_id="<accountid>", order_id=order)

print(json.dumps(ed, indent=2, sort_keys=True))

time.sleep(5)

######################################

ed = avanza.place_order_sell_fund(
    account_id='<accountid>',
    order_book_id = "56127",
    volume = 2)
print(json.dumps(ed, indent=2, sort_keys=True))

order = ed["orderId"]

time.sleep(1)

ed = avanza.delete_order(account_id="<accountid>", order_id=order)

print(json.dumps(ed, indent=2, sort_keys=True))

Not sure if this is the optimal solution but since buy and sell are working with different parameters (amount vs volume) having a universal function might be confusing too.

warna720 commented 3 years ago

Maybe the two functions can be merged e.g. place_order_fund with a OrderType argument since its only the route path which diffs?

tux2000 commented 3 years ago

Nja, buy is sending the order amount in SEK in the payload while sell is sending the order volume in number of shares in the payload. Having logic within the function that converts volume (shares) to amount (SEK) will add complexity and reduce flexibility.

An alternative might be to have a universal function that accepts both volume and amount with None as default plus Ordertype as argument. Then some logic within the function could only use the appropriate argument and potentially convert between units but users might get more easily confused.

So: not sure what the optimal solution is

warna720 commented 3 years ago

I think current solution makes the caller observant of the amount/volume diff, which i prefer over having it hidden inside. Maybe we can try to RE it and call Avanza API with amount in sell, if that works then it would be nice

tux2000 commented 3 years ago

Maybe we can try to RE it and call Avanza API with amount in sell, if that works then it would be nice

I tested sending an entry "amount" in the payload for the sell request. It resulted in an error 400 requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.avanza.se/_api/fund-guide/fund-order-page/sell

warna720 commented 3 years ago

Alright. LGTM then.

Qluxzz commented 3 years ago

Thanks for helping out!