Investors & landlords

If your status will be non-resident in for your 2021 taxes, you'll need to use Form 1040-NR to report your income form US sources. You'll receive form(s) 1099 from your broker with amounts to report. You loss theoretically could offset the whole amount of your gain; you'll need to pay attention to Short term (if you hold your stocks less that a year) and Long term (holding period more that 1 year), they are reporting separately. Short term loss can offset short term gain, and long term loss can offset long term gain. Unused loss can be carried forward to future years.