# Optimize a Trading Strategy: An Introduction

Welcome to our new series about how to optimize a trading strategy. This introductory article was prepared specifically for algorithmic traders who possess ample trading experience but sometimes get confused and possibly mislead by mathematics and statistics. The general audience can also benefit from reading this article by learning which statistical approaches are likely NOT to work in real life. In particular, we’ll disseminate, in the context of algorithmic trading, what it really means by “optimization” and what it really means when we say one set of parameters is “better” than another. The methodology discussed here is generally applicable to all kinds of trading algorithms. Let’s get started.

Mathematical optimization is the selection of a best element, with regard to some criterion, from some set of available alternatives.

Cool. Sounds too complicated? Let’s look at a concrete example: optimize a simplified Avellaneda & Stoikov’s market making strategy. The constants we’d like to optimize are the minimum spread and the maximum spread. They are called `SPREAD_PROPORTION_MINIMUM` and `SPREAD_PROPORTION_MAXIMUM`, respectively in that model. Assume we have 3 candidates: a. minimum spread = 0.001, maximum spread = 0.01, b. minimum spread = 0.002, maximum spread = 0.02, c. minimum spread = 0.003, maximum spread = 0.03. The frozen historical market dataset is coinbase’s btc-usd’s best bids/asks and trades from 2021–07–01T00:00:00Z to 2021–08–01T00:00:00Z. Quick download of historical data can be achieved by a script: https://github.com/crypto-chassis/ccapi/blob/v5.10.0/app/src/spot_market_making/config.env.example#L31.

`python3 download_historical_market_data.py --exchange coinbase --base-asset btc --quote-asset usd --start-date 2021-07-01 --end-date 2021-08-01 --historical-market-data-directory <any-location-you-like>`

Now let us draw sub-samples from it such that each sub-sample contains 2 day’s of data:

`sub-sample-1: 2021–07–01T00:00:00Z to 2021–07–03T00:00:00Zsub-sample-2: 2021–07–02T00:00:00Z to 2021–07–04T00:00:00Z...sub-sample-30: 2021–07–30T00:00:00Z to 2021–08–01T00:00:00Z`

For constants candidate a, let us run our market making program’s executable `spot_market_making` using each sub-sample and record the corresponding final account balances. For 30 sub-samples, we get 30 such balances : \$7786, \$8725, …, \$8565 (Here we have converted final BTC to USD using the mid price at the final moment). Their average value is \$8658. This is the expected value of B for constants candidate a. Now repeat the same for the other two constants candidate. We get that the expected value of B for constants candidate b is \$9737, and the expected value of B for constants candidate c is \$9959. Because our criterion is maximization of the expected value of B, we’d select constants candidate c as the best one.

• What is the appropriate amount of historical data to use?
• What might be other ways to create a random-variable dataset from the frozen historical dataset?
• In those backtests, we used an initial BTC balance of 0 and an initial USD balance of 10000. What if we change those initial balances to some other values?
• If the sub-samples are changed to contain 3 days of data rather than 2 days, does that change our selection on the best candidate?
• If our optimization target is not the maximization of the expected value of B but rather the minimization of the standard deviation of B, what’s the consequence and interpretation of that? What other optimization targets can be used?
• Grid search is exhaustive but very time-consuming. What other search algorithms can be used to accelerate the process of finding the best candidate?

^_^ If you are interested in our work or collaborating with us, join us on Discord https://discord.gg/b5EKcp9s8T. 🎉

Disclaimer: This is an educational post rather than investment/financial advice.

--

--

## More from Open Crypto Trading Initiative

An ongoing effort to embrace and promote open solutions for the crypto financial services industry