Pyne is a system for creating programs that work with market data. Programs written in it can calculate and display values derived from prices, or define the rules of a trading strategy. In this book, we work through the whole process, from writing a program to checking its results.

What do we mean by Pyne code?

We write programs in Pyne code. It is a language designed specifically for processing market data. The code describes what the system should calculate, what it should display, and which conditions should trigger an action.

An indicator might calculate the average price of the last ten candles and draw it as a line on a price chart. A price chart shows how prices change over time. A strategy also defines entry and exit rules: for example, it opens a long position when a condition is met. Running it on historical data lets you examine the trades those rules would have produced under the specified execution conditions. This is called a backtest.

We start by building simple indicators. These let you learn about the data and the language before writing trading rules.

Pyne code uses Python’s writing rules. We call these rules syntax: they determine, for example, where parentheses go and how we mark lines that belong together. You do not need to learn Python first; you learn the rules you need through the book’s examples.

This is why files have the .py extension normally used for Python. Pyne code can also run in a Python runtime with PyneCore installed. Python provides the runtime, while PyneCore supplies Pyne’s behavior and function library.

How does a program process the data?

In our examples, market data arrives as a sequence of candles. A candle summarizes the data for a period, including its opening and closing prices. In daily data, one candle represents one day.

PyneCore runs the program’s calculations on these candles in order. For example, it can calculate an average on each candle using prices available up to that point. It also manages previous calculation values and data that must be kept between candles.

This differs from the way an ordinary Python program runs. The @pyne marker at the start of the file tells PyneCore to prepare and run the code accordingly. In the chapter explaining the code, we look at the marker’s exact form and the meaning of edge, which appears beside it in the book’s examples.

How does it differ from vectorized processing?

Python trading frameworks include both candle-by-candle and vectorized approaches. With vectorized processing, you perform operations on entire sequences of numbers: for example, you calculate the averages and buy signals for all candles at once. You often manage the data in tables or arrays.

In PyneCore, you describe what to do on the current candle. The system executes those steps and then moves to the next candle. It manages calculation history and retained state according to Pyne’s language rules.

Write the steps in a simple function

Several candle-by-candle frameworks require you to create a class for your strategy. A class groups related data and operations; the functions defined inside it are called methods. In these frameworks, you put settings and trading rules into prescribed methods and access prices or positions through data belonging to the class.

In our Pyne code examples, you write what you want to calculate and when you want to act in a main() function. Its lines run in sequence for each candle: first you calculate a value, then check a condition, and finally draw a signal or place an order. PyneCore handles stepping through the candles.

You can therefore begin without learning to create classes or use inheritance, or dividing your program among a framework’s methods. Simple examples only require variables, function calls, and conditions, which you learn in the following chapters.

Follow the rules in time order

This is close to how you think through a trading decision: “I check the current price and its movement so far. If my condition is met, I enter. While I have a position, I watch the exit condition.” You can follow events in this same order in the code and use the debugger to inspect variables at a particular candle.

Accidentally using future data is harder

With vectorized processing, the entire dataset may be available. A shift in the wrong direction or a poorly chosen average can therefore include a price that was not known at the time of the decision. For example, using a whole year’s average for January decisions would also use prices from later months. The program can still run and produce convincing results; the error may be hard to spot. This is called look-ahead bias.

Pyne’s own time series make the current candle’s values and those of already processed candles available. A history lookup cannot read the next candle’s price. This reduces the chance of accidentally using future data and makes it easier to check what information led to a decision.

You still need to take care when preparing data. If an external dataset already contains information that only became known later, candle-by-candle execution alone cannot detect that.

There can be a speed trade-off

For some programs, candle-by-candle execution can be slower than a well-written vectorized calculation. This can matter especially when you try many strategy or parameter variants on a large dataset. Vectorized systems often speed up these bulk calculations with optimized numerical libraries. The actual difference depends on the program and the amount of data.

In this book, you learn candle-by-candle execution: rules expressed in time order and how to check the information behind each decision.

For other approaches, see vectorized processing in VectorBT and candle-by-candle strategies in Backtrader.

What are PyneIDE and PyneCore for?

PyneIDE is an extension for the Visual Studio Code editor. You use it to write programs, select their data, and run them. You also view the results on a chart and investigate errors there.

PyneCore is the system that runs the programs. It loads and prepares a program, processes the candles with it, and produces the calculation and trade results. PyneIDE uses it in the background and displays those results.

In the next chapter, you set up PyneIDE and create your first project. We use sample data for learning, so you can run the same program again and compare the results of your changes.