Similar to local minimum strategy, the idea is to wait for price to go somewhat to the extreme and then wait for pullback/retracement.

The degree to which you want the price to go to extreme (before entering the trade) is completely up to you.

In this code example using the Badger algo-bot starter code, I set the percentile to 20% (i.e. when price was at the lowest 20% in the last 20 candles).

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

Of course you can adjust the percentile, as well as the time window you’re considering.

Then after backtesting hundreds (or thousands) of trades you can see if you potentially have a winning strategy.

The code

// PRICE PERCENTILE STRATEGY

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

bool is20percentile(double[] prices, double currentPrice) {
	double lowerCount = 0;
	foreach(price; prices) {
		if (price <= currentPrice) lowerCount += 1.0;
	}
	if (lowerCount / prices.length <= 0.2) return true;
	return false;
}

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

	Candle[] btcCandles = this.candles[c.BTCUSDT];
	double currentPrice = btcCandles[$-1].close;
	double[] last20prices = btcCandles[$-21..$-1].map!(c => c.low).array;

	if (
		!hasOpenPosition() &&
		is20percentile(last20prices, currentPrice)
	) {
		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

All in all, counting on a reversal doesn’t seem like a profitable strategy.

We blew our account after making 2321 trades:

----------------------------
| PERFORMANCE    |
----------------------------
- Initial capital: 100000.00
- Final capital::: 999.18
- Total trades:::: 2321

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

- Drawdown: -99.00 %
- Pushup::: 1.81 %
----------------------------

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