We introduce a simple extension of the minority game in which the market rewards contrarian (resp. trend-following) strategies when it is far from (resp. close to) efficiency. The model displays a smooth crossover from a regime where contrarians dominate to one where trend-followers dominate. In the intermediate phase, the stationary state is characterized by non-Gaussian features as well as by the formation of sustained trends and bubbles.PACS numbers: 89.65. Gh, 05.70.Fh Financial markets are known to generate non-trivial fluctuation phenomena [1,2] that are qualitatively reproduced by several models where agents with prescribed trading rules interact through a complex mechanism of price formation [3,4,5,6,7]. Originally devised to get a more fundamental grasp on the critical behavior of systems of heterogeneous agents, the minority game [8] was able to capture some of the complex macroscopic phenomenology of markets starting from primitive microscopic ingredients [9,10,11], clarifying the roles of different factors contributing to the complexity of market dynamics. Still, many important issues escape a more basic investigation.One of these is the interaction of different types of agents. Broadly speaking, traders can be divided in two groups, namely contrarians (or fundamentalists) and trend-followers (or chartists). The former believe that the market is close to a stationary state and buy (sell) when they repute the stock to be underpriced (overpriced), thus inducing anti-correlation in market returns and holding the price close to its 'fundamental' value. The latter, instead, extrapolate trends from recent price increments and buy or sell assuming that the next increment will occur in the direction of the trend, thus creating positive return correlations and large price drifts ('bubbles'). Chartist behavior, which can also be driven by imitation, is known to cause market instability [6,12]. Fundamentalists act instead as a restoring force that dumps market inefficiencies and excess volatility. It has been argued [12,13] that contrarians (trend followers) are described by minority (majority) game players (but see also [14,15]), and the analysis of mixed majority-minority games has shown that the presence of trend followers can severely alter the market's efficiency [16]. However, agents in these models are committed to either one of the types, and switching from one group to the other, a key feature in other models [6], is not allowed.Here we introduce a class of market games that bypasses this limitation. We assume that trend-following behavior dominates when price movements are small, as agents try to anticipate trends, whereas traders turn to a contrarian conduct when the market becomes chaotic. This mechanism causes a 'feedback' in the dynamics of the excess demand: when it is small, trend-followers dominate and drive it to larger values; but once it has become sufficiently large, contrarians take over and drive it back to smaller values by inducing anti-correlations. In this way, it is the market tha...