Skip to content

This is a quick python tutorial on how to setup a trading bot connected with Alpaca Trading, using Lumibot, allowing to start a trading bot with no actual money, for educational purposes.

plumti/alpaca-trading-bot-with-lumibot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Basic Trading bot using Alpaca Trading and Lumibot API

This is a quick python tutorial on how to setup a trading bot connected with Alpaca Trading, using Lumibot, allowing to start a trading bot with no actual money.

Disclaimer: This tutorial is for educational purposes only and should not be interpreted as trading advice. The provided code and datasets are used for visualization and experimentation purposes and may not represent a realistic trading bot.

By following the steps below, you'll be able to understand the code and get started with your trading bot project.

Installation

Step 1: Create an Alpaca Account: Sign up here Fill out the required information to create your Alpaca account. Once your account is created, log in to your Alpaca dashboard.

Step 2: Generate API Key and Secret: Here's how to do it: In your Alpaca dashboard, navigate to the "Quick Trade" section. You can find it in the top navigation bar or in the sidebar menu, depending on the dashboard's layout. Generating API Key and Secret

Click on "View API Keys" to generate a new API key and secret pair. Generating API Key and Secret

Once generated, make sure to copy and securely store your API key and secret. You won't be able to access the secret again after leaving this page.

Step 3: Configuring Your Trading Bot: With your Alpaca API key and secret in hand, you're now ready to configure your trading bot using the Lumibot framework. I recommend looking at the "get started" part of the documentation at this stage: here.

Have a look at lumibot's documentation it explains it all

#This is before you run the code, assuming you already have python installed (e.g python and command line, or an IDE such as Visual Studio Code with python and its terminal)
pip install lumibot
--------------------------------------------
from lumibot.brokers import Alpaca 
from lumibot.strategies import Strategy
from lumibot.traders import Trader
from lumibot.entities import Asset
ALPACA_CONFIG = {
    "API_KEY": "ENTER_API_KEY",
    "API_SECRET": "ENTER_API_SECRET",
    "ENDPOINT": "https://paper-api.alpaca.markets",
}
class MeanReversionStrategy(Strategy): 
    def initialize(self): 
        self.symbol = "AAPL"
        self.sleeptime = "1M"
    def on_trading_iteration(self):
        if broker.is_market_open():
            last_price = self.get_last_price(self.symbol)
            print(f"Last Price: {last_price:.2f}")
        else:
            print("market is closed")
if __name__ == "__main__":
    broker = Alpaca(ALPACA_CONFIG)
    strategy = MeanReversionStrategy(broker=broker)
    trader = Trader()
    trader.add_strategy(strategy)
    trader.run_all()

Can you figure out what the code from above does?

It retrieves the last price of AAPL from ALPACA trading, it runs every '1M' so every minute... here is what it looks like

What could be next?

Using a higher variety of pre-written functions listed on lumibot's documentation e.g to get out of position, buy, short stocks or even retrive historical data of prices (however this might require you to upgrade to the paid version).

Sources

About

This is a quick python tutorial on how to setup a trading bot connected with Alpaca Trading, using Lumibot, allowing to start a trading bot with no actual money, for educational purposes.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published