DEV Community

Cover image for A change in vision | Building Stocksimpy (Devlog 4)
Suleyman Sade
Suleyman Sade

Posted on • Originally published at Medium

A change in vision | Building Stocksimpy (Devlog 4)

Imagine being able to test your strategy in less than 10 lines of Python code, rather than spending hours with other libraries. This is what will make Stocksimpy useful.

This is the 4th (technically 5th) post in my devlog series on building Stocksimpy, a lightweight Python backtesting library. Read the rest of the series here.

I’ve been developing Stocksimpy for about a month now. Along the way, I learned a lot, from how to properly document a library to how to write reusable code. Those were my original goals: I wanted to learn. But if I’m honest, learning for myself isn’t enough — I want to build something actually useful for others too.

This shift made me revisit the origin of Stocksimpy.

The idea came when I was building a simple stock prediction script. I could generate numbers and plots, but all I saw was just a bunch of data flowing and no real sense of whether the model was “working.” Was it actually successful, or just noise?

Naturally, I looked for existing Python backtesting tools. What I found was overwhelming: libraries that required setting up accounts, managing APIs, and losing myself among lines of configuration. These are powerful tools, but for quick experimentation, they felt like overkill. I didn’t want complexity — I wanted something lightweight, intuitive, and well-documented.

That’s how the idea Stocksimpy was born. A Python library designed to be simple enough for a weekend experiment, yet structured enough to scale as strategies get more advanced. From the start, I chose to build in public, updating the GitHub repo as the project grew.

But recently, I realised I was just re-implementing the same indicator logic that major libraries like TA-Lib already provide. That was never the purpose of Stocksimpy. So I am doing some reconfiguration on the skeleton of the library. Here is what it will look like:

  • indicators.py: Contains some simple common indicators for quick testing and development.
  • StockData: Holds the Pandas data frame related to stock data.
  • Portfolio: Contains the history of buy/sell, currently held stocks, and manages buy/sell operations.
  • Backtester: Where the user inputs their strategy and a StockData to test, where the main loop occurs.
  • Visualize: Visualizes the result and change of the total amount.

I finished coding a working draft for the first three, and will post a blog post about Portfolio soon.

As these are the main classes of my library, I am aiming for a workflow similar to the following:

def strategy():
  # This is where the user builds their own strategy
  skip

from stocksimpy import StockData, Visualize, Backtester # 1

# Import the data, supports SQLite, pd.dataframe, dict, yfinance, and more
data = StockData(df) # 2

# Create a Backtester object with $100,000 initial money
backtest = Backtester(data, strategy, initial_cap=100000) # 3

# Runs it
backtest.run_backtest() # 4

# Visualizes the cap change
Visualize.visualize(backtest) # 5
Enter fullscreen mode Exit fullscreen mode

So if this works as I expected, with only 5 lines of code it is possible to run a backtest for a strategy.

If there is anything you want me to add, or any suggestions, please reach out to me on my socials, or comment it below 👇. It really helps with improving the library.


If you would like to support me:

Thanks for reading — let me know what else you want me to add.

Top comments (0)