# Current HardStop Implementation Review

## Current Basis

The current P0 set uses `HardStopFloatingLossPercent=8.0`, even though the source default remains `12.0`.

The existing loss-side HardStop condition was:

```text
ps.floatingLossPercent >= HardStopFloatingLossPercent
```

`ps.floatingLossPercent` is calculated in `GetPositionState()` as:

```text
(-floatingProfit / AccountInfoDouble(ACCOUNT_BALANCE)) * 100
```

So the current HardStop basis is current-balance percent.

## Values Used

- Balance value: `ACCOUNT_BALANCE` at each tick.
- Floating loss value: sum of floating profit/loss across EA-managed positions; only negative floating P/L contributes.
- Margin fail-safe: `marginLevel <= HardStopMarginLevel` remains active for all Basis modes.
- P0 effective percent: `8.0%`.
- 50,000 JPY equivalent threshold: `4,000 JPY`.
- 100,000 JPY equivalent threshold: `8,000 JPY`.

## Timing

HardStop is evaluated on every tick inside `OnTick()` after `GetPositionState()` and `GetSafeMarginLevel()`.

## Close Path

When HardStop is true and `UseEmergencyCloseAtHardStop=true`, the EA calls:

```text
CloseAllEaPositions("hard stop emergency close")
```

If the close succeeds, it then calls `ApplyHardStopAfterMode()`.

## Log Path

The close reason containing `hard stop` is mapped to:

```text
HARDSTOP_CLOSE
```

through `CloseEventTypeFromReason()`.

## After-Mode

`ApplyHardStopAfterMode()` sets `lastHardStopTime` and starts cooldown/session/day stop depending on `HardStopAfterMode`. The P0 set uses cooldown mode.

## Market Closed Handling

If `CloseAllEaPositions()` receives market-closed retcodes, retry behavior is only activated when the corresponding retry input is enabled. In this compare, `UseGlobalMarketClosedRetry=false` and `UseMarketClosePreControl=false`, so the test does not reintroduce retry or pre-close behavior.

## Implemented Change

Only the HardStop loss threshold basis was made selectable:

- `HARDSTOP_BALANCE_PERCENT`
- `HARDSTOP_FIXED_YEN`
- `HARDSTOP_INITIAL_BALANCE_R`

The default remains `HARDSTOP_BALANCE_PERCENT`, preserving existing behavior.
