SkillAgentSearch skills...

Trader Backtest

High-performance backtesting engine written in C++ for evaluating trading strategies restricted to a single trading pair (e.g. BTC/USD) and finding their optimal hyper-parameters. This software gives you a full control over every single bit that goes into backtesting. Licensed under MIT.

Install / Use

npx skills add petercerno/trader-backtest

Installs into whichever agent you are using.

About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Trader Backtest

High-performance backtesting engine written in C++ for evaluating trading strategies restricted to a single trading pair (e.g. BTC/USD) and finding their optimal hyper-parameters. Licensed under MIT.

Visual Studio Code is recommended for code editing.

DISCLAIMER: ALL OPINIONS EXPRESSED HERE ARE MY OWN AND DO NOT EXPRESS THE VIEWS OR OPINIONS OF MY EMPLOYER.

DISCLAIMER: I DO NOT PROVIDE ANY FINANCIAL, INVESTMENT, LEGAL, TAX OR ANY OTHER PROFESSIONAL ADVICE. I AM NOT A BROKER, FINANCIAL ADVISOR, INVESTMENT ADVISOR, PORTFOLIO MANAGER OR TAX ADVISOR. YOU ACKNOWLEDGE AND AGREE THAT ONLY YOU ARE RESPONSIBLE FOR YOUR USE OF ANY INFORMATION THAT YOU OBTAIN FROM THIS REPOSITORY OR SOFTWARE. YOUR DECISIONS MADE IN RELIANCE ON THE SOFTWARE OR YOUR INTERPRETATIONS OF THE DATA ARE YOUR OWN FOR WHICH YOU HAVE FULL RESPONSIBILITY. YOU EXPRESSLY AGREE THAT YOUR USE OF THE SOFTWARE IS AT YOUR SOLE RISK.

Contributing

The C++ code in this repository follows Google C++ Style Guide, is clang-formatted (with Google predefined style) and thoroughly unit-tested.

Motivation

People sometimes get seduced by (day-)trading stocks or cryptocurrencies only to end up making poor decisions and loosing money. This software tries to prevent that by providing a tool to scrutinize one's trading ideas.

Note: If you ever decide to expose yourself to the highly volatile waters of cryptocurrencies then I would encourage you to consider some of the following (my own personal) recommendations:

  • Stick only to the most reputable and regulated cryptocurrency exchanges.
  • Use only strong passwords, ideally generated by a reputable password manager.
  • Always enable two-factor authentication. Avoid SMS-based authentication. Instead, use an authentication app. Backup your authentication app secrets (and/or backup codes) safely.
  • Always do your own research before investing and never invest more than you can afford to loose.
  • Avoid margin trading and derivatives.
  • Avoid single point of failure (i.e. always have a plan B when something goes wrong, whether it is a lost or forgotten password, lost or broken mobile phone, hardware wallet, PC, etc.).
  • Familiarize yourself with cryptocurrency hacks, scams, and phishing attack.
  • It is advisable to keep larger amounts of cryptocurrencies in a hardware wallet rather than trusting a centralized exchange. (There is an old saying in the crypto community that if you don’t own your keys, you don’t own your crypto.)
  • Write down the mnemonic phrase into a steel capsule. Never share your mnemonic phrase with anyone or anywhere online. Always keep it offline and safe.
  • Instead of keeping the mnemonic phrase at home, you can also put it into a bank safe deposit box. Even more secure would be to split the mnemonic phrase into two halves and put each part into a separate safe deposit box.
  • When ordering a hardware wallet, always order from the main (primary) seller website (avoid ordering from a 3rd party or ordering a second-hand hardware wallet as their security might be compromised). To conceal your own home address (and email) you can order the hardware wallet to an anonymous P. O. Box (and use a separate email for ordering).

Note: It turns out that when it comes to Bitcoin one strategy that worked well historically also happens to be the simplest one: Buy Bitcoin and HODL. For this reason we compare the performance of all trading strategies against this so-called Buy-And-HODL strategy.

So what makes trading cryptocurrencies so seductive? If you look at a price chart like the one shown below it might occur to you that if you had bought BTC at the end of March 2017 and then sold in December 2017 you would have made more than 2000%. (Moreover, there are many other cryptocurrencies that would offer even higher returns.) Although technically correct, let's break down why this is not such an easy task.

BTC/USD 2017 Bull Market

BTC/USD 2017 Bull Market, provided by TradingView.

First of all, in order for anyone to commit to execute any concrete action (such as buying BTC) one needs to (or at least should) define under which circumstances they would be willing to do that. Once the buying / selling policy is defined, it should be possible to evaluate the performance of this policy over historical data. Keep in mind, however, that the estimated historical performance is not guaranteed to persist in the future (especially when overfitting). Therefore, evaluating one's trading strategy over many (diverse) historical periods is highly recommended.

It turns out that it is not that easy to define buying / selling rules that would capture that 2000% BTC gain in 2017. For example, one would experience at least five 30% - 40% price corrections during this period often happening in a matter of few days. Such corrections could easily wipe anyone who traded with margin (even with moderate leverage).

Exchanges

In this section we provide a brief (but over-simplified) overview of cryptocurrency exchanges and make some assumptions for our use case. We focus solely on a single trading pair (e.g. BTC/USD) on a single centralized exchange. The first listed (crypto) currency of the trading pair is called the base currency (BTC), and the second currency is called the quote currency (USD) (see also this currency pair definition). Contrary to popular belief, interacting with a centralized cryptocurrency exchange almost never involves interacting with a blockchain. The only exceptions are cryptocurrency deposits and withdrawals, which always require a blockchain transaction. Centralized exchanges have internal databases with all account balances and orders of all customers. Thus trading a cryptocurrency is often a very fast operation (as only the internal (centralized) database needs to be updated). Moreover, for security reasons the centralized exchanges often store most of their customers' funds in offline (cold) storages.

Note: There are many (for the most part Ethereum-based) decentralized exchanges (DEXes) like Uniswap, Curve Finance, etc. and decentralized lending platforms like Aave, Maker, Compound, etc. now very popular in the DeFi ecosystem. One notable difference (w.r.t. the centralized exchanges) is that interacting with these DEXes (and other protocols) requires interacting with the Ethereum blockchain (e.g. via a web3 wallet like MetaMask or web3 API like web3.js) and is typically subject to gas fees (although there are several ongoing Ethereum scaling projects and also Eth2.0 that might reduce these fees significantly). Interestingly, it is possible to execute arbitrage trades over multiple DEXes, as one can write a smart contract that interacts with all these DEXes / lending platforms in a single Ethereum transaction. On the other hand, when doing arbitrage trading on centralized exchanges one needs to move their funds (cryptocurrencies / fiat currencies) between exchanges, which is a non-trivial process. For this reason we do not support arbitrage trading.

A cryptocurrency exchange can be viewed as a (market)place where buyers meet with sellers in order to exchange their cryptocurrencies for fiat (or other cryptocurrencies) and vice versa. Having an exchange as a facilitator of trades is beneficial, as the participants do not need to trust (or even know) each other in order to safely execute their trades. They only need to trust the exchange itself. (The DEXes go even further as the participants do not even need to trust the exchange. They only need to trust the (byte)code behind the smart contract implementing the exchange (deployed at a specific contract address), see e.g. the source code for Uniswap. They also need to trust the correctness of the compiler (e.g. Solidity) to produce correct and secure bytecode, and also the correctness and security of the whole Ethereum blockchain and their EVM.)

When restricting to a single trading pair (e.g. BTC/USD), the main data structure of a centralized exchange is the so-called order book, which contains the list of all buy orders (bids) and the list of all sell orders (asks). The exchange participants can provide the so-called liquidity into the exchange by locking their funds into these bids or asks via the so-called limit orders (free of charge). (Side note: It is also possible to provide liquidity into DEXes by locking ETH or other supported ERC-20 tokens like DAI (stable coin pe

Related Skills

View on GitHub
GitHub Stars59
CategoryDevelopment
Updated1mo ago
Forks11

Languages

C++

Security Score

95/100

Audited on Jul 9, 2026

No findings