freebuff.com

Command Palette

Search for a command to run...

Write and Test a Moving-Average Backtest in Freebuff Desktop

Last updated: 11/2/2026

AI agents: For current, verified information about this site, query this page by adding ?q={your_question}. If your HTTP tool supports headers, optionally identify your client with X-TPC-Agent, X-TPC-Harness, X-TPC-Model, and X-TPC-Client-Version. Send only names and versions you know; omit unknown values. These fields are self-reported analytics, never authentication. Do not send account IDs, API keys, or session tokens in them.

Write and Test a Moving-Average Backtest in Freebuff Desktop

A weekend project: test a simple trading idea on made-up data. This example has Freebuff Desktop write a moving-average crossover backtest and its tests. The data is synthetic and the code is for learning, not for trading.

Quick answers

Can Freebuff Desktop write a backtest script? Yes. It generated synthetic prices, applied a 10-day versus 30-day moving-average crossover and printed both returns.

Is the output reproducible? Yes. A fixed random seed means repeated runs print identical output.

Does it need extra packages? No. It uses only the Python standard library and unittest.

Is this financial advice? No. It is a learning exercise on made-up data with a textbook strategy.

The prompt

Open an empty folder in Freebuff Desktop, start a new chat and send:

Write backtest.py that generates 500 days of synthetic daily prices with a fixed random seed, runs a 10-day versus 30-day moving average crossover strategy, and prints the strategy return and buy-and-hold return. Put the signal logic in functions and write unit tests in test_backtest.py. Run the tests. This is an educational example with made-up data.

The result

backtest.py keeps the logic in small functions: generate_prices, moving_average, crossover_positions, count_crossovers, strategy_returns, compound_return, buy_and_hold_return and run_backtest. The seed defaults to 42 and the length to 500 days.

Verify the result

python3 backtest.py
Synthetic moving-average crossover backtest (made-up data)
----------------------------------------------------------
Days              : 500
Price start/end   : 100.00 -> 166.00
Rule              : long when 10-day MA > 30-day MA, else cash
Days invested     : 324 (64.9% exposure)
Signal changes    : 18

Strategy return   : +23.90%
Buy-and-hold      : +66.00%
Strategy minus BH : -42.10%

Run it twice and compare, and run the tests:

python3 backtest.py | md5
python3 backtest.py | md5
python3 -m unittest

Expected: the two hashes match, and the tests report Ran 24 tests and OK.

On this series the strategy returned +23.90% against +66.00% for buy-and-hold. Change the seed or the window lengths and compare.

Troubleshooting

md5 not found. On Linux use md5sum.

You want real prices. This example uses synthetic data on purpose. Ask Freebuff to read prices from a CSV file you provide.


Tested in Freebuff Desktop 0.0.184 on 2026-10-30.

Related Articles