Lumibot: Backtesting and Algorithmic Trading
Library ON THIS PAGE
An Easy to Use and Powerful Backtesting and Trading Library for Crypto,
Getting Started
Stocks, Options, Futures and FOREX Step 1: Install Lumibot
Step 2: Create a Strategy for
Backtesting
Looking for a library that makes it easy for you to backtest your trading strategies and
Step 3: Take Your Bot Live
easily create algorithmic trading robots? Well you've found us!
All Together
Additional Resources
Lumibot is a full featured, super fast library that will allow you to easily create trading
Need Extra Help?
robots that are profitable in many different asset classes, including Stocks, Options,
Table of Contents
Futures, FOREX, and more. It is in active development and is constantly being
Indices and tables
updated to include new features and bug fixes.
If you want to make a fortune with trading, then you need a library that will make it
easy for you to check your trading strategies against historical data to make sure they
are profitable before you invest in them. Lumibot makes it easy for you to do this
(backtest) your trading strategies and easily convert them to algorithmic trading
robots.
Need Extra Help?
Visit Lumiwealth for courses, community, and
profitable pre-made trading bots.
Features
Works with Crypto, Options, Futures, Stocks, and FOREX!
We're one of the few libraries that supports Algorithmic Options
trading and backtesting, as well as many other asset classes.
Built in Live Trading
It's very easy to switch between live trading and backtesting, just a
few lines of code to go from backtesting to live trading.
Support For Top Brokers
We support many of the top brokers in the industry, including:
Binance, Coinbase, Kucoin, Alpaca, Interactive Brokers and
TradeStation. We will also support more brokers in the future.
Easy to Use and Get Started
We've built the library to be easy to use and get started in minutes.
We also offer courses and tutorials to help you with building your
own trading bot. (see our Algorithmic Trading course here)
Advanced Analyics Out of the Box
We've built a suite of advanced analytics tools that can be used to
analyze and optimize your trading strategies, including interactive
charts and more.
Event Based Backtesting
Our backtesting engine is event-based so that you can see your
strategies run as if they were live trading. You don't have to deal with
complicated and confusing vector math to see your strategies run.
Getting Started
To get started with the library, first install it on your computer by typing this into your
Terminal (on a Mac) or Powershell (on a Windows):
Terminal (on a Mac) or Powershell (on a Windows):
pip install lumibot
Getting Started
After you have installed Lumibot on your computer, you can create a strategy and
backtest it using free data available from Yahoo Finance, or use your own data. Here’s
how to get started:
Step 1: Install Lumibot
Note
Ensure you have installed the latest version of Lumibot. Upgrade using the following command:
pip install lumibot --upgrade
Install the package on your computer:
pip install lumibot
Step 2: Create a Strategy for Backtesting
Here’s some code to get you started:
from datetime import datetime
from [Link] import YahooDataBacktesting
:
from [Link] import Strategy
# A simple strategy that buys AAPL on the first day and holds it
class MyStrategy(Strategy):
def on_trading_iteration(self):
if self.first_iteration:
aapl_price = self.get_last_price("AAPL")
quantity = self.portfolio_value // aapl_price
order = self.create_order("AAPL", quantity, "buy")
self.submit_order(order)
# Pick the dates that you want to start and end your backtest
backtesting_start = datetime(2020, 11, 1)
backtesting_end = datetime(2020, 12, 31)
# Run the backtest
[Link](
YahooDataBacktesting,
backtesting_start,
backtesting_end,
)
Step 3: Take Your Bot Live
Once you have backtested your strategy and found it to be profitable on historical
data, you can take your bot live. Notice how the strategy code is exactly the same.
Here’s an example using Alpaca (you can create a free Paper Trading account here in
minutes: [Link]
from [Link] import Alpaca
from [Link] import Strategy
:
from [Link] import Trader
ALPACA_CONFIG = {
"API_KEY": "YOUR_ALPACA_API_KEY",
"API_SECRET": "YOUR_ALPACA_SECRET",
"PAPER": True # Set to True for paper trading, False for live trading
}
# A simple strategy that buys AAPL on the first day and holds it
class MyStrategy(Strategy):
def on_trading_iteration(self):
if self.first_iteration:
aapl_price = self.get_last_price("AAPL")
quantity = self.portfolio_value // aapl_price
order = self.create_order("AAPL", quantity, "buy")
self.submit_order(order)
trader = Trader()
broker = Alpaca(ALPACA_CONFIG)
strategy = MyStrategy(broker=broker)
# Run the strategy live
trader.add_strategy(strategy)
trader.run_all()
Important
Remember to start with a paper trading account to ensure everything works as expected before
moving to live trading.
All Together
Here’s the complete code:
from datetime import datetime
from [Link] import YahooDataBacktesting
:
from [Link] import Alpaca
from [Link] import Strategy
from [Link] import Trader
ALPACA_CONFIG = {
"API_KEY": "YOUR_ALPACA_API_KEY",
"API_SECRET": "YOUR_ALPACA_SECRET",
"PAPER": True
}
class MyStrategy(Strategy):
def on_trading_iteration(self):
if self.first_iteration:
aapl_price = self.get_last_price("AAPL")
quantity = self.portfolio_value // aapl_price
order = self.create_order("AAPL", quantity, "buy")
self.submit_order(order)
trader = Trader()
broker = Alpaca(ALPACA_CONFIG)
strategy = MyStrategy(broker=broker)
# Run the strategy live
trader.add_strategy(strategy)
trader.run_all()
Or you can download the file here:
[Link]
tart_single_file.py
Additional Resources
If you would like to learn how to modify your strategies, we suggest that you first learn
about Lifecycle Methods, then Strategy Methods, and Strategy Properties. You can
find the documentation for these in the menu, with the main pages describing what
they are, then the sub-pages describing each method and property individually.
We also have some more sample code that you can check out here:
[Link]
We wish you good luck with your trading strategies. Don’t forget us when you’re
swimming in cash!
Need Extra Help?
:
Need Extra Help?
If you need extra help building your strategies and making them profitable,
Lumiwealth has you covered. By visiting Lumiwealth, you not only learn how to use
the Lumibot library but also gain access to a wealth of highly profitable algorithms.
Our strategies have shown exceptional results, with some achieving over 100% annual
returns and others reaching up to 1,000% in backtesting. Join us to learn from our
expertise and take your trading to the next level.
Visit Lumiwealth
Why Choose Lumiwealth?
By joining Lumiwealth, you'll learn to:
Create and implement your own trading algorithms
Access a library of high-performing strategies
Learn from experts with proven track records
Achieve potentially high returns on your investments
Discover More at Lumiwealth
Important
Build Trading Bots with AI
:
Build Trading Bots with AI
Want to create trading bots without writing code? Visit BotSpot - our platform for building, testing,
and deploying trading strategies using AI!
Create strategies using natural language
Backtest on historical data
Deploy to live markets
Join a community of algorithmic traders
Get started at [Link]
Table of Contents
Home
Build Bots with AI
GitHub
Discord Community
Get Pre-Built Profitable Strategies
Deployment Guide
Example Strategy for Deployment
Choosing Your Deployment Platform
Deploying to Render
Deploying to Replit
Secrets Configuration
Broker Configuration
Tradier Configuration
Alpaca Configuration
Coinbase Configuration
Kraken Configuration
Interactive Brokers Configuration
Interactive Brokers-Legacy Configuration
General Environment Variables
Final Steps
Conclusion
What is Lumibot?
Lumiwealth
Getting Started With Lumibot
Step 1: Install the Package
Step 2: Import the Following Modules
:
Step 3: Create an Alpaca Paper Trading Account
Step 4: Configure Your API Keys
Step 5: Create a Strategy Class
Step 6: Instantiate the Trader, Alpaca, and Strategy Classes
Step 7: Backtest the Strategy (Optional)
Step 8: Run the Strategy
Adding Trading Fees
Profiling to Improve Performance
Lifecycle Methods
Summary
def initialize
:
Reference
def on_trading_iteration
Reference
def before_market_opens
Reference
def before_starting_trading
Reference
def before_market_closes
Reference
def after_market_closes
Reference
def on_abrupt_closing
Reference
def on_bot_crash
Reference
def trace_stats
Reference
def on_new_order
Reference
def on_partially_filled_order
Reference
def on_filled_order
Reference
def on_canceled_order
Reference
def on_parameters_updated
Reference
Variable Backup & Restore
How It Works
Benefits of [Link]
Usage Guide
Database Configuration
Database Storage Structure
Strategy Methods
Order Management
self.create_order
self.submit_order
:
self.submit_orders
self.cancel_order
self.cancel_orders
self.cancel_open_orders
self.get_selling_order
self.sell_all
self.get_order
self.get_orders
self.get_asset_potential_total
Account Management
self.get_portfolio_value
self.get_cash
self.get_position
self.get_positions
Data
self.get_last_price
self.get_last_prices
self.get_historical_prices
self.get_historical_prices_for_assets
self.get_quote
self.get_yesterday_dividend
self.get_next_trading_day
Chart Functions
self.add_marker
self.add_line
self.get_markers_df
self.get_lines_df
Parameters
self.get_parameters
self.set_parameters
Options
self.get_chain
self.get_chains
:
self.get_greeks
self.get_strikes
self.get_expiration
self.get_multiplier
self.options_expiry_to_datetime_date
self.get_next_trading_day
DateTime
self.get_datetime
self.get_timestamp
self.get_round_minute
self.get_last_minute
self.get_round_day
self.get_last_day
self.get_datetime_range
self.localize_datetime
self.to_default_timezone
Miscellaneous
self.log_message
[Link]
self.set_market
self.update_parameters
self.get_parameters
self.await_market_to_close
self.await_market_to_open
self.wait_for_order_registration
self.wait_for_order_execution
self.wait_for_orders_registration
self.wait_for_orders_execution
Strategy Properties
[Link]
[Link]
:
self.portfolio_value
Strategy.portfolio_value
self.first_iteration
Strategy.first_iteration
self.is_backtesting
Strategy.is_backtesting
self.quote_asset
Strategy.quote_asset
[Link]
[Link]
self.initial_budget
Strategy.initial_budget
self.minutes_before_closing
Strategy.minutes_before_closing
self.minutes_before_opening
Strategy.minutes_before_opening
[Link]
[Link]
self.last_on_trading_iteration_datetime
Strategy.last_on_trading_iteration_datetime
[Link]
[Link]
[Link]
[Link]
self.unspent_money
Strategy.unspent_money
Entities
Asset
Documentation
:
Bars
Documentation
Data
Data
Order
Advanced Order Types
Order With Legs
Documentation
Position
Position
Trading Fee
TradingFee
Backtesting
Files Generated from Backtesting
How To Backtest
Files Generated from Backtesting
Backtesting Function
Yahoo
Pandas (CSV or other data)
[Link] Backtesting
ThetaData Backtesting
Tearsheet HTML
Trades Files
Indicators Files
Logs CSV
Brokers
Alpaca
Documentation
:
Interactive Brokers
Market Data Subscriptions
Two-Factor Authentication (2FA)
Using a Secondary Username
Using a Paper Trading Account
Strategy Setup
Example Strategy
Interactive Brokers Legacy
Crypto Brokers (Using CCXT)
Configuration Settings
Running Your Strategy
Full Example Strategy
Tradier
Getting Started
Configuration
Running Your Strategy
Full Example Strategy
Indices and tables
Index
Module Index
Search Page
Copyright © 2021, Lumiwealth
Made with Sphinx and @pradyunsg's Furo
: