// 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: the XTester engine runs this strategy over the project's candles and
//    executes orders, fees and balances itself; this file only decides. A JDK is needed:
//    Studio offers the Java language pack when none is installed.
// 3. Add your decisions in onBar. context.api() places orders (buy, sell, placeOrder,
//    closePosition); context.indicators() reads the engine's indicators (rsi, sma, ema,
//    macd, call("ADX", "period", 14)); context.logger() writes to the strategy log.
// Ask the AI assistant to explain this file.
import xtester.Bar;
import xtester.Context;
import xtester.Strategy;
import xtester.SymbolKey;
import java.math.BigDecimal;

class UserStrategy extends Strategy {
    private int phase = 0;
    @Override
    public void onInit(Context context) {
        context.logger().info("UserStrategy initialized: " + context.parameters().symbolKey());
    }

    @Override
    public void onTick(BigDecimal price) {
    }

    @Override
    public void onBar(SymbolKey symbolKey, Bar bar) {
        if (phase == 0 && bar.close().compareTo(bar.open()) > 0) {
            var result = context.api().buy("0.01");
            phase = result.success() ? 1 : 2;
            if (!result.success()) context.logger().info(result.errorMessage());
        } else if (phase == 1 && bar.close().compareTo(bar.open()) < 0) {
            var result = context.api().closePosition();
            phase = 2;
            if (!result.success()) context.logger().info(result.errorMessage());
        }
    }

    @Override
    public void onFinish(Context context) {
    }
}
