Codementor Events

Algo Trading 101: Building Your First Stock Trading Bot in Python πŸ€–πŸ

Published Apr 10, 2020Last updated Oct 07, 2020
Algo Trading 101: Building Your First Stock Trading Bot in Python πŸ€–πŸ

Written by: Blade Nelson

If you want to host your bot, I personally recommend this: TreeHost.io They use eco friendly servers so you can save the planet while your algo makes you money.

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

It can be overwhelming for a new Python developer to get started with algorithmic trading. I would know because I have been there too! When I began my algo trading journey back in 2017, I had the oppurtunity to fly out to NYC and attend QuantCon, a convention run by one of the largest algo trading funds on the planet: Quantopian. I went to a dozen talks by some of the best quants (short for quantitatives, aka the people doing math and writing strategies for algo funds.) This journey taught me a lot but also left me with a lot of questions: How do I write a bot that can trade stocks? What strategies can I use to be profitable? Trading stocks with an algorithm is no walk in the park. So in this article let's break down the core components of how you build an algo trader.

Requirements for this project:

Download Source Code Button.png

ALPACA VIDEO THUMNAIL.png

What is Algo Trading?

Trading algorithms or trading algos allow a computer to buy and sell stocks on the stock market. These buys & sells rely on calculations and logic written in programming languages. (Example: If a stock's price drops 5% in 1 hour, buy it)

The objective of a trading algorithm is consistent profit while minimizing your risk, and tracking your investment portfolio automatically so you don't have to.

The previous company I mentioned Quantopian used to be my favorite algo trading platform but was plauged by speed problems. So I've always been looking for a Quantopian alternative. After many years of research, I met some people using a new platform called alpaca.markets and it turned out to be really cool!
alpaca_logotype_black.png

Welcome to Alpaca

My favorite stock API is alpaca.markets which has native bindings in Python. Combine Python with realtime stock data and trading with up to 200 requests per every minute per API key. This is a very powerful tool which didn't exist two or three years ago. Alpaca also allows us to buy and sell stocks in the live market in a paper trading account. This paper trading feature lets you test your strategies without ever risking real money on your trades.

Install the alpaca_trade_api via pip by using the following command in terminal:

pip3 install alpaca-trade-api

Need help installing the Alpaca API? https://pypi.org/project/alpaca-trade-api/

Make sure you have an Alpaca.markets account before continuing this tutorial. They are free to open and you can begin testing without depositing any money.

AFTER you create an account

alpaca dashboard.png
This is the Alpaca paper trading dashboard. It shows you your paper positions and how your portfolio is performing.

API Keys.png

On the right side of the dashboard, you can see your API keys. These will be used in Python so that we can authenicate with your Alpaca account and interacte with your portfolio via an API.

Making your first Alpaca call:

import alpaca_trade_api as tradeapi
import time

key = "PKM01EIB1JJ0YX16UZJA"
sec = "DoGEuj7JxWjbKzlAFM5VaHhCUNrQX0SFaDPMHOjt"

#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()

print(account.status)

This script will connect to the Alpaca API using your credentials. It will then print the status of your account.

If this script outputs "ACTIVE" then you're ready to move on to the next tutorial! (Coming soon! Follow me to stay tuned!!!)

Part 2: https://www.codementor.io/@powderblock/15i9btwqyi

Discover and read more posts from Blade Nelson
get started
post commentsBe the first to share your opinion
Daniel K
5 months ago

When purchasing from a supplier in another country, it’s essential to navigate the complexities of international trade. For valuable insights and tips on becoming a successful global importer, check out https://arbstore.org/blog/post/Join-ArbStore-as-an-Arbiter, where experts provide guidance and support to navigate the intricate world of cross-border commerce. Importing goods can be a strategic move for businesses looking to diversify their product offerings or reduce costs. However, it requires a keen understanding of import regulations, customs procedures, and potential challenges like currency exchange rates. To make informed decisions, consider seeking guidance from experts in international trade.

Peter Tsetelman
4 years ago

Indeed a useful article! Thank you Blade :)
If I may share my recent readings, just a little remark for those who is more interested in developing a trading bot for crypto, not for stocks - I may recommend a recent blog post from the platform, which I actively use:
https://blog.trality.com/developing-simple-trading-bot-with-trality-bot-code-editor/

Giuseppe Salvatore
4 years ago

I must this is really good as a quick-start guide. I was reading about algorithms and theory behind financial trading but didn’t get the chance to have anything running so far. Even if it’s a quick test (well… If we don’t consider the time to create an account in Alpaca, there’s a lot of agreements to read) it’s good to have a local python script interacting with a broker. Can’t wait for the next post. Just checking, do you have a Udemy course by chance? If not you should plan to make one, your style will be a success there

Giuseppe Salvatore
4 years ago

Do you know where Alpaca makes money from if not from fees?

Show more replies