Skip to content

QuickStart tutorial for getting started with Stock Indicators for Python. This is for developers who may be new to Python or who need clarification about setting up prerequisites.

License

Notifications You must be signed in to change notification settings

facioquo/stock-indicators-python-quickstart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Stock Indicators for Python: QuickStart Guide

A beginner's guide to setting up and using the Stock Indicators for Python library for financial market analysis.

Tip

TLDR, for the impatient, run these commands to fast-forward this tutorial:

git clone https://github.com/facioquo/stock-indicators-python-quickstart.git
cd stock-indicators-python-quickstart
python -m venv .venv
source .venv/bin/activate  # On Windows use: .venv\Scripts\activate
pip install stock-indicators
python main.py

or follow step-by-step instructions below

Install prerequisite software

Required software versions:

VS Code Extensions:

# Verify installations
python --version   # Should be ≥ 3.8
dotnet --version   # Should be ≥ 6.0

Setup your project

  1. Create a new project folder.

  2. Optional: initialize a git repository in this folder with git init bash command and add a Python flavored .gitignore file. I found this one in the gitignore templates repo.

  3. Initialize Python workspace with a virtual environment (a cached instance):

    # create environment
    python -m venv .venv
    
    # then activate it
    source .venv/bin/activate  # On Windows use: .venv\Scripts\activate

    You can also use VS Code command: Python: Create Environment ... and then Python: Select Interpreter to pick your just created venv instance. When done correctly, you should have a .venv folder in the root of your project folder. There are other ways to initialize in a global environment; however, this is the recommended approach from the Python tutorial I'd mentioned above.

  4. Install the stock-indicators package from PyPI

    # install the package
    pip install stock-indicators

    I'm using v1.3.1, the latest version. You should see it installed in .venv/Lib/site-packages.

    # verify the install
    pip freeze --local
    ...
    clr-loader==0.2.7
    pycparser==2.22
    pythonnet==3.0.5
    stock-indicators==1.3.1
    typing_extensions==4.12.2
    ...

Write the code

It's time to start writing some code.

  1. To start, add a quotes.csv file containing historical financial market prices in OHLCV format. Use the one I put in this repo. You can worry about all the available stock quote sources later.

  2. Create a main.py file and import the utilities we'll be using at the top of it.

    import csv
    from datetime import datetime
    from itertools import islice
    from stock_indicators import indicators, Quote
  3. Import the data from the CSV file and convert it into an iterable list of the Quote class.

    # Get price history data from CSV file
    with open("quotes.csv", "r", newline="", encoding="utf-8") as file:
       rows = list(csv.reader(file))
    
    # Convert rows into Quote objects that stock-indicators understands
    # CSV returns strings, but Quote needs numbers for prices and volume
    quotes = []
    for row in rows[1:]:  # skip header row
       quotes.append(
          Quote(
             datetime.strptime(row[0], "%Y-%m-%d"),  # date
             row[1],  # open
             row[2],  # high
             row[3],  # low
             row[4],  # close
             row[5],  # volume
          )
       )

    These quotes can now be used by the stock-indicators library. For a quickstart that uses pandas.DataFrame, see our online ReplIt code example for the Williams Fractal indicator.

  4. Calculate an indicator from the quotes

    # Calculate 5-period Simple Moving Average
    results = indicators.get_sma(quotes, 5)
  5. Configure results for console output

    # Show the results
    print("Date        SMA")
    for r in islice(results, 0, 30):  # show first 30 days
       sma = f"{r.sma:.3f}" if r.sma else ""
       print(f"{r.date:%Y-%m-%d}  {sma}")

Run the code

  1. Click the Run Python File in Terminal (►) play button in the top-right side of the VS Code editor to run the code, or execute from the commandline in your bash terminal. The SMA indicator output will print to the console.

    # from CLI (optional)
    python main.py
    Date        SMA
    --------------------
    2017-01-03
    2017-01-04
    2017-01-05
    2017-01-06
    2017-01-09  213.872
    2017-01-10  214.102
    2017-01-11  214.200
    2017-01-12  214.226
    2017-01-13  214.196
    2017-01-17  214.156
    2017-01-18  214.210
    2017-01-19  213.986
    2017-01-20  214.024
    ...

    The small deviations shown in these raw results are normal for double floating point precision data types. They're not programming errors. Developers will usually truncate or round to fewer significant digits when displaying. We're showing 3 decimal places here.

You've done it! That's the end of this QuickStart guide.


Common issues

  • Import errors: Ensure you've activated the virtual environment
  • Runtime errors: Verify .NET SDK installation
  • .NET loading issues: On Linux/macOS, you may need additional dependencies

Next steps

Getting help

Having trouble? Try these resources:

Share your work

Built something cool? Share it with the community!

— @DaveSkender, January 2025

About

QuickStart tutorial for getting started with Stock Indicators for Python. This is for developers who may be new to Python or who need clarification about setting up prerequisites.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages