According to the attitude estimation and three-axis magnetometer on-line calibration, a real time moving horizon estimation algorithm is presented in this paper. First, moving horizon estimation filter is designed since system constraints existing in most practical cases cannot be solved analytically in the framework of Kalman filter. Taking advantage of the optimal problem in dealing with constraints, the presented method converts the attitude estimation problem into an optimal one by which the quaternion normalization property can be solved analytically in smaller searching space with better efficiency and accuracy. Second, through a series of linearization of system equations, Gauss-Newton iterative method is applied in the horizon window composed of finite history information to obtain the best state estimation and meet the real time requirement at the same time. Once the newest best state estimation value is obtained, it will be sacked into the horizon window and the oldest one discarded. By this way, the filter is moving forward. Finally, based on the proposed method, the three-axis magnetometer parameter on-line calibration combined with attitude estimation is solved without adding any system state dimension, which can also make sure that the measurements with three-axis magnetometer are in the form of vector as its obvious benefits in the sense of ensuring information quantity. On considering the extreme environment such as great temperature gradient, mechanical pressure and complex electromagnetic fields, different from that of the off-line calibration, the calibration parameter is changed definitely. So the on-line calibration is necessary though neglected by most papers. Simulation results show that under the condition of small initial errors, the difference of accuracy among EKF, UKF and moving horizon estimation is small. But the computational burden of the last one is relatively large. The advantage of the described method is not so obvious in this case. But when the initial errors are large, the moving horizon estimation still can get the precise results no matter how great are the EKF (extended Kalman filter) and UKF (unscented Kalman filter) deviated from the true values. Thus the proposed method has its high accuracy and poor sensitivity of the initial and systematic errors along with fast convergence, all of which are vital in most actual environments.