OPEC (Organization of Petroleum Exporting Countries) logo is seen on a mobile phone with the President of United States of America, Donald Trump on the background. Omar Marques | LightRocket | Getty Images

On the morning of April 20, the oil market woke up to a surprise. President Donald Trump was on Twitter warning OPEC that he would not tolerate the cartel's oil price manipulation. At the time, investors might have written off the early morning tweet as another outburst from a mercurial president. But seven months later, it looks more like the opening salvo in a campaign to deflect blame when oil prices rise and take credit when they fall. Longtime Trump followers would have also recognized back in April that Trump was not picking a new fight, but reviving an old grudge with OPEC. The cartel was a favorite target for Trump when he was testing the political waters and honing his America First message during the 2012 election cycle.

It's a really low-cost strategy for him because if he gets oil prices down, he gets credit for it. If prices rise, he can blame OPEC. ... There's no real risk on this. It's quite clever in a way. Derek Brower RS Energy Group director

To be sure, OPEC has played a pivotal role in the oil price recovery since crude fell to 12½-year lows in 2016. Last year, the 15-nation cartel partnered with Russia and other exporters to keep 1.8 million barrels per day off the market. But the other major policy driving oil prices this year is Trump's decision to restore sanctions on Iran, OPEC's third biggest producer. A review of Trump's OPEC tweets this year shows the president often sought to blame the group just when his Iran policy was pushing up prices. "It's a really low-cost strategy for him because if he gets oil prices down, he gets credit for it. If prices rise, he can blame OPEC," said Derek Brower, a director specializing in political risk at RS Energy Group. "There's no real risk on this. It's quite clever in a way." Trump may deploy that strategy sooner than expected. After a meeting fraught with confusion, intrigue and infighting, OPEC and its allies agreed to a fresh round of production cuts this week, hoping to stem a collapse in oil prices that dragged Brent crude below $50 recently. CNBC annotated Trump's tweets to show how the president has used Twitter to shape the conversation around this year's wild oil price swings.

Trump's pre-White House tweets: A brief history

April 20: Trump's opening shot

June 13: Trump dodges blame for rising oil prices

Trump laid off OPEC for nearly two months, until the week before the cartel's midyear meeting: @realDonaldTrump: Oil prices are too high, OPEC is at it again. Not good! U.S. crude had recently spiked above $70 for the first time since 2014 after Trump restored sanctions on Iran. The consensus in the oil market was nearly universal: The blame for higher oil prices rested largely with Trump. Yet the president continued to insist OPEC alone was at fault. Concerned that its Iran policy would send oil prices higher, the Trump administration had been actively lobbying Saudi Arabia to hike output. By the time Trump sent his tweet, the output increase looked like a done deal, and U.S. crude had fallen from a recent high near $73 to about $67. Yet the average U.S. gasoline price was still near $3 a gallon. Higher pump prices threatened to erase the boost that middle- and working-class Americans got from tax cuts, the Republicans' only major legislative accomplishment in 2017. That was a political liability ahead of midterm elections in November. Notably, Trump accused Obama of conspiring with the Saudis to lower oil prices ahead of the 2012 election. @realDonaldTrump: I believe @BarackObama made a deal with the Saudis to increase oil production until after the election. Then (cont) http://tl.gd/i57g67

June 22: Trump's last ditch pitch

The day of OPEC's midyear meeting, Trump sent one final request: Hope OPEC will increase output substantially. Need to keep prices down! Trump got what he asked for. Sort of. OPEC agreed to hike output by about 1 million bpd, but the plan was short on details and left the market with more questions than answers. Instead of backing down, oil prices charged higher.

June 30: Trump takes his gripe to the king

July 4: Fireworks on Independence Day

Sept 20: Trump's time is running out

Nov. 12: Oil prices collapse, but not enough for Trump

Nov. 21: Trump leverages the Khashoggi crisis