The simplest code example for Badger algo-bot starter code is “buy and hold” strategy where your main focus is picking a good entry point.

This code example picks the entry point at random (at the beginning of the backtestPeriod).

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

But you can decide based on variety of factors like: local minimum/maximum, price percentile, moving-average crossover, etc.

The Code

The algorithm is simple. Just buy randomly, hold for 20 candles, and then sell.

// BUY AND HOLD STRATEGY

// you can also add custom variables here
// their state is preserved between the loops
int minutesPassed = 0;

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 //

	Order marketBuy = Order(
		c.BTCUSDT,
		OrderType.MARKET,
		Direction.BUY,
		1_000_000, // target price
		0 // stop price
	);
	Order marketSell = Order(Action.CLOSE);

	if (!hasOpenPosition()) {
		this.order(marketBuy);
	}

	if (minutesPassed >= 20 && hasOpenPosition()) {
		this.order(marketSell);
		minutesPassed = 0;
		//return false; // stop the bot
	}

	minutesPassed++;

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

Test results

Even in a situation where BTC went from roughly 44k to 64k from the start of 2024 to the end of April 2024 –> the strategy of buying and holding for shorter periods of time blew up the account.

After 2302 trades we lost 99% of our (hypothetical) account.

----------------------------
| PERFORMANCE    |
----------------------------
- Initial capital: 100000.00
- Final capital::: 998.12
- Total trades:::: 2302

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

- Drawdown: -99.00 %
- Pushup::: 3.80 %
----------------------------

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