Until recently, Sen. John Melcher has had all the earmarks of a loser, a bewildered politician caught up in a series of attacks from left and right, dissension within his own party and a confused campaign style that has translated into one of the most inept bids for reelection in Montana history.

In the final days, however, Melcher has taken the offensive, struggling to shift the focus from a beleaguered defense of his record onto Larry Williams, the GOP challenger who carries heavy political baggage.

His attacks, according to GOP tracking polls, have been successful, erasing Williams' 4-point lead and apparently giving Melcher a substantial lead. The politicians believe the race remains very close. From the beginning, it has been a bizarre variation of national political strategies and national ideological trends.

Melcher's support of the Reagan administration's 1981 budget and tax proposals have not helped him significantly even though Reagan carried Montana by 57 percent to Jimmy Carter's 32 percent.

Instead, those votes resulted in significant defections from the Democratic Party's liberal wing, many of whom remain lukewarm at best.

Nor did this and Melcher's opposition to abortion prevent the National Conservative Political Action Committee from targeting him for defeat, and running a $220,000 negative campaign denouncing Melcher as "too liberal" for Montana.

Finally, his votes for Reagan's program have made it difficult for him to capitalize on the recession by attacking Reaganomics.

While Melcher is under attack from the right, Williams has staked out positions on Melcher's left, each of which appeals to key Democratic constituent groups that have voiced anger at various aspects of his voting record.

Williams supports the nuclear freeze and has called for partial unilateral nuclear disarmament, a rare stance anywhere.

In addition, Williams is pro-choice on abortion and has been a far stronger supporter of environmental issues than Melcher. Williams also has refused to take out-of-state donations and has run ads sharply critical of Melcher for accepting more than $300,000 in union and corporate PAC contributions.

At the same time, Williams is trying to hold GOP voters with his firm support of Reagan's economic program -- "it's on the right track" -- and with help from Reagan, who appeared in Great Falls on Williams' behalf today. Combined with Melcher's muddled campaigning, Williams' aggressiveness diverted attention from his controversial past.

During the 1970s Williams was twice in trouble with the Securities and Exchange Commission for highly questionable stock and commodity investor advisory services. Under pressure, he temporarily gave up his SEC registration in 1972, and later ran into trouble for promoting a commodities investment scheme that he claimed was the equivalent of "the holy grail."

He also wrote a book, "How to Prosper in the Coming Good Years," that appears to suggest tactics to bond salesmen for persuading elderly homeowners to trade their homes for bonds that return them only about 60 cents on the dollar.

Although the book describes the tactic as a "morally questionable technique," Williams contended in an interview that the book is providing neutral "investor advice" rather than recommendations. Concerning his troubles with the SEC, Williams said, "I've never been convicted of anything. I've never been found guilty of anything."