import nexustrade as nt


def create_btc_weekly_accumulation():
    asset = nt.crypto_asset("BTC-USD")
    return nt.portfolio(
        "Public SEO crypto example - BTC weekly accumulation",
        [
            nt.strategy(
                "Buy Bitcoin weekly",
                (nt.DaysSinceOrder(asset, "Buy", "Filled") >= nt.Value(7)),
                nt.buy(asset, 150, "dollars")
            ),
        ],
        initial_value=10000,
        supports_crypto=True,
        supports_fractional_shares=True,
        alerts_enabled=False,
        dividend_policy='cash',
    )


def backtest_btc_weekly_accumulation(client=None):
    client = client or nt.NexusTradeClient.from_environment()
    operation = client.create_backtest(
        nt.backtest(
            create_btc_weekly_accumulation(),
            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-btc-weekly-accumulation-2024-spy-v1",
    )
    return client.wait_for_backtest(operation["id"])


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