Where the idea comes from

The bilateral filter comes from image processing. It removes noise but keeps sharp edges. The Bilateral Filter Trend indicator (by QuantAlgo on TradingView) applies the same idea to price.

A plain moving average weighs every candle in its window the same, so it lags at reversals: old prices pull it back. The bilateral filter almost ignores candles whose price is far from the current one. When the market moves sharply in a new direction, the filter switches to the new level quickly and smooths the small swings around it.

How the filter is computed

On every closed candle the filter takes the last N closes (the filter window). Each close gets a weight made of two factors:

  • Time weight: the older the candle, the smaller the weight. This is a Gaussian kernel of the candle's age; its width is the time kernel σt, in candles.
  • Price weight: the farther a close is from the reference price, the smaller the weight. The distance is measured in price kernel widths of σr × ATR(14). Candles far from the current price barely affect the filter.
filter = Σ wᵢ · closeᵢ / Σ wᵢ
wᵢ = exp(−i² / 2σt²) · exp(−½ · ((closeᵢ − reference) / (σr · ATR))²)

In our implementation the reference price is the median of the last three closes (spike rejection), so a single abnormal candle does not drag the filter along. With spike rejection off, the reference is the current close.

The filter is causal: it uses closed candles only. A closed candle's value never changes later, so the signal does not repaint.

When the trend flips

The trend remembers the filter's extreme since the last flip. In an uptrend that is the filter's high. When the filter drops below that high by more than the reversal threshold × the price kernel width, the trend turns down. A downtrend turns up the same way, from its low.

The threshold is measured in ATR, so the rule works the same in calm and volatile markets: in a volatile period the filter has to travel more dollars before the trend changes.

How the BILATERAL_FUTURES bot trades

  • One pair, USDT-M futures on Binance or OKX. Decisions are made only on closed candles of the chosen timeframe, 4h by default.
  • The bot is always in a position in the filter's direction: long in an uptrend, short in a downtrend.
  • When the filter flips, the position is closed at market and the opposite one is opened at once.
  • Position size comes from risk: risk % × capital / (stop in ATR × ATR). By default that is 2% of capital and a 3 ATR stop, with the notional capped at three times the capital.
  • The stop rests on the exchange as a stop order. After a stop-out the bot skips one candle and re-enters in the filter's current direction.
  • The trailing stop is off by default (50 ATR). When it is on, after a trailing exit the bot waits for the filter to flip instead of re-entering the same trend at once (this can be turned off). That cuts needless entries: on 12h with a 3 ATR trail, over one year the profit grew from +29 to +124 USDT and the drawdown fell from −107 to −59 USDT.
  • The daily (10%) and monthly (35%) loss limits close the position and pause the bot until the next day or month.

Settings

SettingDefaultWhat it changes
Filter window40 candlesHow many recent closes are averaged
Time kernel12 candlesHow fast old candles are forgotten
Price kernel1.5 × ATRWhich candles count as far away and barely matter
Reversal threshold1.5 kernel widthsHigher means fewer and later flips
Spike rejectiononThe reference price is the median of three closes
Timeframe4hFilter candles, ATR and the pause after a stop
Risk per trade2% of capitalWhat a trade loses if the stop is hit; the position size comes from it
Stop3 ATRStop distance from the entry; with the same risk it changes the position size
Trailing50 ATR (off)How far the stop trails the best price since the entry
After a trailing exitwait for a flipWhether to re-enter the same side after a trailing exit
Pyramid1 unit (off)Adds the same size every 1 ATR in the trade's direction
Pause after a stop1 candleHow many candles the bot skips after a stop-out
Loss limits10% a day, 35% a monthA loss beyond a limit closes the position until the next day or month

Results on history

All runs below use the same engine as the backtest in the bot, on BTCUSDT (OKX perpetual). Capital is fixed at 1000 USDT and profit is withdrawn. They include a 0.05% fee and 0.03% slippage on every entry and exit, plus 0.01% funding per 8 hours on longs. The periods end on November 1, 2026. Each table changes one setting and keeps the others at their defaults. Drawdown is measured on the equity curve including the open position.

Timeframe

The lower the timeframe, the more often the filter flips: more trades, more profit and a deeper drawdown. BTC over these periods: +2% in 6 months, −29% in 1 year, +10% in 2 years, +26% in 5 years.

TFPeriodProfitTradesWinnersMax drawdown
4h6 months+255 USDT197−164 USDT
4h1 year+334 USDT4214−168 USDT
4h2 years+199 USDT7923−308 USDT
4h5 years+828 USDT20159−428 USDT
12h6 months+74 USDT63−60 USDT
12h1 year+120 USDT136−94 USDT
12h2 years+121 USDT2710−159 USDT
12h5 years+424 USDT7023−229 USDT
1d6 months+66 USDT42−49 USDT
1d1 year+55 USDT83−73 USDT
1d2 years+30 USDT145−157 USDT
1d5 years+87 USDT3713−233 USDT

4h earned the most, but over 5 years its drawdown reached 428 USDT, 43% of the capital. 12h is calmer: +424 USDT over 5 years with a 229 USDT drawdown. On 1d there are few trades and the result is weak. The 1h, 2h and 6h timeframes are not in these tables; you can check them with the backtest on the bot page.

Over the last year BTC fell 29%, and the bot made most of its profit on shorts. Fewer than half of the trades won, but on average the winners were 2.5–4 times larger than the losers: the bot holds the position while the trend runs and cuts mistakes quickly with the stop. With 2% risk and a 3 ATR stop the position is about 300 USDT out of 1000 on 12h and about 550–700 USDT on 4h.

Trailing and waiting for a flip

The trailing stop follows the best price since the entry and closes the position when the price pulls back by the set number of ATR. Without waiting, the bot re-enters the same side on the next candle. With a 3 ATR trail that makes 3–4 times more trades: 141 instead of 42 over a year on 4h. With the “wait for a flip” box checked, after such an exit the bot waits until the filter flips and then enters the new side.

TFTrailingAfter the exitProfit, 1 yearTrades, 1 yearProfit, 5 yearsDrawdown, 5 years
4hoff—+334 USDT42+828 USDT−428 USDT
4h3 ATRre-enter+166 USDT141+571 USDT−359 USDT
4h3 ATRwait for a flip+124 USDT34+258 USDT−197 USDT
4h4 ATRwait for a flip+73 USDT37+585 USDT−214 USDT
4h6 ATRre-enter+299 USDT60+817 USDT−385 USDT
4h6 ATRwait for a flip+306 USDT42+935 USDT−322 USDT
12hoff—+120 USDT13+424 USDT−229 USDT
12h3 ATRre-enter+29 USDT45+255 USDT−206 USDT
12h3 ATRwait for a flip+124 USDT11+418 USDT−91 USDT
12h4 ATRwait for a flip+146 USDT11+190 USDT−161 USDT
12h6 ATRwait for a flip+127 USDT13+468 USDT−204 USDT

On 4h a close trail (3–4 ATR) cuts the big trends the bot earns on, and profit drops. A 6 ATR trail with waiting for a flip made +935 USDT over 5 years against +828 without trailing, and the drawdown fell from 428 to 322 USDT. On 12h a 3 ATR trail with waiting earned almost as much as no trailing over 5 years (+418 against +424 USDT), and the drawdown fell from 229 to 91 USDT.

Risk and stop

The risk per trade sets the position size. Profit and drawdown grow with it almost proportionally, and the share of winning trades does not change. Figures for 4h:

Risk per tradeProfit, 1 yearDrawdown, 1 yearProfit, 5 yearsDrawdown, 5 years
1%+167 USDT−84 USDT+414 USDT−214 USDT
2%+334 USDT−168 USDT+828 USDT−428 USDT
3%+501 USDT−252 USDT+1242 USDT−642 USDT
4%+668 USDT−336 USDT+1656 USDT−857 USDT

With 4% risk the drawdown over 5 years reached 857 USDT, almost the whole capital. It is better to start with 1–2%.

With the same risk, the stop changes the position size: the closer the stop, the larger the position. So a close stop gives more profit, but also a deeper drawdown, and it is hit more often. Figures for 4h:

StopProfit, 1 yearDrawdown, 1 yearProfit, 5 yearsDrawdown, 5 years
2 ATR+539 USDT−258 USDT+1366 USDT−606 USDT
2.5 ATR+417 USDT−181 USDT+1100 USDT−508 USDT
3 ATR+334 USDT−168 USDT+828 USDT−428 USDT
4 ATR+255 USDT−126 USDT+846 USDT−366 USDT
5 ATR+194 USDT−109 USDT+682 USDT−277 USDT

Filter settings and the pause after a stop

A reversal threshold of 1.5 gave the best result on 4h. A lower threshold flips the position on noise more often, a higher one flips later. On 12h a threshold of 2 was slightly better over a year, and 1.5 over 5 years. The table shows profit:

Reversal threshold4h, 1 year4h, 5 years12h, 1 year12h, 5 years
1+35 USDT+443 USDT+4 USDT+133 USDT
1.5+334 USDT+828 USDT+120 USDT+424 USDT
2+279 USDT+324 USDT+131 USDT+273 USDT
2.5+76 USDT+129 USDT+117 USDT+262 USDT
3+81 USDT+570 USDT+95 USDT+306 USDT

A filter window above 40 candles changes nothing: with a 12-candle time kernel, old candles already weigh almost nothing. A window of 20 on 4h made +879 USDT over 5 years against +828.

Filter window4h, 1 year4h, 5 years12h, 1 year12h, 5 years
20 candles+343 USDT+879 USDT+129 USDT+461 USDT
30 candles+344 USDT+769 USDT+119 USDT+417 USDT
40 candles+334 USDT+828 USDT+120 USDT+424 USDT
60 candles+334 USDT+828 USDT+120 USDT+424 USDT
80 candles+334 USDT+828 USDT+120 USDT+424 USDT

The pause after a stop gives no clear gain. Without a pause, 4h made +1041 USDT over 5 years against +828 with a similar drawdown (453 against 428 USDT), but over the last year and on 12h a 1-candle pause was better.

Pause after a stop4h, 1 year4h, 5 years12h, 1 year12h, 5 years
0 candles+311 USDT+1041 USDT+115 USDT+363 USDT
1 candle+334 USDT+828 USDT+120 USDT+424 USDT
3 candles+282 USDT+827 USDT+99 USDT+423 USDT

Pyramid

The pyramid adds the same size again every time the price moves 1 ATR in the trade's direction, and pulls the stop up. Profit grows, but the drawdown grows faster. Figures for 4h:

UnitsProfit, 1 yearDrawdown, 1 yearProfit, 5 yearsDrawdown, 5 years
1 (off)+334 USDT−168 USDT+828 USDT−428 USDT
2+564 USDT−329 USDT+1587 USDT−886 USDT
3+840 USDT−489 USDT+2460 USDT−1510 USDT

With three units the drawdown over 5 years exceeded the capital: 1510 USDT with 1000 of capital, so the account would have been lost. Turn the pyramid on only together with a lower risk.

How to set up the bot

  1. Start with the defaults: 4h, 2% risk, a 3 ATR stop, trailing off.
  2. If the 4h drawdown feels too deep, take 12h: less profit, but about half the drawdown.
  3. Scale profit and drawdown with the risk: at 1% both are half as large, at 3% one and a half times larger.
  4. On 4h keep the trail at 6 ATR or more; on 12h a 3 ATR trail works well. With trailing on, keep “wait for a flip” checked.
  5. Leave the reversal threshold at 1.5 and the window at 40: they gave the best or close to the best result.
  6. Turn the pyramid on only with a reduced risk.
  7. Before starting, backtest your settings on the bot creation page and try the bot on a demo account.

Strengths and weaknesses

  • Strength: at reversals the filter lags less than a moving average of the same length and ignores single spikes.
  • Strength: the signal does not repaint, and the entry and exit rule is simple and testable.
  • Weakness: in a sideways market the bot flips often and pays fees and stops.
  • Weakness: the bot is always in a position, so a sharp gap against it hits the capital before the stop can trigger.
Create a Bilateral bot