Investors looking to get a piece of the latest market surge poured cash into stock funds at the highest pace ever during the past four weeks.

Mutual funds and ETFs that focus on stocks garnered $58 billion in fresh money during the period that ended Wednesday, according to Bank of America Merrill Lynch. The rush comes during a period when the rose about 4 percent and was off to one of its fastest-ever starts for a calendar year.

The rush of cash is inspired by a "fear of missing out," Michael Hartnett, BofAML's chief investment strategist, said in a report on weekly fund flows. He added that the "frothy price action likely continues in the short term," though the firm's indicator of market sentiment is getting closer to a sell level.

New money didn't just go into exchange-traded funds that passively track market indexes like the S&P 500 and Dow industrials. Active funds, which have seen massive outflows in the past several years, also have reached a four-year peak inflows during the past four weeks.

All told, equity ETFs have pulled in $38.2 billion in 2018 while their mutual fund counterparts have gathered a net $5.6 billion, according to BofAML. Most mutual funds are run by managers who actively pick stocks and carry considerably higher fees than ETFs.

The one-week total inflow of $23.9 billion was the seventh-best on record.

U.S. funds showed the strongest money gains with $6.4 billion for the week, while Japan attracted $3.6 billion, emerging markets saw $3.5 billion and Europe pulled in $2.2 billion in creations. U.S. large-cap funds were the biggest beneficiary by style, with $6.5 billion of inflows, BofAML reported.

By sector, financials took in $1.6 billion while technology and energy saw $700 million apiece.

The surge comes as sentiment surveys show both professional and mom-and-pop investors getting much more optimistic.

The latest Investors Intelligence poll of professional newsletter editors saw bulls outnumber bears by 66.7 percent to 12.7 percent, the biggest spread since April 1986. The American Association of Independent Investors retail survey reported the difference at 54.1 percent to 21.4 percent.