The premise of this strategy is that whatever is happening will continue happening (especially when it comes to a strong trend).

In this example, I’m using 5 consecutive green candles as an indicator of a strong trend. Because something is obviously causing such a clear move in one direction.

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

In the code below, using the Badger algo-trading framework, you can see how I’m checking whether 5 back-to-back candles are green.

And if they are, the trade is executed.

// TREND FOLLOWING STRATEGY

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

int trades = 0;

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

	if (trades >= 1 && !hasOpenPosition()) {
		return false; // stop the bot
	}

	Candle[] btcCandles = this.candles[c.BTCUSDT];
	double currentPrice = btcCandles[$-1].close;

	bool is5green = 
		btcCandles[$-2].color == Color.GREEN &&
		btcCandles[$-3].color == Color.GREEN &&
		btcCandles[$-4].color == Color.GREEN &&
		btcCandles[$-5].color == Color.GREEN &&
		btcCandles[$-6].color == Color.GREEN;

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

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

It is truly remarkable what can be done with just a couple of lines of code. And within seconds, any strategy can be tested on hundreds or thousands of trades.