In this tutorial series, we will learn about how to place a market order to buy or sell a U.S. Equity or ETF. We will move forward assuming that you’ve registered with Alpaca. If you haven’t please visit Alpaca and sign up. Feel free to check out the other tutorials in this series at Alpaca Resources.

Alpaca - Commission-Free API First Stock Brokerage
Alpaca is a modern platform for trading. Alpaca’s API is the interface for your trading algorithms, bots, or applications to communicate with Alpaca’s brokerage and other services.

We will use an Alpaca paper trading account to demonstrate how to buy and sell a stock,  via the API.


Step 1:

Log in to your Alpaca account and click on “Paper Overview”. That should lead to a web page as shown in the picture below.

As we can see, a new paper trading account has a starting equity of $100,000 and a buying power of $400,000. These can be reset at any time during your usage by clicking the “Reset” button and they will return back to their original equity of $100,000 and a buying power of $400,000 respectively.

Paper Trading Equity
Paper Trading Equity 

Step 2:

Let’s create a class called “pythonBuyBot” and paste our code for the endpoints in the class.

pythonBuyBot with our API Keys
pythonBuyBot with our API Keys

Step 3:

We will create a function called “pythonBuyBot” that places a market order to buy a stock.  Similar code can be found on the Alpaca Docs under the orders section.

pythonBuyBot with our API Keys
pythonBuyBot with our API Keys

Step 4:

Similarly let’s create a function to sell a stock, or in this case one stock of Tesla. Again, similar code can be found on the Alpaca Docs under the orders section.

pythonBuyBot with our function to buy/sell a stock
pythonBuyBot with our function to buy/sell a stock

After placing a market order to buy and/or sell, your order history will look like the following (prices will vary)

Order History
Order History

Commission Free Algorithmic Trading...Alpaca API
Commission Free Algorithmic Trading...Alpaca API


Thank you for using Alpaca. Here are a couple more links that  you might be interested in.

Alpaca GitHub repository

Alpaca
Alpaca builds an API for free stock trading. Alpaca has 39 repositories available. Follow their code on GitHub.

Alpaca Blog

Alpaca Blog | API for Stock Trading
Alpaca Blog for product updates, new integration, and more. Alpaca’s commission-free trading API is built for algorithmic trading and building apps

Alpaca Forum

Alpaca Community Forum
Alpaca user forum for FAQ, feature requests, many discussions around Alpaca stock trading API

Alpaca API documentation

Welcome
This page will help you get started with Alpaca Docs. You’ll be up and running in a jiffy!

You can also follow Alpaca and our weekly updates on our LinkedIn, Alpaca Community Slack and @AlpacaHQ on Twitter!

Commission-Free trading means that there are no commission charges for Alpaca self-directed individual cash brokerage accounts that trade U.S. listed securities through an API. Relevant SEC and FINRA fees may apply.

Brokerage services are provided by Alpaca Securities LLC ("Alpaca"), memberFINRA/SIPC, a wholly-owned subsidiary of AlpacaDB, Inc. Technology and services are offered by AlpacaDB, Inc.