  The main object considered in this paper is the trace function, defined as a
suitably normalized trace of a product of intertwining operators for the
Drinfeld-Jimbo quantum group, multiplied by the exponential of an element of
the Cartan subalgebra. This function depends of two parameters -- the element
of the Cartan subalgebra, and the highest weight of the Verma module in which
the trace is taken. The main results of the paper are that the trace function
satisfies two systems of difference equations with respect to the first
parameter (the quantum Knizhnik-Zamolodchikov-Bernard and Macdonald-Ruijsenaars
equations), and that it is symmetric with respect to the two parameters. In
particular, this implies that for each of the above two systems of equations
there is the dual system with respect to the second parameter, which is also
satisfied by the trace function.
  The paper establishes a connection between the I.Frenkel-Reshetikhin theory
of quantum conformal blocks, the work of Felder-Mukhin-Tarasov-Varchenko on the
quantum KZB and Ruijsenaars equations, the work of Etingof-I.Frenkel- Kirillov
Jr.-Styrkas on traces of intetwining operators, and the Macdonald- Cherednik
theory. The methods of the paper are based on the theory of dynamical twists
and R-matrices.
