It pays to be on the president's bad side. At least it has for public companies over the past two years, a pair of indexes created by Ritholtz Wealth Management CIO Barry Ritholtz and featured in Bloomberg show.

Driving the news: President Trump has broken with past presidential precedent and heaped both praise and disparagement on a collection of U.S. companies since taking office. Ritholtz tracked the stock performance of companies relative to their relationship with the president.

In 2017, the "Oligarch Index" of Trump's favored companies rose 20%, but trailed the S&P 500's gain of 21%. During the same year, the "Drain the Swamp Index" of Trump's hated Twitter targets rose 43%.

Similar results were seen in 2018. For the one-year period ended Jan. 10, the Oligarch Index fell 23%. The Drain the Swamp Index rose 6.3%.

"That is a 29 percentage point advantage for Trump’s most-hated companies over his most favored ones," Ritholtz noted in his piece for Bloomberg Opinion. "This is even bigger spread than the first year we tracked the indexes, when the spread was 23 percentage points."

The relative performance of the indexes likely has more to do with who Trump likes than whether or not he goes after them on Twitter, Ritholtz said.

Jeff Bezos, owner of the Washington Post, and founder and CEO of Amazon.com had a good year. Amazon gained 32%.

Twitter, a favorite target of conservatives last year, whose founder, Jack Dorsey, refused to create a lying Hillary emoji, did even better, gaining 36%.

Even the "failing" New York Times added more than 23% during the period.

The Oligarch index, meanwhile, had some disasters.

Goldman Sachs, dealing with an ethical cloud, fell 31%.

Ford Motor dropped 34%.

Facebook, a conduit for fake news that may have helped Trump's electoral prospects, lost almost a quarter of its value.

Exxon Mobil, whose former CEO served for a time as secretary of state in the Trump administration, fell 17%.

"All told, it was a rough year for the Oligarchs," Ritholtz said, "with little other than huge tax cuts to comfort them."

Go deeper: Trump's Big Tech contradictions