As a non-resident alien, I was told that I do not have to pay capital gains tax in the USA. Why then is there line 14 for reporting capital gains in 1040NR?
Publication 519 page 18 has
If you are engaged in a U.S. trade or business, all income, gain, or loss for the tax year that you get from sources within the United States (other than certain investment income) is treated as effectively connected income.
If you are engaged in a U.S. trade or business only because you perform personal services in the United States during the tax year, income and gains from assets, and gains and losses from the sale or exchange of capital assets are generally not effectively connected with your trade or business. However, if there is a direct economic relationship between your holding of the asset and your trade or business of performing personal services, the income, gain, or loss is effectively connected.
. If your only U.S. business activity is trading in stocks, securities, or commodities (including hedging transactions) through a U.S. resident broker or other agent, you are not engaged in a trade or business in the United States.
So only if the capital gains have a direct economic relationship with the trade or business performed are they considered to be effectively connected. Moreover, if the only business activity is trading in stocks or securities then the capital gains are not effectively connected.
Further, page 21 has
Reporting. Report your gains and losses from the sales or exchanges of capital assets that are not effectively connected with a trade or business in the United States on page 4 of Form 1040-NR. Report gains and losses from sales or exchanges of capital assets (including real property) that are effectively connected with a trade or business in the United States on a separate Schedule D (Form 1040 or 1040-SR), Form 4797, or both. Attach them to Form 1040-NR.
So capital gains of a non-resident alien when they are effectively connected must be reported on line 14, and, when they are not, in Schedule NEC on page 4 on 1040NR.