// 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 builds this folder (package main) with the
//    xtester SDK and runs it over the project's candles, executing orders, fees and balances
//    itself; this file only decides. A Go toolchain is needed: Studio offers the Go language pack.
// 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. Do not write func main: the engine generates it.
// Ask the AI assistant to explain this file.
package main

import (
	"fmt"

	"xtester"
)

type UserStrategy struct {
	xtester.Base
	phase int
}

func init() { xtester.Register(func() xtester.Strategy { return &UserStrategy{} }) }

func (s *UserStrategy) OnInit(ctx *xtester.Context) {
	ctx.Logger.Info(fmt.Sprintf("UserStrategy initialized: %s", ctx.Parameters.SymbolKey))
}

func (s *UserStrategy) OnTick(ctx *xtester.Context, price *float64) {
}

func (s *UserStrategy) OnBar(ctx *xtester.Context, symbolKey xtester.SymbolKey, bar xtester.Bar) {
	if s.phase == 0 && bar.Close > bar.Open {
		result := ctx.Api.Buy("0.01")
		if result.Success { s.phase = 1 } else { s.phase = 2; ctx.Logger.Info(result.ErrorMessage) }
	} else if s.phase == 1 && bar.Close < bar.Open {
		result := ctx.Api.ClosePosition(nil, "")
		s.phase = 2
		if !result.Success { ctx.Logger.Info(result.ErrorMessage) }
	}
}

func (s *UserStrategy) OnFinish(ctx *xtester.Context) {
}
