This is one of the frequently asked questions.
Basic rule: Non permanent tax resident is not taxable for foreign source income unless the gain is repatriated into Japan
The basic rule is that foreign sourced income of non-permanent tax residents (who have been in Japan more than 5 years in the past 10 years) is taxable only if money is brought into Japan.
One of the common misunderstandings about it is unless the money is sent from the brokerage account, you could claim that it came from your savings and it is a separate money from the capital gain and therefore, it should not be taxable. But that is not true.
The income tax enforcement law, article #17 section 4, paragraph 1 says any money transferred to Japan will be regarded as the money that was brought into Japan from the gain.
Income Tax Enforcement Ordinance Article 17 sec. 4 para. 1
Exceptions in capital gain from foreign stocks
There are important exceptions here. Capital gains from certain categories of investments are not taxable. Here are the four categories:
1) Stocks that are taxable in the country where its issuing entity exists because ownership is more than certain percentage defined by the tax law of the country
2) Stocks of a company that owns more than 50% of its asset in properties
3) Stocks that represents a membership of golf courses
4) Stocks that were purchased with in 10 years from its sales and purchaser at the time of purchase was not a non-permant resident (non-resident) 特定有価証券の譲渡
The items from #1 to #3 are taxable in the country where the company is located. It is usually consistent with tax treaties.
The item #4 is confusing but the point is simple. If you are a non-tax-permanent at the time of sales and you were not a resident at the time of the purchase of the shares and it was within 10 years range going back from the time of the sales, it is not taxable unless the gain is brought in.
Please see the below chart. That is easier to understand.
Public announcement of the revisions of tax laws in 2017
https://www.nta.go.jp/law/tsutatsu/kihon/shotoku/02/01.htm