text: import pandas as pd
  import numpy as np
  import matplotlib
  import matplotlib.pyplot as plt
  from sklearn import preprocessing
  
  matplotlib.use("Agg")
  import datetime
  
  from finrl.config import config
  from finrl.marketdata.yahoodownloader import YahooDownloader
  from finrl.preprocessing.preprocessors import FeatureEngineer
  from finrl.preprocessing.data import data_split
  from finrl.env.env_stocktrading import StockTradingEnv
  from finrl.model.models import DRLAgent
  from finrl.trade.backtest import BackTestStats
  
  
  def train_one():
      """
      train an agent
      """
      print("==============Start Fetching Data===========")
      df = YahooDownloader(
          start_date=config.START_DATE,
          end_date=config.END_DATE,
          ticker_list=config.DOW_30_TICKER,
      ).fetch_data()
      print("==============Start Feature Engineering===========")
      fe = FeatureEngineer(
                      use_technical_indicator=True,
                      tech_indicator_list = config.TECHNICAL_INDICATORS_LIST,
                      use_turbulence=True,
                      user_defined_feature = False)
  
      processed = fe.preprocess_data(df)
  
      # Training & Trade data split
      train = data_split(processed, config.START_DATE, config.START_TRADE_DATE)
      trade = data_split(processed, config.START_TRADE_DATE, config.END_DATE)
  
      # data normalization
      # feaures_list = list(train.columns)
      # feaures_list.remove('date')
      # feaures_list.remove('tic')
      # feaures_list.remove('close')
      # print(feaures_list)
      # data_normaliser = preprocessing.StandardScaler()
      # train[feaures_list] = data_normaliser.fit_transform(train[feaures_list])
      # trade[feaures_list] = data_normaliser.fit_transform(trade[feaures_list])
  
      # calculate state action space
      stock_dimension = len(train.tic.unique())
      state_space = (
          1
          + 2 * stock_dimension
          + len(config.TECHNICAL_INDICATORS_LIST) * stock_dimension
      )
  
      env_kwargs = {
          "hmax": 100, 
          "initial_amount": 1000000, 
          "transaction_cost_pct": 0.001, 
          "state_space": state_space, 
          "stock_dim": stock_dimension, 
          "tech_indicator_list": config.TECHNICAL_INDICATORS_LIST, 
          "action_space": stock_dimension, 
          "reward_scaling": 1e-4
          
      }
  
      e_train_gym = StockTradingEnv(df = train, **env_kwargs)
  
      e_trade_gym = StockTradingEnv(df = trade, turbulence_threshold = 250, **env_kwargs)
      env_train, _ = e_train_gym.get_sb_env()
      env_trade, obs_trade = e_trade_gym.get_sb_env()
  
      agent = DRLAgent(env=env_train)
  
      print("==============Model Training===========")
      now = datetime.datetime.now().strftime("%Y%m%d-%Hh%M")
  
      model_sac = agent.get_model("sac")
      trained_sac = agent.train_model(model=model_sac, 
          tb_log_name='sac', total_timesteps=80000
      )
  
      print("==============Start Trading===========")
      df_account_value, df_actions = DRLAgent.DRL_prediction(
          model=trained_sac, test_data=trade, test_env=env_trade, test_obs=obs_trade
      )
      df_account_value.to_csv(
          "./" + config.RESULTS_DIR + "/df_account_value_" + now + ".csv"
      )
      df_actions.to_csv("./" + config.RESULTS_DIR + "/df_actions_" + now + ".csv")
  
      print("==============Get Backtest Results===========")
      perf_stats_all = BackTestStats(df_account_value)
      perf_stats_all = pd.DataFrame(perf_stats_all)
      perf_stats_all.to_csv("./" + config.RESULTS_DIR + "/perf_stats_all_" + now + ".csv")
  