Qluxzz / avanza

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

Placing orders (buy/sell) for funds #31

Closed tux2000 closed 3 years ago

tux2000 commented 3 years ago

Hej.

I was looking for functionality to buy and sell funds using this library. To what I could see this functionality is not yet available and the function place_order is limited to instruments like stocks and certificates that are traded on an exchange.

In the website the API endpoint I am looking for is https://www.avanza.se/_api/fund-guide/fund-order-page/buy with the request payload being for example: {"orderbookId":"56127","accountId":"NNNNNNNN","amount":"100","orderbookName":"Simplicity Likviditet A"}

I assume the corresponding endpoint is available in the mobile API too and thus hopefully this should be possible to implement.

warna720 commented 3 years ago

Haven't yet reached the point to testing place_order in production yet but when skimming through the code I dont see any restrictions for the type of order. Just specify which instrument by order_book_id: https://github.com/Qluxzz/avanza/blob/3f3a4c5193cb0c686bfbce46d68824a29651af7f/avanza/avanza.py#L1476

tux2000 commented 3 years ago

I tried using the place_order function but got stuck with price and volume (required by the function) vs amount (entered as only quantity on the website)

warna720 commented 3 years ago

Ah yeah your’e right, its a whole different endpoint

warna720 commented 3 years ago

For the one implementing this in the future: Looks like its 2 requests

  1. POST https://www.avanza.se/_api/fund-guide/fund-order-page/buy Payload {"orderbookId":"512559","accountId":"<accountId>","amount":"100","orderbookName":"Handelsbanken Hållbar Energi A1 SEK"} Response {"orderRequestStatus":"SUCCESS","orderId":<orderId>,"message":"","accountId":"<accountId>"}
  2. POST https://www.avanza.se/_api/fund-guide/fund-order-page/order Payload {"accountId":"<accountId>","orderId":<orderId>} Response {"visibleOnAccountDate":"2021-05-17","stopTime":"2021-05-14T13:00:00"}
tux2000 commented 3 years ago

I added fund buying functionality in a new function place_order_buy_fund on this branch: https://github.com/tux2000/avanza/tree/fund_orders

This is not elegant yet and should probably be tested more. But it worked to send a buy order for a fund that showed up on the Avanza website. Only the first request from @warna720 's comment (https://github.com/Qluxzz/avanza/issues/31#issuecomment-839985434) seems to be strictly needed. And I left out the orderbookName in the request payload...

Maybe it helps with a proper implementation...

tux2000 commented 3 years ago

Here are some details for the sell call to the API (as performed by the website):

Two requests:

  1. POST https://www.avanza.se/_api/fund-guide/fund-order-page/sell Payload: {"orderbookId":"56127","accountId":"<accountId>","volume":1.241047,"orderbookName":"Simplicity Likviditet A","nav":112.82} Response: {"orderRequestStatus":"SUCCESS","orderId":<orderId>,"message":"","accountId":"<accountId>"}

  2. POST https://www.avanza.se/_api/fund-guide/fund-order-page/order Payload: {"accountId":"<accountId>","orderId":<orderId>} Response: {"visibleOnAccountDate":"2021-05-24","stopTime":"2021-05-20T13:00:00"}

warna720 commented 3 years ago

Huh, so both volume and amount works in the first request works?

tux2000 commented 3 years ago

The requests in my comment are for a sell transaction, while your request examples in https://github.com/Qluxzz/avanza/issues/31#issuecomment-839985434 is for a buy transaction.

warna720 commented 3 years ago

Ah got it. Will you make a PR soon btw?

tux2000 commented 3 years ago

I might want to refactor the code a bit and I would like to have both buy and sell working before doing a pull request. But of course anybody is more than welcome to use what already is present in my fork to speed up an implementation...

tux2000 commented 3 years ago

Now I also implemented a function to sell funds shares. Its available at https://github.com/tux2000/avanza/tree/fund_orders

Deleting of fund orders is possible with the already available function delete_order

warna720 commented 3 years ago

Nice job!

tux2000 commented 3 years ago

Not really happy yet with introducing two more functions but it works for now. I will send a pull request in a minute to start the discussion how this can be included in upstream...