Where to stop key derivation when looking at balance of a deterministic wallet If I want to know the total balance in all the addresses which can be generated in a deterministic wallet (like electrum or armory).

I have the master public key allowing me to derive an infinite number of public keys.

What is the number of addresses I should generate to check the balance ?

I suppose I can stop if the last X addresses don't have any transactions.

What is the value of X for electrum and armory (when recovering from a backup) ?
The gap limit is the maximum number of consecutive unused addresses in your deterministic sequence of addresses. Electrum uses a gap limit to stop looking for addresses, it is set to 5 by default, so the client get all addresses until 5 unused addresses are found.


https://electrum.org/faq.html#gap-limit

If you used a different gap limit when creating the addresses, then you need to keep record of it or remember it. Addresses that appeared beyond the gap limit won't be found unless you set the gap limit you used.

For example: Using MPK on a web server you used gap limit 40 to create 40 addresses for your web store. Then you had 39 open orders that are not paid and the address number 40 has a payment.

When you restore with gap limit 5 (default in the client) you'll have 5 unused empty addresses, you won't see the other 34 that are also unused but related to open orders neither you'll see or have balance for the address number 40 that has a payment on it.