Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placing orders (buy/sell) for funds #31

Closed
tux2000 opened this issue May 12, 2021 · 13 comments · Fixed by #32
Closed

Placing orders (buy/sell) for funds #31

tux2000 opened this issue May 12, 2021 · 13 comments · Fixed by #32

Comments

@tux2000
Copy link
Contributor

tux2000 commented May 12, 2021

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
Copy link
Contributor

warna720 commented May 12, 2021

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:

order_book_id: str,

@tux2000 tux2000 changed the title Placing orders (BUY/SELL) for funds Placing orders (buy/sell) for funds May 12, 2021
@tux2000
Copy link
Contributor Author

tux2000 commented May 12, 2021

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
Copy link
Contributor

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

@warna720
Copy link
Contributor

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
Copy link
Contributor Author

tux2000 commented May 16, 2021

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 (#31 (comment)) seems to be strictly needed. And I left out the orderbookName in the request payload...

Maybe it helps with a proper implementation...

@tux2000
Copy link
Contributor Author

tux2000 commented May 19, 2021

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
Copy link
Contributor

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

@tux2000
Copy link
Contributor Author

tux2000 commented May 19, 2021

The requests in my comment are for a sell transaction, while your request examples in #31 (comment) is for a buy transaction.

@warna720
Copy link
Contributor

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

@tux2000
Copy link
Contributor Author

tux2000 commented May 19, 2021

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
Copy link
Contributor Author

tux2000 commented May 20, 2021

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
Copy link
Contributor

Nice job!

@tux2000
Copy link
Contributor Author

tux2000 commented May 20, 2021

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants