Marty Chavez, a former candidate to lead Goldman Sachs and one of its few openly gay top executives, is leaving the Wall Street powerhouse.

Chavez, who rose to prominence as a computer programmer who played a key role advancing Goldman’s trading technology, plans to “retire from the firm” at the end of the year, when he will take on a largely honorary role as senior director, according to a Tuesday memo obtained by The Post.

Chavez is being replaced as co-head of Goldman’s trading division by Marc Nachmann, who is currently based in London as co-head of the investment banking group. German-born Nachman, 48, will lead Goldman’s trading unit alongside co-heads Ashkok Varadhan and Jim Esposito.

It’s one of the biggest personnel changes since CEO David Solomon started to remake the company in his image last October.

Goldman, which has struggled for years to make profits from bond trading, has been increasingly focused on making money from investment banking and branching into new areas, like consumer finance.

Nachmann has spent about 25 years at the bank, and was previously the head of the Global Financing Group and the head of the bank’s Latin America arm. Nachmann was named partner in 2004.

Chavez, 55, has spent about 19 years at the bank in two stints, and was one of the friendlier public faces for the bank. He’s a Latino among a largely white group of executives, one of the most powerful openly gay executives in a notoriously macho industry. In the 1990s, he kept a terrier named Galahad to help him stay sober.

Chavez rose through the ranks at Goldman thanks in part to his technology background, and had at one point overseen the company’s roughly 9,000 coders, according to a New York Times profile.

He also pushed the company to be more open with clients about its technology — a stark turnaround for a bank that twice sued a programmer over seven years for taking largely open-source code.

He rose to chief financial officer at Goldman in 2017, under the previous top executive, Lloyd Blankfein.

At the time, Chavez was seen as a potential — if long-shot — successor to Blankfein.

However, he lasted only about 18 months in the job before Solomon moved him to be one of three co-heads in the bank’s securities division — a change that was largely seen on Wall Street as a demotion.

“Marty has been an outstanding role model and mentor to many,” according to the memo, which was signed by Solomon and co-COOs John Waldron and Stephen Scherr.

“He has also been a strong advocate for the firm’s diversity and inclusion efforts, including as a member of the Firmwide Hispanic/Latino Network and the Lesbian, Gay, Bisexual and Transgender (LGBT) Network. In particular, Marty has been a dedicated and passionate leader in promoting LGBT inclusion both in and outside the workplace.”