# Follower EA — Implementation Plan

## 1. Scope

Build `FollowerCopier.mq5`.

After compilation:

```text
FollowerCopier.ex5
```

It runs inside MT5 Desktop on a Windows MT5 Host connected to a Follower account.

## 2. Core Rule

Follower EA is the component that executes copied broker trades.

The Go Server never places broker orders directly.

## 3. Runtime

```text
Windows MT5 Host
└── MT5 Desktop
    └── FollowerCopier.ex5
```

## 4. Connection

Follower establishes a secure outbound connection to Go.

Input/configuration:

```text
Follower ID
API credential
Copier Server endpoint
```

## 5. Event Handling

Initial events:

```text
POSITION_OPEN
POSITION_MODIFY
POSITION_PARTIAL_CLOSE
POSITION_CLOSE
```

Flow:

```text
receive
→ validate
→ check idempotency
→ calculate volume
→ map symbol
→ risk check
→ execute
→ report result
```

## 6. Lot Modes

Support:

### Exact
```text
Follower = Master volume
```

### Multiplier
```text
Follower = Master volume × multiplier
```

### Balance proportional
```text
Master lot × follower balance / master balance
```

### Equity proportional
```text
Master lot × follower equity / master equity
```

Normalize using:

```text
min volume
max volume
volume step
```

## 7. Symbol Mapping

Support different broker symbols.

Example:

```text
EURUSD → EURUSDm
```

Mapping may differ per Master/Follower relationship.

## 8. Position Mapping

Follower must maintain:

```text
master_id
master_position_id
follower_id
follower_position_id
```

Use mapping when modifying/closing positions.

## 9. Idempotency

Before execution:

```text
has event already been processed?
```

If yes:

```text
do not execute again
ACK safely
```

A network retry must never create a second trade.

## 10. Risk Controls

Initial:

```text
copy_enabled
max_lot
max_positions
allowed_symbols
allowed_direction
max_exposure
```

A trade that violates configured safety limits should be rejected and reported.

## 11. Execution

Use MQL5 trading APIs inside the Follower EA.

Capture:

```text
broker result
order ticket
deal ticket
position ticket
executed volume
executed price
error code
```

Never assume order/deal/position IDs are interchangeable.

## 12. ACK

After processing:

```text
SUCCESS
REJECTED
FAILED
```

send an execution acknowledgement/result to Go.

## 13. Reconnect and Replay

Store enough state to support:

```text
last processed sequence
processed event IDs
```

On reconnect:

```text
connect
→ authenticate
→ report last processed sequence
→ receive replay
→ continue live events
```

## 14. Synchronization

For a newly connected Follower:

```text
SYNC_REQUEST
↓
receive Master current positions
↓
create configured positions
↓
establish mappings
↓
SYNC_COMPLETE
```

## 15. Broker Constraints

Before execution validate:

- symbol exists
- market is open
- volume valid
- margin sufficient
- SL/TP valid
- trading allowed

## 16. POC

First implementation:

```text
Go
 ↓
FollowerCopier.ex5
 ↓
MT5
 ↓
open one test position
```

Then test:

```text
modify
partial close
close
```

## 17. Definition of Done

Follower EA is accepted when:

- events execute correctly
- duplicates do not duplicate trades
- volume is normalized
- symbols are mapped
- ACKs are returned
- reconnect/replay works
- position mappings remain correct
