Codementor Events

Your First Stock Trading Bot πŸ€–πŸPart 2: Buy & Sell Stocks in Python w/ Alpaca!

Published Jul 14, 2020Last updated Jul 15, 2020
Your First Stock Trading Bot πŸ€–πŸPart 2: Buy & Sell Stocks in Python w/ Alpaca!

Written by: Blade Nelson

Read the first tutorial if you haven't already!

Algo Trading 101: Your First Stock Bot in Python 🐍

After installing the alpaca_trade_api library in Python, we are ready to place buy & sell orders! This will allow us to simulate profit & loss in our algorithms!

Video: https://www.youtube.com/watch?v=gWl5HFXO8P4

Download Source Code Button.png

ALPACA VIDEO THUMNAIL.png

Introduction to buy & sell orders:

When trading stocks, we have many different order types including limit orders and market orders. Each different type serves a unique purpose and is used in different scenarios.

What are market orders & limit orders?

There are two main order types you need to consider when algo trading:
market orders & limit orders.

A market order is executed immediately, at the current market price.

Market orders are used when it's more important to you that the order goes through quickly, rather than at a great price. A market order is the simplest of the order types, and is therefore the easiest to use. Making it a good starting point for begineers.

A limit order is executed only when the stock reaches a set price. A price set by the trader.
Limit orders are more commonly used by advanced traders because it allows for the ability to specify a set price that you want to enter at. This also makes profit calcuations and exit targets easier to calculate. Making this well suited for firms and more experienced traders.

How to Place a Buy Order in Python:

To place a market order in Alpaca, use the following starter code, as well as your API Keys obtained from right panel on the Alpaca Paper Trading dashboard. (Make an account first! )

Download Source Code Button.png

import alpaca_trade_api as tradeapi
import time

key = "<YOUR KEY HERE>"
sec = "<YOUR SECRET KEY HERE>"

#API endpoint URL
url = "https://paper-api.alpaca.markets"

#api_version v2 refers to the version that we'll use
#very important for the documentation
api = tradeapi.REST(key, sec, url, api_version='v2')

#Init our account var
account = api.get_account()

#Should print 'ACTIVE'
print(account.status)

#Place buy order:
#When placing orders, use the 'api' object we define above^
#api.submit_order() allows you to pass params to place orders
#Example buy order:

#All args in the submit_order() MUST BE STR!
#Symbol means the stock ticker (FB, AAPL, IBM)
#qty means quantity
#side means buy or sell
order = api.submit_order(symbol="FB",
                         qty="10",
                         side="buy",
                         type="limit",
                         limit_price="180.15",
                         time_in_force="day")

print(order)


Feel free to leave a comment here if you have any questions! πŸ˜ƒ

Discover and read more posts from Blade Nelson
get started
post commentsBe the first to share your opinion
Z fisher
3 years ago

Great video! How do you analyze market data from an API? Also, is there any market data APIs that you would recommend?

Vinay Kakkera
3 years ago

Awesome video, I am following your example and my order just says accepted and not filled. Do you know why that is?

Vinay Kakkera
3 years ago

I am using the market price.

David Bendet
4 years ago

Awesome, always wanted a Python stock trading bot :) thanks for the source code too!!! Do you have any more GitHub examples?

Blade Nelson
4 years ago

Hey David, thanks for responding. Please follow my GitHub here: https://github.com/powderblock

Show more replies