// EDUCATIONAL TESTER EXAMPLE — one symbol, initially flat; not for live trading.
// First bullish candle: attempt a 0.01 market buy. First later bearish candle:
// attempt to close the whole position. At most one entry and one exit request.
// Acceptance is not a fill; errors stop the example. No retries, restart recovery,
// position reconciliation or risk controls. Choose valid size/funds in Studio.
// GETTING STARTED
// 1. Open Data and download candles for an exchange, market, and symbol.
// 2. Press F5 inside a project: the XTester engine compiles every .cpp of this folder with the
//    header-only xtester.hpp and runs it over the project's candles, executing orders, fees and
//    balances itself; this file only decides. A C++ compiler is needed (Xcode CLT, clang/g++).
// 3. Add your decisions in onBar. ctx.api places orders (buy, sell, placeOrder, closePosition);
//    ctx.indicators reads the engine's indicators (rsi, sma, ema, call("ADX", ...)); ctx.logger
//    writes to the strategy log. XTESTER_STRATEGY defines main: never write main yourself.
// Ask the AI assistant to explain this file.
#include "xtester.hpp"

class UserStrategy : public xtester::Strategy {
	int phase = 0;
	void onInit(xtester::Context& ctx) override {
		ctx.logger.info("UserStrategy initialized: " + ctx.parameters.symbolKey.str());
	}

	void onTick(xtester::Context& ctx, std::optional<double> price) override {
	}

	void onBar(xtester::Context& ctx, const xtester::SymbolKey& symbolKey, const xtester::Bar& bar) override {
		if (phase == 0 && bar.close > bar.open) {
			auto result = ctx.api.buy("0.01");
			phase = result.success ? 1 : 2;
			if (!result.success) ctx.logger.info(result.errorMessage);
		} else if (phase == 1 && bar.close < bar.open) {
			auto result = ctx.api.closePosition();
			phase = 2;
			if (!result.success) ctx.logger.info(result.errorMessage);
		}
	}

	void onFinish(xtester::Context& ctx) override {
	}
};
XTESTER_STRATEGY(UserStrategy)
