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
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 thecurrent 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! )
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!
Great video! How do you analyze market data from an API? Also, is there any market data APIs that you would recommend?
Awesome video, I am following your example and my order just says accepted and not filled. Do you know why that is?
I am using the market price.
Awesome, always wanted a Python stock trading bot :) thanks for the source code too!!! Do you have any more GitHub examples?
Hey David, thanks for responding. Please follow my GitHub here: https://github.com/powderblock