| 147 | save_json(valid_records, valid_save_path) |
| 148 | |
| 149 | def run(cfg, env, plots, memory, provider, diverse_query, strategy_agents, exp_path, mode = "train"): |
| 150 | |
| 151 | trading_records_path = os.path.join(exp_path, "trading_records") |
| 152 | os.makedirs(trading_records_path, exist_ok=True) |
| 153 | memory_path = os.path.join(exp_path, "memory_records") |
| 154 | os.makedirs(memory_path, exist_ok=True) |
| 155 | |
| 156 | if cfg.if_load_trading_record and cfg.trading_record_path is not None: |
| 157 | print("load trading records...") |
| 158 | record_path = os.path.join(cfg.root, cfg.trading_record_path) |
| 159 | trading_records = load_json(record_path) |
| 160 | else: |
| 161 | trading_records = { |
| 162 | "symbol": [], |
| 163 | "day": [], |
| 164 | "value": [], |
| 165 | "cash": [], |
| 166 | "position": [], |
| 167 | "ret": [], |
| 168 | "date": [], |
| 169 | "price": [], |
| 170 | "discount": [], |
| 171 | "kline_path": [], |
| 172 | "trading_path": [], |
| 173 | "total_profit": [], |
| 174 | "total_return": [], |
| 175 | "action": [], |
| 176 | "reasoning": [], |
| 177 | } |
| 178 | |
| 179 | state, info = env.reset() |
| 180 | |
| 181 | if cfg.checkpoint_start_date is not None: |
| 182 | for action, date in zip(trading_records["action"], trading_records["date"]): |
| 183 | if date <= cfg.checkpoint_start_date: |
| 184 | action = env.action_map[action] |
| 185 | state, reward, done, truncated, info = env.step(action) |
| 186 | else: |
| 187 | break |
| 188 | |
| 189 | while True: |
| 190 | |
| 191 | action = run_step(cfg, |
| 192 | state, |
| 193 | info, |
| 194 | plots, |
| 195 | memory, |
| 196 | provider, |
| 197 | diverse_query, |
| 198 | strategy_agents, |
| 199 | exp_path, |
| 200 | trading_records, |
| 201 | mode) |
| 202 | |
| 203 | assert action in env.action_map.keys(), f"Action {action} is not in the action map {env.action_map.keys()}" |
| 204 | |
| 205 | action = env.action_map[action] |
| 206 | state, reward, done, truncated, info = env.step(action) |