In this paper, we predict the prices of gold and bitcoin in five years in a risky and risk-free environment, respectively, and make decisions that maximize profits after five years, starting with $1,000. It also introduces interventions on transaction costs to obtain a series of optimal decision results. In the environment where risk is introduced, this paper first uses a combination of rolling forecasts and XGBoost models to predict the daily prices of bitcoin and gold, and then introduces the concept of risky investment, builds a dynamic investment optimization model under the risky environment based on the predicted values, and draws conclusions about the optimal strategy under risky investment. Then, in the risk-free environment, this paper determines the decision and timing of all trades by modeling a moving average trading system with convergence dispersion rate. In addition, this paper derives trading strategies under a range of costs by intervening on the transaction costs. Based on the relationship between cash position and trading time under different costs, it is concluded that assets are severely affected by changes in costs.