A buying panic by institutions and foreign investors yesterday sent the stock market soaring to its best gain of 1978, up 19.92 points as measured by the Dow Jones industrial average.

Trading volume on the New York Stock Exchange set an all-time record of 52.3 million shares, eclipsing the previous record of 44.5 million shares set on Feb. 20, 1976.

The market closed at 795.13, its best level for the day and the highest close since last Jan. 5, when it stood at 804.92. Advances on the Big Board led declines by about 4 to 1.

Analysts attributed the market's surge, on top of an 8.92 point gain on Thursday, to an improvement in investor psychology and a lemming-like reaction by institutional investors like mutual funds, bank trust departments and pension funds to commit their large cash reserves which have accumulated in recent months.

"There's a stampede to get in, and there's a very narrow door," commented David A. Baker, in charge of portfolio strategy for Drexel Burnham Lambert.

"There's no good reason for the market to trade 50 million shares or move 19 points at a time," Baker added. "The world is the same today as it was yesterday. But if a few major investors decide to commit reserves, others can't decide not to play the same game - so in that respect it's self-fulfilling."

Analysts, however, did point to some positive factors in the news background that helped send the market roaring forward.

These included a relatively small $400 million increase in the nation's money supply announced after the market close on Thursday, which was considerably smaller than had been expected, and which eased fears that the Federal Reserve Board might be forced into a new round of tightening.

And just before the market opened, the Federal Reserve Board reported a 1.4 percent gain in industrial production for March, the largest gain in a year, and an indication that the economy is rebounding strongly from the weather and coal-strike-plagued winter doldrums.

Meanwhile, the initial tepid reaction to President Carter's anti-inflation program announced earlier this week was replaced with a feeling of encouragement that at least the administration was finally willing to face up to its chief economic problem.

And the dollar had a good day in the foreign exchange markets.

In addition, there was a feeling of encouragement that Congress seemed willing to join the administration in trying to combat inflation. The resounding defeat of the farm bill in the House, and growing talks that a smaller, rather than a larger, tax cut may be needed - contrary to the usual election-year psychology in COngress - were viewed positively by investors who are now hoping that the $60 billion deficit for the coming fiscal year can be reduced.

"The worries are still there," commented E.F. Hutton and Co. Vice President Newton F. Zinder, "but there's a feeling that the problems are being recognized, and with that there always comes the hope that something positive will occur."

"Today is the culmination of a lot of factors," commented Larry Wachtel, a vice president with bache Halsey Stuart Shields. "For the last three months the market has been barraged by a lot of bad news: the ideast, inflation, weakness in the dollar, and that President Carter was wishy-washy.Each time the market hit a disappointment, instead of cracking, it snapped back, it was resilient."

Yesterday, Wachtel added, "the cycle of cash and the cycle of psychology ran together" as institutions, which had as of the beginning of April built up cash positions larger even than what they maintained at the bottom of the 1974 bear market, plunged in aggressively.

"The institutions are a homogeneous field," noted Wachtel. "No one likes to be alone. If the fashion is to be in cash, everyone is in cash. If the fashion is to be in stocks, everyone is in stocks."

On top of this, foreigners, who have stayed out of the U.S. equity markets recently because of the decline of the dollar relative to their currencies, also jumped in decisively.

Most analysts predicted that the momentum of the last few days should carry into at least the early part of next week, but that some reaction was inevitable. However, they predicted that volume should remain heavy for some time.

Perhaps for that reason, brokerage stocks were among the best performers in yesterday's market. Merrill Lynch Pierce Fenner & Smith was up 15/8 to 17. Bache Halsey Stuart Shields rose 1 1/4 to 7, and Paine Webber climbed 7/8 to 7 3/8.

The most active stock on the exchange was Scott Paper, which closed up 5/8 at 13/ 7/8 on volume of 1.4 million shares. A block of 1 million shares of Scott traded at 13.

Other stocks in the most active list included some of the former institutional favorities which in the last 18 months have been drubbed while so-called secondary issues have been carrying on a pull market of their own. Many of these stocks are included in the Dow Jones.

Sears Roebuck rose 1/2 to 24 1/4. Eastman Kodak climbed 3/4 to 45 7/8.And General Motors rose 1 1/4 to 64 1/2.

All in all, 1,234 issues advanced on the New York Stock Exchange, 354 declined and 333 remained unchanged.

The broader NYSE composite index gained 1.02 to close at 51.93.

On the American Stock Exchange, 486 stocks advanced while 215 declined, and the AMEX index closed the day at 134.69, up 1.01.