import nexustrade as nt


def create_eth_ema_regime():
    asset = nt.crypto_asset("ETH-USD")
    return nt.portfolio(
        "Public SEO crypto example - ETH 12/26 EMA regime",
        [
            nt.strategy(
                "Enter ETH 12/26 EMA regime",
                ((nt.EMA(asset, 12, "Day") > nt.EMA(asset, 26, "Day")) & (nt.PositionValue(asset) == nt.Value(0))),
                nt.buy(asset, 95, "percent of buying power")
            ),
            nt.strategy(
                "Exit ETH 12/26 EMA regime",
                ((nt.EMA(asset, 12, "Day") <= nt.EMA(asset, 26, "Day")) & (nt.PositionValue(asset) > nt.Value(0))),
                nt.sell(asset, 100, "percent of current positions")
            ),
        ],
        initial_value=10000,
        supports_crypto=True,
        supports_fractional_shares=True,
        alerts_enabled=False,
        dividend_policy='cash',
    )


def backtest_eth_ema_regime(client=None):
    client = client or nt.NexusTradeClient.from_environment()
    operation = client.create_backtest(
        nt.backtest(
            create_eth_ema_regime(),
            start_date="2024-01-01", end_date="2024-12-31",
            interval="Day", baseline_symbol="SPY",
            initial_value=10000, generate_events=True,
            dividend_policy="cash",
            fee_config={"Stock": {"type": "percent", "amount": 0.1},
                        "Cryptocurrency": {"type": "percent", "amount": 0.7},
                        "Option": {"type": "dollars", "amount": 0.65}},
        ),
        idempotency_key="explore-eth-ema-regime-2024-spy-v1",
    )
    return client.wait_for_backtest(operation["id"])


# Build locally: book = create_eth_ema_regime()
# Run and inspect: completed = backtest_eth_ema_regime(); print(completed["result"])
