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

Implement sell buy (Market & Limit) operations #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

notAmine
Copy link

Hello, I tried to implement the different buy / sell operations. I'm opening this PR for feedback and for help debugging the issue of not being able to submit orders due to the ' {"error":"quantity is invalid."} ' mysterious error!

@Zechtitus
Copy link

Zechtitus commented Feb 12, 2021

@notAmine Think I finally figured out what was going wrong with the post
basically the gotcha is that the post needs to have json=data.

here is a code snippet from my code where I am just making my own requests call - adding the auth header and then doing the call. Works for me

   def limit_buy(self, account_id, security_id, price, quantity):
        data = {
            "account_id": str(account_id),
            "quantity": quantity,
            "security_id": str(security_id),
            "order_type": "buy_quantity",
            "order_sub_type": "limit",
            "time_in_force": "day",
            "market_value": float(price),
            "limit_price": float(price)
        }
        try:
            import requests
            url = "https://trade-service.wealthsimple.com/orders"
            session = self.ws.session
            headers = {
                'Authorization': session.headers['Authorization']
            }
            response = requests.post(url, json=data, headers=headers)
            response = response.json()
            return response
        except Exception as e:
            print(e)

in your own code I attempted to give an example of how to fix - think this is what you need

    def place_order(self, order: dict) -> dict:

        """Posts an order (market) to Wealthsimple API
        Parameters
        ----------
        :param order:
            The order dict containing the data to be submitted
        Returns
        -------
        dict
            A dict representing the  submitted order
        """
        url = "{}/orders".format(self.TradeAPI.APIMAIN)
        headers = {
            'Authorization': self.session.headers['Authorization']
        }
        response = requests.post(url, json=order, headers=headers)
        response = response.json()
        return response["results"]

@notAmine
Copy link
Author

Thank you @Zechtitus! I moved on to different projects but I hope this code benefits others :)

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 this pull request may close these issues.

2 participants