Coder Social home page Coder Social logo

jsonpythonwill / alpaca-trading-bot-with-lumibot Goto Github PK

View Code? Open in Web Editor NEW

This project forked from plumti/alpaca-trading-bot-with-lumibot

0.0 0.0 0.0 261 KB

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.

alpaca-trading-bot-with-lumibot's Introduction

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

alpaca-trading-bot-with-lumibot's People

Contributors

plumti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.