Ready to go further
Next: a rule built on these averages
The EMA crossover page shows how a rule that compares two averages turns into entries and exits, and how often it whipsaws.
What is included
- What it is, in plain words
- The formula or mechanism, with exact arithmetic
- How it hits grid, martingale and DCA bots
- A check you can run before you switch a bot on
Who it is for
- People who run or plan to run a trading bot
- Anyone reading a backtest or a bot sales page
- Readers who want the numbers, not the pitch
- Not advice, and not a signal service
Key facts
- TA-Lib is a well known library of technical analysis functions. In general terms, such a library takes price arrays in and returns indicator arrays out.
- Two tools can both say "EMA 5" and give different numbers. Seeding, rounding and how the first bars are handled can all differ.
- In the made-up example below, the same 12 closes give an EMA of 108.84 at bar 12 with one seed and 108.87 with another (calculated by us on 29 Oct 2026, exact).
- A small gap matters when a rule compares two lines or a line and a price, because the cross can happen on a different bar.
- The cure is a check: compute a few values by hand, compare them with the library and the chart, and write down any difference.
What an indicator library is
An indicator is a formula applied to prices: an average, a range, a ratio of gains to losses. A bot needs those numbers every bar. Writing each formula yourself is slow and easy to get wrong, so many bots use a ready library. TA-Lib is one such library; others exist for most languages. This page does not compare versions or products. It covers what any such library does and the one problem that catches people out.
The usual shape is simple. You pass in arrays of open, high, low, close and volume, plus settings like the period. You get back an array the same length as the input. The first few entries are empty or zero, because an indicator with a period of 5 cannot exist before bar 5. See RSI and Bollinger Bands for two indicators bots commonly use.
Why a library at all
- It is used and tested by many people, so obvious mistakes are rarer than in a hand-written version.
- It is fast on long histories, which matters in a backtest.
- It keeps backtest code and live code on the same formula.
Why the definition must match your chart
An indicator name is not a full specification. "Moving average" can be simple, exponential or weighted. "RSI" can smooth its averages in more than one way. "ATR" and "Stochastic" have settings that differ by platform. Even for the same formula, the start of the series can differ: the first value of an exponential average has to come from somewhere.
The chart you looked at when you designed the rules uses one definition. If the bot's library uses another, the rules you tested by eye are not the rules the bot runs. The same trap shows up when the backtest tool and the live platform use different code for the same indicator.
- Seeding. Is the first exponential value the first close, or the average of the first N closes?
- Smoothing type. Some indicators use a plain average, others a recursive smoothing with a different constant.
- Price used. Close, typical price, or something else. A setting hidden in a menu can change it.
- Warm-up. Recursive indicators carry a memory of earlier bars. Starting the history later gives slightly different numbers for a while.
- Bar in progress. A live platform may include the unfinished bar. A library fed only finished bars will not. This links to look-ahead bias.
A worked example: a moving average check on 12 made-up closes
These 12 closing prices are made up. The period is 5, so the simple average (SMA) is the mean of the latest 5 closes. The exponential average (EMA) uses weight 0.33 for the newest close, which is 2 divided by 6, and 0.67 for the previous EMA. The column "EMA seed SMA" starts at bar 5 with the SMA. The column "EMA seed close" starts at bar 1 with the first close. Everything was calculated by us on 29 Oct 2026 and is exact to two decimals.
| Bar | Close | SMA 5 | EMA seed SMA | EMA seed close |
|---|---|---|---|---|
| 1 | 100 | - | - | 100.00 |
| 2 | 102 | - | - | 100.67 |
| 3 | 101 | - | - | 100.78 |
| 4 | 103 | - | - | 101.52 |
| 5 | 105 | 102.20 | 102.20 | 102.68 |
| 6 | 104 | 103.00 | 102.80 | 103.12 |
| 7 | 106 | 103.80 | 103.87 | 104.08 |
| 8 | 108 | 105.20 | 105.24 | 105.39 |
| 9 | 107 | 106.00 | 105.83 | 105.92 |
| 10 | 109 | 106.80 | 106.89 | 106.95 |
| 11 | 111 | 108.20 | 108.26 | 108.30 |
| 12 | 110 | 109.00 | 108.84 | 108.87 |
Read the table. At bar 5, the SMA is 102.20. The EMA seeded with that SMA is also 102.20, while the EMA seeded with the first close is 102.68, a gap of 0.48. By bar 12 the gap has shrunk to 0.03, because older bars fade out. The SMA at bar 12 is 109.00.
A small gap is not harmless. Suppose a rule says "buy when the close is above the EMA". On a day when the close lands between the two EMA values, one version says buy and the other says no. Across a long backtest that is a different list of trades.
How to check a library against your chart
- Pick a short, quiet stretch of prices and export it, with times.
- Compute one indicator by hand or in a spreadsheet, for five or six bars, using the stated formula.
- Read the same bars off the chart's data window.
- Run the library on the same bars with the same settings and price type.
- Compare to the price precision you trade. Write down the gap and its cause.
- Repeat after the history has warmed up, since recursive values converge.
If the three agree after the warm-up, the definition matches. If not, change the bot's formula to match the chart, or change the chart's. Either is fine; what matters is a single definition everywhere, and a trading journal line recording which one you used.
What a library does not tell you
A matching indicator only means the numbers are right. It says nothing about whether a rule built on them has any edge. See overfitting for how rules fit to past data fail, and EMA crossover for a rule that uses the average from this page.
Keep Going
Related Tools and Guides
Keep exploring
FAQ
Frequently Asked Questions
What is TA-Lib?
It is a library of technical analysis functions that turn price arrays into indicator arrays, such as moving averages and RSI. Similar libraries exist for most programming languages.
Why does my indicator differ from the chart?
Common causes are a different seed for the first value, a different smoothing type, a different price input, or a chart that includes the unfinished bar.
Does a library make my bot more accurate?
It makes the indicator numbers consistent and tested. It does not make the rules around them profitable.
How do I test an indicator function?
Compute a few values by hand for a short list of prices, run the function on the same list, and compare to the price precision you trade.
Do I need to match to many decimals?
Match to the precision where a rule could flip, such as a cross of two lines. Recursive indicators converge, so differences shrink after a warm-up.
Are the numbers on this page real?
Example numbers are made up to show how the maths works, and the page says so. A figure only counts as exact when the page says it was calculated on a stated date. Nothing you enter in a calculator leaves your browser.
Can I use this to decide whether to run a bot?
It can show whether a bot's numbers hold together and how much an account can lose. It cannot say what the market will do, and it is not advice. Use it to ask better questions, then read the terms of your own broker or exchange.
Is this financial advice?
No. It is general education about indicator code. The prices are made up and no result here predicts anything.
Know the Risk Before You Run It
Free calculators and guides, added every week. Leave your details and we will email you when new ones go up.
Trading carries a risk of loss