This strategy is based on the assumption that the price tends to reverse/retrace from extremes and this algorithm captures such behavior.

When the price reaches local minimum (20 candles), the trade is executed, expecting the price correction in opposite direction.

entry position (white dot) and exit position (yellow square)

The code

Below is the code that uses Badger algo-bot starter code for backtesting and visualization:

bool hasOpenPosition() {
	return 
		this.positions.length != 0 &&
		this.positions[$-1].action == Action.OPEN;
}

override bool trade() {
	if(!super.trade()) return false;
	// ALL CODE HERE - BEGIN //

	long window = 20; // 20-candles minimum
	Candle[] btcCandles = this.candles[c.BTCUSDT];
	double currentPrice = btcCandles[$-1].close;
	double lastCandleMinimum = btcCandles[$-2].low;
	Candle[] candlesWindow = btcCandles[$-window-1..$-1];
	double lowestPrice = candlesWindow.map!(c => c.low).minElement;

	if (
		!hasOpenPosition() &&
		lastCandleMinimum <= lowestPrice
	) {
		Order marketBuy = Order(
			c.BTCUSDT,
			OrderType.MARKET,
			Direction.BUY,
			currentPrice * 1.003, // target price
			currentPrice * 0.995, // stop price
		);
		this.order(marketBuy);
	}

	// ALL CODE HERE - END //
	return true;
}

Test results

After nearly 2400 trades, we blew up our account.

----------------------------
| PERFORMANCE    |
----------------------------
- Initial capital: 100000.00
- Final capital::: 996.75
- Total trades:::: 2399

- ROI:::::: -99.00 %
- Min. ROI: -99.00 %
- Max. ROI: 0.10 %

- Drawdown: -99.00 %
- Pushup::: 2.62 %
----------------------------

Want to develop your own AI instead of hardcoding the trading rules?