[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"similar-AminHP--gym-anytrading":3,"tool-AminHP--gym-anytrading":61},[4,18,26,36,44,53],{"id":5,"name":6,"github_repo":7,"description_zh":8,"stars":9,"difficulty_score":10,"last_commit_at":11,"category_tags":12,"status":17},4358,"openclaw","openclaw\u002Fopenclaw","OpenClaw 是一款专为个人打造的本地化 AI 助手，旨在让你在自己的设备上拥有完全可控的智能伙伴。它打破了传统 AI 助手局限于特定网页或应用的束缚，能够直接接入你日常使用的各类通讯渠道，包括微信、WhatsApp、Telegram、Discord、iMessage 等数十种平台。无论你在哪个聊天软件中发送消息，OpenClaw 都能即时响应，甚至支持在 macOS、iOS 和 Android 设备上进行语音交互，并提供实时的画布渲染功能供你操控。\n\n这款工具主要解决了用户对数据隐私、响应速度以及“始终在线”体验的需求。通过将 AI 部署在本地，用户无需依赖云端服务即可享受快速、私密的智能辅助，真正实现了“你的数据，你做主”。其独特的技术亮点在于强大的网关架构，将控制平面与核心助手分离，确保跨平台通信的流畅性与扩展性。\n\nOpenClaw 非常适合希望构建个性化工作流的技术爱好者、开发者，以及注重隐私保护且不愿被单一生态绑定的普通用户。只要具备基础的终端操作能力（支持 macOS、Linux 及 Windows WSL2），即可通过简单的命令行引导完成部署。如果你渴望拥有一个懂你",349277,3,"2026-04-06T06:32:30",[13,14,15,16],"Agent","开发框架","图像","数据工具","ready",{"id":19,"name":20,"github_repo":21,"description_zh":22,"stars":23,"difficulty_score":10,"last_commit_at":24,"category_tags":25,"status":17},3808,"stable-diffusion-webui","AUTOMATIC1111\u002Fstable-diffusion-webui","stable-diffusion-webui 是一个基于 Gradio 构建的网页版操作界面，旨在让用户能够轻松地在本地运行和使用强大的 Stable Diffusion 图像生成模型。它解决了原始模型依赖命令行、操作门槛高且功能分散的痛点，将复杂的 AI 绘图流程整合进一个直观易用的图形化平台。\n\n无论是希望快速上手的普通创作者、需要精细控制画面细节的设计师，还是想要深入探索模型潜力的开发者与研究人员，都能从中获益。其核心亮点在于极高的功能丰富度：不仅支持文生图、图生图、局部重绘（Inpainting）和外绘（Outpainting）等基础模式，还独创了注意力机制调整、提示词矩阵、负向提示词以及“高清修复”等高级功能。此外，它内置了 GFPGAN 和 CodeFormer 等人脸修复工具，支持多种神经网络放大算法，并允许用户通过插件系统无限扩展能力。即使是显存有限的设备，stable-diffusion-webui 也提供了相应的优化选项，让高质量的 AI 艺术创作变得触手可及。",162132,"2026-04-05T11:01:52",[14,15,13],{"id":27,"name":28,"github_repo":29,"description_zh":30,"stars":31,"difficulty_score":32,"last_commit_at":33,"category_tags":34,"status":17},1381,"everything-claude-code","affaan-m\u002Feverything-claude-code","everything-claude-code 是一套专为 AI 编程助手（如 Claude Code、Codex、Cursor 等）打造的高性能优化系统。它不仅仅是一组配置文件，而是一个经过长期实战打磨的完整框架，旨在解决 AI 代理在实际开发中面临的效率低下、记忆丢失、安全隐患及缺乏持续学习能力等核心痛点。\n\n通过引入技能模块化、直觉增强、记忆持久化机制以及内置的安全扫描功能，everything-claude-code 能显著提升 AI 在复杂任务中的表现，帮助开发者构建更稳定、更智能的生产级 AI 代理。其独特的“研究优先”开发理念和针对 Token 消耗的优化策略，使得模型响应更快、成本更低，同时有效防御潜在的攻击向量。\n\n这套工具特别适合软件开发者、AI 研究人员以及希望深度定制 AI 工作流的技术团队使用。无论您是在构建大型代码库，还是需要 AI 协助进行安全审计与自动化测试，everything-claude-code 都能提供强大的底层支持。作为一个曾荣获 Anthropic 黑客大奖的开源项目，它融合了多语言支持与丰富的实战钩子（hooks），让 AI 真正成长为懂上",150037,2,"2026-04-10T23:33:47",[14,13,35],"语言模型",{"id":37,"name":38,"github_repo":39,"description_zh":40,"stars":41,"difficulty_score":32,"last_commit_at":42,"category_tags":43,"status":17},2271,"ComfyUI","Comfy-Org\u002FComfyUI","ComfyUI 是一款功能强大且高度模块化的视觉 AI 引擎，专为设计和执行复杂的 Stable Diffusion 图像生成流程而打造。它摒弃了传统的代码编写模式，采用直观的节点式流程图界面，让用户通过连接不同的功能模块即可构建个性化的生成管线。\n\n这一设计巧妙解决了高级 AI 绘图工作流配置复杂、灵活性不足的痛点。用户无需具备编程背景，也能自由组合模型、调整参数并实时预览效果，轻松实现从基础文生图到多步骤高清修复等各类复杂任务。ComfyUI 拥有极佳的兼容性，不仅支持 Windows、macOS 和 Linux 全平台，还广泛适配 NVIDIA、AMD、Intel 及苹果 Silicon 等多种硬件架构，并率先支持 SDXL、Flux、SD3 等前沿模型。\n\n无论是希望深入探索算法潜力的研究人员和开发者，还是追求极致创作自由度的设计师与资深 AI 绘画爱好者，ComfyUI 都能提供强大的支持。其独特的模块化架构允许社区不断扩展新功能，使其成为当前最灵活、生态最丰富的开源扩散模型工具之一，帮助用户将创意高效转化为现实。",108322,"2026-04-10T11:39:34",[14,15,13],{"id":45,"name":46,"github_repo":47,"description_zh":48,"stars":49,"difficulty_score":32,"last_commit_at":50,"category_tags":51,"status":17},6121,"gemini-cli","google-gemini\u002Fgemini-cli","gemini-cli 是一款由谷歌推出的开源 AI 命令行工具，它将强大的 Gemini 大模型能力直接集成到用户的终端环境中。对于习惯在命令行工作的开发者而言，它提供了一条从输入提示词到获取模型响应的最短路径，无需切换窗口即可享受智能辅助。\n\n这款工具主要解决了开发过程中频繁上下文切换的痛点，让用户能在熟悉的终端界面内直接完成代码理解、生成、调试以及自动化运维任务。无论是查询大型代码库、根据草图生成应用，还是执行复杂的 Git 操作，gemini-cli 都能通过自然语言指令高效处理。\n\n它特别适合广大软件工程师、DevOps 人员及技术研究人员使用。其核心亮点包括支持高达 100 万 token 的超长上下文窗口，具备出色的逻辑推理能力；内置 Google 搜索、文件操作及 Shell 命令执行等实用工具；更独特的是，它支持 MCP（模型上下文协议），允许用户灵活扩展自定义集成，连接如图像生成等外部能力。此外，个人谷歌账号即可享受免费的额度支持，且项目基于 Apache 2.0 协议完全开源，是提升终端工作效率的理想助手。",100752,"2026-04-10T01:20:03",[52,13,15,14],"插件",{"id":54,"name":55,"github_repo":56,"description_zh":57,"stars":58,"difficulty_score":32,"last_commit_at":59,"category_tags":60,"status":17},4721,"markitdown","microsoft\u002Fmarkitdown","MarkItDown 是一款由微软 AutoGen 团队打造的轻量级 Python 工具，专为将各类文件高效转换为 Markdown 格式而设计。它支持 PDF、Word、Excel、PPT、图片（含 OCR）、音频（含语音转录）、HTML 乃至 YouTube 链接等多种格式的解析，能够精准提取文档中的标题、列表、表格和链接等关键结构信息。\n\n在人工智能应用日益普及的今天，大语言模型（LLM）虽擅长处理文本，却难以直接读取复杂的二进制办公文档。MarkItDown 恰好解决了这一痛点，它将非结构化或半结构化的文件转化为模型“原生理解”且 Token 效率极高的 Markdown 格式，成为连接本地文件与 AI 分析 pipeline 的理想桥梁。此外，它还提供了 MCP（模型上下文协议）服务器，可无缝集成到 Claude Desktop 等 LLM 应用中。\n\n这款工具特别适合开发者、数据科学家及 AI 研究人员使用，尤其是那些需要构建文档检索增强生成（RAG）系统、进行批量文本分析或希望让 AI 助手直接“阅读”本地文件的用户。虽然生成的内容也具备一定可读性，但其核心优势在于为机器",93400,"2026-04-06T19:52:38",[52,14],{"id":62,"github_repo":63,"name":64,"description_en":65,"description_zh":66,"ai_summary_zh":66,"readme_en":67,"readme_zh":68,"quickstart_zh":69,"use_case_zh":70,"hero_image_url":71,"owner_login":72,"owner_name":73,"owner_avatar_url":74,"owner_bio":75,"owner_company":76,"owner_location":75,"owner_email":77,"owner_twitter":75,"owner_website":75,"owner_url":78,"languages":79,"stars":84,"forks":85,"last_commit_at":86,"license":87,"difficulty_score":88,"env_os":89,"env_gpu":90,"env_ram":90,"env_deps":91,"category_tags":98,"github_topics":99,"view_count":32,"oss_zip_url":75,"oss_zip_packed_at":75,"status":17,"created_at":110,"updated_at":111,"faqs":112,"releases":142},6474,"AminHP\u002Fgym-anytrading","gym-anytrading","The most simple, flexible, and comprehensive OpenAI Gym trading environment (Approved by OpenAI Gym)","gym-anytrading 是一个专为强化学习交易算法设计的开源工具包，基于广受欢迎的 OpenAI Gym 框架构建。它旨在解决金融交易领域缺乏统一、灵活且易于上手的仿真环境这一痛点，帮助开发者和研究人员更高效地训练和测试自动交易策略。\n\n该工具主要面向外汇（FOREX）和股票两大市场，提供了三个核心环境：通用的抽象环境 TradingEnv，以及分别针对外汇和股市的 ForexEnv 与 StocksEnv。其独特的技术亮点在于对复杂交易逻辑的极致简化：摒弃了传统算法中繁琐的“持有”、“入场”、“出场”等多重动作定义，仅保留最核心的“买入”与“卖出”两个动作；同时将持仓状态简化为“做多”与“做空”。这种设计大幅降低了智能体的学习难度，避免了因动作空间过大导致的训练效率低下问题，让模型能更专注于捕捉市场规律。\n\ngym-anytrading 非常适合从事量化交易研究的科研人员、希望尝试强化学习在金融领域应用的开发者，以及需要快速验证交易想法的算法工程师。通过简单的 pip 安装即可上手，它为探索自动化交易策略提供了一个坚实且灵活的起点，让用户能将精力集中在策略优化而非环境搭建上。","# gym-anytrading\n\n`AnyTrading` is a collection of [OpenAI Gym](https:\u002F\u002Fgithub.com\u002Fopenai\u002Fgym) environments for reinforcement learning-based trading algorithms.\n\nTrading algorithms are mostly implemented in two markets: [FOREX](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FForeign_exchange_market) and [Stock](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FStock). AnyTrading aims to provide some Gym environments to improve and facilitate the procedure of developing and testing RL-based algorithms in this area. This purpose is obtained by implementing three Gym environments: **TradingEnv**, **ForexEnv**, and **StocksEnv**.\n\nTradingEnv is an abstract environment which is defined to support all kinds of trading environments. ForexEnv and StocksEnv are simply two environments that inherit and extend TradingEnv. In the future sections, more explanations will be given about them but before that, some environment properties should be discussed.\n\n**Note:** For experts, it is recommended to check out the [gym-mtsim](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-mtsim) project.\n\n## Installation\n\n### Via PIP\n```bash\npip install gym-anytrading\n```\n\n### From Repository\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\ncd gym-anytrading\npip install -e .\n\n## or\n\npip install --upgrade --no-deps --force-reinstall https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Farchive\u002Fmaster.zip\n```\n\n## Environment Properties\nFirst of all, **you can't simply expect an RL agent to do everything for you and just sit back on your chair in such complex trading markets!**\nThings need to be simplified as much as possible in order to let the agent learn in a faster and more efficient way. In all trading algorithms, the first thing that should be done is to define **actions** and **positions**. In the two following subsections, I will explain these actions and positions and how to simplify them.\n\n### Trading Actions\nIf you search on the Internet for trading algorithms, you will find them using numerous actions such as **Buy**, **Sell**, **Hold**, **Enter**, **Exit**, etc.\nReferring to the first statement of this section, a typical RL agent can only solve a part of the main problem in this area. If you work in trading markets you will learn that deciding whether to hold, enter, or exit a pair (in FOREX) or stock (in Stocks) is a statistical decision depending on many parameters such as your budget, pairs or stocks you trade, your money distribution policy in multiple markets, etc. It's a massive burden for an RL agent to consider all these parameters and may take years to develop such an agent! In this case, you certainly will not use this environment but you will extend your own.\n\nSo after months of work, I finally found out that these actions just make things complicated with no real positive impact. In fact, they just increase the learning time and an action like **Hold** will be barely used by a well-trained agent because it doesn't want to miss a single penny. Therefore there is no need to have such numerous actions and only `Sell=0` and `Buy=1` actions are adequate to train an agent just as well.\n\n### Trading Positions\nIf you're not familiar with trading positions, refer [here](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FPosition_\\(finance\\)). It's a very important concept and you should learn it as soon as possible.\n\nIn a simple vision: **Long** position wants to buy shares when prices are low and profit by sticking with them while their value is going up, and **Short** position wants to sell shares with high value and use this value to buy shares at a lower value, keeping the difference as profit.\n\nAgain, in some trading algorithms, you may find numerous positions such as **Short**, **Long**, **Flat**, etc. As discussed earlier, I use only `Short=0` and `Long=1` positions.\n\n## Trading Environments\nAs I noticed earlier, now it's time to introduce the three environments. Before creating this project, I spent so much time to search for a simple and flexible Gym environment for any trading market but didn't find one. They were almost a bunch of complex codes with many unclear parameters that you couldn't simply look at them and comprehend what's going on. So I concluded to implement this project with a great focus on simplicity, flexibility, and comprehensiveness.\n\nIn the three following subsections, I will introduce our trading environments and in the next section, some IPython examples will be mentioned and briefly explained.\n\n### TradingEnv\nTradingEnv is an abstract class which inherits `gym.Env`. This class aims to provide a general-purpose environment for all kinds of trading markets. Here I explain its public properties and methods. But feel free to take a look at the complete [source code](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Ftrading_env.py).\n\n* Properties:\n> `df`: An abbreviation for **DataFrame**. It's a **pandas'** DataFrame which contains your dataset and is passed in the class' constructor.\n>\n> `prices`: Real prices over time. Used to calculate profit and render the environment.\n>\n> `signal_features`: Extracted features over time. Used to create *Gym observations*.\n>\n> `window_size`: Number of ticks (current and previous ticks) returned as a *Gym observation*. It is passed in the class' constructor.\n>\n> `action_space`: The *Gym action_space* property. Containing discrete values of **0=Sell** and **1=Buy**.\n>\n> `observation_space`: The *Gym observation_space* property. Each observation is a window on `signal_features` from index **current_tick - window_size + 1** to **current_tick**. So `_start_tick` of the environment would be equal to `window_size`. In addition, initial value for `_last_trade_tick` is **window_size - 1** .\n>\n> `shape`: Shape of a single observation.\n>\n> `history`: Stores the information of all steps.\n\n* Methods:\n> `seed`: Typical *Gym seed* method.\n>\n> `reset`: Typical *Gym reset* method.\n>\n> `step`: Typical *Gym step* method.\n>\n> `render`: Typical *Gym render* method. Renders the information of the environment's current tick.\n>\n> `render_all`: Renders the whole environment.\n>\n> `close`: Typical *Gym close* method.\n\n* Abstract Methods:\n> `_process_data`: It is called in the constructor and returns `prices` and `signal_features` as a tuple. In different trading markets, different features need to be obtained. So this method enables our TradingEnv to be a general-purpose environment and specific features can be returned for specific environments such as *FOREX*, *Stocks*, etc.\n>\n> `_calculate_reward`: The reward function for the RL agent.\n>\n> `_update_profit`: Calculates and updates total profit which the RL agent has achieved so far. Profit indicates the amount of units of currency you have achieved by starting with *1.0* unit (Profit = FinalMoney \u002F StartingMoney).\n>\n> `max_possible_profit`: The maximum possible profit that an RL agent can obtain regardless of trade fees.\n\n### ForexEnv\nThis is a concrete class which inherits TradingEnv and implements its abstract methods. Also, it has some specific properties for the *FOREX* market. For more information refer to the [source code](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Fforex_env.py).\n\n* Properties:\n> `frame_bound`: A tuple which specifies the start and end of `df`. It is passed in the class' constructor.\n>\n> `unit_side`: Specifies the side you start your trading. Containing string values of **left** (default value) and **right**. As you know, there are two sides in a currency pair in *FOREX*. For example in the *EUR\u002FUSD* pair, when you choose the `left` side, your currency unit is *EUR* and you start your trading with 1 EUR. It is passed in the class' constructor.\n>\n> `trade_fee`: A default constant fee which is subtracted from the real prices on every trade.\n\n\n### StocksEnv\nSame as ForexEnv but for the *Stock* market. For more information refer to the [source code](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Fstocks_env.py).\n\n* Properties:\n> `frame_bound`: A tuple which specifies the start and end of `df`. It is passed in the class' constructor.\n>\n> `trade_fee_bid_percent`: A default constant fee percentage for bids. For example with trade_fee_bid_percent=0.01, you will lose 1% of your money every time you sell your shares.\n>\n> `trade_fee_ask_percent`: A default constant fee percentage for asks. For example with trade_fee_ask_percent=0.005, you will lose 0.5% of your money every time you buy some shares.\n\nBesides, you can create your own customized environment by extending TradingEnv or even ForexEnv or StocksEnv with your desired policies for calculating reward, profit, fee, etc.\n\n## Examples\n\n\n### Create an environment\n\n\n```python\nimport gymnasium as gym\nimport gym_anytrading\n\nenv = gym.make('forex-v0')\n# env = gym.make('stocks-v0')\n```\n\n- This will create the default environment. You can change any parameters such as dataset, frame_bound, etc.\n\n### Create an environment with custom parameters\nI put two default datasets for [*FOREX*](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fdatasets\u002Fdata\u002FFOREX_EURUSD_1H_ASK.csv) and [*Stocks*](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fdatasets\u002Fdata\u002FSTOCKS_GOOGL.csv) but you can use your own.\n\n\n```python\nfrom gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL\n\ncustom_env = gym.make(\n    'forex-v0',\n    df=FOREX_EURUSD_1H_ASK,\n    window_size=10,\n    frame_bound=(10, 300),\n    unit_side='right'\n)\n\n# custom_env = gym.make(\n#     'stocks-v0',\n#     df=STOCKS_GOOGL,\n#     window_size=10,\n#     frame_bound=(10, 300)\n# )\n```\n\n- It is to be noted that the first element of `frame_bound` should be greater than or equal to `window_size`.\n\n### Print some information\n\n\n```python\nprint(\"env information:\")\nprint(\"> shape:\", env.unwrapped.shape)\nprint(\"> df.shape:\", env.unwrapped.df.shape)\nprint(\"> prices.shape:\", env.unwrapped.prices.shape)\nprint(\"> signal_features.shape:\", env.unwrapped.signal_features.shape)\nprint(\"> max_possible_profit:\", env.unwrapped.max_possible_profit())\n\nprint()\nprint(\"custom_env information:\")\nprint(\"> shape:\", custom_env.unwrapped.shape)\nprint(\"> df.shape:\", custom_env.unwrapped.df.shape)\nprint(\"> prices.shape:\", custom_env.unwrapped.prices.shape)\nprint(\"> signal_features.shape:\", custom_env.unwrapped.signal_features.shape)\nprint(\"> max_possible_profit:\", custom_env.unwrapped.max_possible_profit())\n```\n\n    env information:\n    > shape: (24, 2)\n    > df.shape: (6225, 5)\n    > prices.shape: (6225,)\n    > signal_features.shape: (6225, 2)\n    > max_possible_profit: 4.054407219413578\n    \n    custom_env information:\n    > shape: (10, 2)\n    > df.shape: (6225, 5)\n    > prices.shape: (300,)\n    > signal_features.shape: (300, 2)\n    > max_possible_profit: 1.1228998536878634\n    \n\n- Here `max_possible_profit` signifies that if the market didn't have trade fees, you could have earned **4.054414887146572** (or **1.1229001800089833**) units of currency by starting with **1.0**. In other words, your money is almost *quadrupled*.\n\n### Plot the environment\n\n\n```python\nenv.reset()\nenv.render()\n```\n\n\n    \n![png](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FAminHP_gym-anytrading_readme_958f99fae286.png)\n    \n\n\n- **Short** and **Long** positions are shown in `red` and `green` colors.\n- As you see, the starting *position* of the environment is always **Short**.\n\n### A complete example\n\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport gymnasium as gym\nimport gym_anytrading\nfrom gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions \nfrom gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL\n\n\nenv = gym.make('forex-v0', frame_bound=(50, 100), window_size=10)\n# env = gym.make('stocks-v0', frame_bound=(50, 100), window_size=10)\n\nobservation = env.reset(seed=2023)\nwhile True:\n    action = env.action_space.sample()\n    observation, reward, terminated, truncated, info = env.step(action)\n    done = terminated or truncated\n\n    # env.render()\n    if done:\n        print(\"info:\", info)\n        break\n\nplt.cla()\nenv.unwrapped.render_all()\nplt.show()\n```\n\n    info: {'total_reward': 27.89616584777832, 'total_profit': 0.989812615901, 'position': \u003CPositions.Long: 1>}\n    \n\n\n    \n![png](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FAminHP_gym-anytrading_readme_c30d70c7df0b.png)\n    \n\n\n- You can use `render_all` method to avoid rendering on each step and prevent time-wasting.\n- As you see, the first **10** points (`window_size`=10) on the plot don't have a *position*. Because they aren't involved in calculating reward, profit, etc. They just display the first observations. So the environment's `_start_tick` and initial `_last_trade_tick` are **10** and **9**.\n\n#### More examples\n\n[Here](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fexamples) are some examples that mix `gym-anytrading` with some well-known libraries, such as `Stable-Baselines3` and `QuantStats`, and show how to utilize our trading environments in other RL or trading libraries.\n\n### Extend and manipulate TradingEnv\n\nIn case you want to process data and extract features outside the environment, it can be simply done by two methods:\n\n**Method 1 (Recommended):**\n\n\n```python\ndef my_process_data(env):\n    start = env.frame_bound[0] - env.window_size\n    end = env.frame_bound[1]\n    prices = env.df.loc[:, 'Low'].to_numpy()[start:end]\n    signal_features = env.df.loc[:, ['Close', 'Open', 'High', 'Low']].to_numpy()[start:end]\n    return prices, signal_features\n\n\nclass MyForexEnv(ForexEnv):\n    _process_data = my_process_data\n\n\nenv = MyForexEnv(df=FOREX_EURUSD_1H_ASK, window_size=12, frame_bound=(12, len(FOREX_EURUSD_1H_ASK)))\n```\n\n**Method 2:**\n\n\n```python\ndef my_process_data(df, window_size, frame_bound):\n    start = frame_bound[0] - window_size\n    end = frame_bound[1]\n    prices = df.loc[:, 'Low'].to_numpy()[start:end]\n    signal_features = df.loc[:, ['Close', 'Open', 'High', 'Low']].to_numpy()[start:end]\n    return prices, signal_features\n\n\nclass MyStocksEnv(StocksEnv):\n    \n    def __init__(self, prices, signal_features, **kwargs):\n        self._prices = prices\n        self._signal_features = signal_features\n        super().__init__(**kwargs)\n\n    def _process_data(self):\n        return self._prices, self._signal_features\n\n    \nprices, signal_features = my_process_data(df=STOCKS_GOOGL, window_size=30, frame_bound=(30, len(STOCKS_GOOGL)))\nenv = MyStocksEnv(prices, signal_features, df=STOCKS_GOOGL, window_size=30, frame_bound=(30, len(STOCKS_GOOGL)))\n```\n\n## Related Projects\n\n* A more complicated version of `anytrading` with five actions, three positions, and a better reward function is developed in the [DI-engine](https:\u002F\u002Fgithub.com\u002Fopendilab\u002FDI-engine\u002Ftree\u002Fmain\u002Fdizoo\u002Fgym_anytrading) project. It is a mid-level tool (somewhere between `anytrading` and `mtsim`), appropriate for semi-experts. More information and documentation can be found [here](https:\u002F\u002Fgithub.com\u002Fopendilab\u002FDI-engine\u002Ftree\u002Fmain\u002Fdizoo\u002Fgym_anytrading\u002Fenvs).\n","# gym-anytrading\n\n`AnyTrading` 是一个基于 [OpenAI Gym](https:\u002F\u002Fgithub.com\u002Fopenai\u002Fgym) 的环境集合，专为强化学习驱动的交易算法设计。\n\n交易算法主要应用于两个市场：[外汇市场](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FForeign_exchange_market) 和 [股票市场](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FStock)。AnyTrading 的目标是提供一些 Gym 环境，以改进和简化在这一领域开发与测试基于强化学习的算法的过程。为此，我们实现了三个 Gym 环境：**TradingEnv**、**ForexEnv** 和 **StocksEnv**。\n\nTradingEnv 是一个抽象环境，旨在支持各类交易场景。ForexEnv 和 StocksEnv 则分别继承并扩展了 TradingEnv。在后续章节中，我们将对这些环境进行更详细的说明，但在那之前，先讨论一下它们的一些特性。\n\n**注意：** 对于专家用户，建议查看 [gym-mtsim](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-mtsim) 项目。\n\n## 安装\n\n### 通过 PIP\n```bash\npip install gym-anytrading\n```\n\n### 从仓库安装\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\ncd gym-anytrading\npip install -e .\n\n## 或者\n\npip install --upgrade --no-deps --force-reinstall https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Farchive\u002Fmaster.zip\n```\n\n## 环境特性\n\n首先，**你不能简单地指望一个强化学习智能体在如此复杂的交易市场中为你完成所有操作，然后自己就坐享其成！**\n为了使智能体能够更快、更高效地学习，必须尽可能地简化问题。在所有交易算法中，首要任务就是定义 **动作** 和 **仓位**。在接下来的两个小节中，我将详细解释这些动作和仓位，并说明如何对其进行简化。\n\n### 交易动作\n\n如果你在网上搜索交易算法，会发现它们通常使用多种动作，例如 **买入**、**卖出**、**持有**、**入场**、**出场** 等。\n\n正如本节开头所述，典型的强化学习智能体只能解决该领域中的一部分问题。如果你从事交易工作，就会明白，决定是否持有、入场或出场（在外汇市场中针对货币对，在股票市场中针对个股）是一个依赖于诸多参数的统计决策，比如你的资金预算、交易的货币对或股票、你在多个市场的资金分配策略等。让强化学习智能体考虑所有这些因素是一项极其繁重的任务，可能需要数年时间才能开发出这样的智能体！在这种情况下，你很可能不会直接使用这个环境，而是会基于它扩展出自己的环境。\n\n经过数月的研究，我最终得出结论：这些复杂多样的动作并不会带来实际的积极效果，反而会使学习过程变得更加冗长。事实上，像 **持有** 这样的动作几乎不会被训练有素的智能体采用，因为它会尽量避免错过任何潜在的收益。因此，没有必要设置如此多的动作，仅使用 `Sell=0` 和 `Buy=1` 两种动作就足以有效地训练智能体。\n\n### 交易仓位\n\n如果你不熟悉交易仓位的概念，可以参考 [这里](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FPosition_\\(finance\\))。这是一个非常重要的概念，你应该尽快掌握。\n\n简单来说：**做多** 仓位是指在价格较低时买入股票，并在其价格上涨时获利；而 **做空** 仓位则是指在价格较高时卖出股票，再用所得资金以更低的价格买入股票，从而赚取差价。\n\n同样地，在一些交易算法中，可能会出现多种仓位，如 **做空**、**做多**、**平仓** 等。正如前面所讨论的，我只使用 `Short=0` 和 `Long=1` 两种仓位。\n\n## 交易环境\n\n正如前面提到的，现在是介绍这三个环境的时候了。在创建这个项目之前，我花了大量时间寻找一个简单且灵活的 Gym 环境，适用于任何交易市场，但始终未能找到合适的方案。现有的许多环境要么代码过于复杂，要么包含大量难以理解的参数，让人难以快速了解其运作机制。因此，我决定专注于简洁性、灵活性和全面性来实现这个项目。\n\n在接下来的三个小节中，我将逐一介绍我们的交易环境；而在下一节中，我们将展示并简要说明一些 IPython 示例。\n\n### TradingEnv\n\nTradingEnv 是一个抽象类，继承自 `gym.Env`。该类旨在为各类交易市场提供一个通用环境。下面我将介绍其公开属性和方法。当然，你也可以直接查看完整的 [源代码](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Ftrading_env.py)。\n\n* 属性：\n> `df`：即 **DataFrame** 的缩写。这是一个 `pandas` 的 DataFrame，包含了你的数据集，并在类的构造函数中传入。\n>\n> `prices`：随时间变化的真实价格，用于计算收益并渲染环境。\n>\n> `signal_features`：随时间提取的特征，用于生成 Gym 观测值。\n>\n> `window_size`：作为 Gym 观测值返回的当前及过去若干个时间步的数量，也在构造函数中传入。\n>\n> `action_space`：Gym 的动作空间属性，包含离散值 **0=卖出** 和 **1=买入**。\n>\n> `observation_space`：Gym 的观测空间属性，每个观测都是对 `signal_features` 中从索引 **current_tick - window_size + 1** 到 **current_tick** 的窗口。因此，环境的 `_start_tick` 将等于 `window_size`。此外，初始值为 `_last_trade_tick` 是 `window_size - 1`。\n>\n> `shape`：单个观测的形状。\n>\n> `history`：存储所有步骤的信息。\n\n* 方法：\n> `seed`：标准的 Gym 种子方法。\n>\n> `reset`：标准的 Gym 重置方法。\n>\n> `step`：标准的 Gym 步进方法。\n>\n> `render`：标准的 Gym 渲染方法，用于显示当前时间步的环境信息。\n>\n> `render_all`：渲染整个环境。\n>\n> `close`：标准的 Gym 关闭方法。\n\n* 抽象方法：\n> `_process_data`：在构造函数中调用，返回 `prices` 和 `signal_features` 的元组。不同的交易市场需要提取不同的特征，因此该方法使我们的 TradingEnv 成为一个通用环境，可以根据具体市场（如外汇、股票等）返回相应的特征。\n>\n> `_calculate_reward`：强化学习智能体的奖励函数。\n>\n> `_update_profit`：计算并更新智能体迄今为止获得的总收益。收益表示以 *1.0* 单位货币开始，最终获得的货币单位数量（利润 = 最终金额 \u002F 初始金额）。\n>\n> `max_possible_profit`：在不考虑交易费用的情况下，强化学习智能体可能获得的最大收益。\n\n### ForexEnv\n这是一个具体的类，继承自 TradingEnv 并实现了其抽象方法。此外，它还具有一些专属于 *FOREX* 市场的属性。更多信息请参阅 [源代码](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Fforex_env.py)。\n\n* 属性：\n> `frame_bound`: 一个元组，用于指定 `df` 的起始和结束位置。该参数在类的构造函数中传入。\n>\n> `unit_side`: 指定您开始交易的一方。取值为字符串 **left**（默认值）和 **right**。如您所知，在 *FOREX* 市场中，货币对存在两个方向。例如，在 *EUR\u002FUSD* 货币对中，当您选择 `left` 方向时，您的计价货币是 *EUR*，您将以 1 欧元开始交易。该参数在类的构造函数中传入。\n>\n> `trade_fee`: 一种默认的固定手续费，每次交易时都会从实际价格中扣除。\n\n\n### StocksEnv\n与 ForexEnv 类似，但适用于 *股票* 市场。更多信息请参阅 [源代码](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fenvs\u002Fstocks_env.py)。\n\n* 属性：\n> `frame_bound`: 一个元组，用于指定 `df` 的起始和结束位置。该参数在类的构造函数中传入。\n>\n> `trade_fee_bid_percent`: 买单的默认固定手续费百分比。例如，当 `trade_fee_bid_percent=0.01` 时，每次卖出股票您将损失 1% 的资金。\n>\n> `trade_fee_ask_percent`: 卖单的默认固定手续费百分比。例如，当 `trade_fee_ask_percent=0.005` 时，每次买入股票您将损失 0.5% 的资金。\n\n此外，您还可以通过扩展 TradingEnv，甚至 ForexEnv 或 StocksEnv，创建自己的自定义环境，并根据需要制定奖励、利润、手续费等计算策略。\n\n## 示例\n\n\n### 创建环境\n\n\n```python\nimport gymnasium as gym\nimport gym_anytrading\n\nenv = gym.make('forex-v0')\n# env = gym.make('stocks-v0')\n```\n\n- 这将创建默认环境。您可以更改数据集、`frame_bound` 等任何参数。\n\n### 使用自定义参数创建环境\n我提供了两个默认数据集，分别用于 [*FOREX*](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fdatasets\u002Fdata\u002FFOREX_EURUSD_1H_ASK.csv) 和 [*股票*](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fgym_anytrading\u002Fdatasets\u002Fdata\u002FSTOCKS_GOOGL.csv)，但您也可以使用自己的数据集。\n\n\n```python\nfrom gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL\n\ncustom_env = gym.make(\n    'forex-v0',\n    df=FOREX_EURUSD_1H_ASK,\n    window_size=10,\n    frame_bound=(10, 300),\n    unit_side='right'\n)\n\n# custom_env = gym.make(\n#     'stocks-v0',\n#     df=STOCKS_GOOGL,\n#     window_size=10,\n#     frame_bound=(10, 300)\n# )\n```\n\n- 需要注意的是，`frame_bound` 的第一个元素应大于或等于 `window_size`。\n\n### 打印一些信息\n\n\n```python\nprint(\"env 信息:\")\nprint(\"> shape:\", env.unwrapped.shape)\nprint(\"> df.shape:\", env.unwrapped.df.shape)\nprint(\"> prices.shape:\", env.unwrapped.prices.shape)\nprint(\"> signal_features.shape:\", env.unwrapped.signal_features.shape)\nprint(\"> 最大可能利润:\", env.unwrapped.max_possible_profit())\n\nprint()\nprint(\"custom_env 信息:\")\nprint(\"> shape:\", custom_env.unwrapped.shape)\nprint(\"> df.shape:\", custom_env.unwrapped.df.shape)\nprint(\"> prices.shape:\", custom_env.unwrapped.prices.shape)\nprint(\"> signal_features.shape:\", custom_env.unwrapped.signal_features.shape)\nprint(\"> 最大可能利润:\", custom_env.unwrapped.max_possible_profit())\n```\n\n    env 信息:\n    > shape: (24, 2)\n    > df.shape: (6225, 5)\n    > prices.shape: (6225,)\n    > signal_features.shape: (6225, 2)\n    > 最大可能利润: 4.054407219413578\n    \n    custom_env 信息:\n    > shape: (10, 2)\n    > df.shape: (6225, 5)\n    > prices.shape: (300,)\n    > signal_features.shape: (300, 2)\n    > 最大可能利润: 1.1228998536878634\n    \n\n- 此处的 `max_possible_profit` 表示，如果市场没有交易费用，以 **1.0** 作为初始资金，您最多可以获得 **4.054414887146572**（或 **1.1229001800089833**）单位的收益。换句话说，您的资金几乎翻了四倍。\n\n### 绘制环境\n\n\n```python\nenv.reset()\nenv.render()\n```\n\n\n    \n![png](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FAminHP_gym-anytrading_readme_958f99fae286.png)\n    \n\n\n- `red` 和 `green` 颜色分别表示 **空头** 和 **多头** 头寸。\n- 如您所见，环境的初始 *头寸* 始终是 **空头**。\n\n### 完整示例\n\n\n```python\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport gymnasium as gym\nimport gym_anytrading\nfrom gym_anytrading.envs import TradingEnv, ForexEnv, StocksEnv, Actions, Positions \nfrom gym_anytrading.datasets import FOREX_EURUSD_1H_ASK, STOCKS_GOOGL\n\n\nenv = gym.make('forex-v0', frame_bound=(50, 100), window_size=10)\n# env = gym.make('stocks-v0', frame_bound=(50, 100), window_size=10)\n\nobservation = env.reset(seed=2023)\nwhile True:\n    action = env.action_space.sample()\n    observation, reward, terminated, truncated, info = env.step(action)\n    done = terminated or truncated\n\n    # env.render()\n    if done:\n        print(\"info:\", info)\n        break\n\nplt.cla()\nenv.unwrapped.render_all()\nplt.show()\n```\n\n    info: {'total_reward': 27.89616584777832, 'total_profit': 0.989812615901, 'position': \u003CPositions.Long: 1>}\n    \n\n\n    \n![png](https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FAminHP_gym-anytrading_readme_c30d70c7df0b.png)\n    \n\n\n- 您可以使用 `render_all` 方法避免每一步都进行渲染，从而节省时间。\n- 如您看到的，图表中的前 **10** 个点（`window_size`=10）并没有显示头寸。这是因为它们不参与奖励、利润等的计算，仅用于展示最初的观测数据。因此，环境的 `_start_tick` 和初始 `_last_trade_tick` 分别是 **10** 和 **9**。\n\n#### 更多示例\n\n[这里](https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fblob\u002Fmaster\u002Fexamples)提供了一些示例，展示了如何将 `gym-anytrading` 与其他知名库（如 `Stable-Baselines3` 和 `QuantStats`）结合使用，并说明如何在其他强化学习或交易库中利用我们的交易环境。\n\n### 扩展和操作 TradingEnv\n\n如果您希望在环境之外处理数据并提取特征，可以通过两种简单的方法实现：\n\n**方法 1（推荐）：**\n\n\n```python\ndef my_process_data(env):\n    start = env.frame_bound[0] - env.window_size\n    end = env.frame_bound[1]\n    prices = env.df.loc[:, 'Low'].to_numpy()[start:end]\n    signal_features = env.df.loc[:, ['Close', 'Open', 'High', 'Low']].to_numpy()[start:end]\n    return prices, signal_features\n\n\nclass MyForexEnv(ForexEnv):\n    _process_data = my_process_data\n\n\nenv = MyForexEnv(df=FOREX_EURUSD_1H_ASK, window_size=12, frame_bound=(12, len(FOREX_EURUSD_1H_ASK)))\n```\n\n**方法 2：**\n\n\n```python\ndef my_process_data(df, window_size, frame_bound):\n    start = frame_bound[0] - window_size\n    end = frame_bound[1]\n    prices = df.loc[:, 'Low'].to_numpy()[start:end]\n    signal_features = df.loc[:, ['Close', 'Open', 'High', 'Low']].to_numpy()[start:end]\n    return prices, signal_features\n\n\nclass MyStocksEnv(StocksEnv):\n    \n    def __init__(self, prices, signal_features, **kwargs):\n        self._prices = prices\n        self._signal_features = signal_features\n        super().__init__(**kwargs)\n\n    def _process_data(self):\n        return self._prices, self._signal_features\n\n    \nprices, signal_features = my_process_data(df=STOCKS_GOOGL, window_size=30, frame_bound=(30, len(STOCKS_GOOGL)))\nenv = MyStocksEnv(prices, signal_features, df=STOCKS_GOOGL, window_size=30, frame_bound=(30, len(STOCKS_GOOGL)))\n```\n\n## 相关项目\n\n* 在 [DI-engine](https:\u002F\u002Fgithub.com\u002Fopendilab\u002FDI-engine\u002Ftree\u002Fmain\u002Fdizoo\u002Fgym_anytrading) 项目中，开发了一个更为复杂的 `anytrading` 版本，包含五种动作、三种仓位以及更优的奖励函数。该工具属于中等难度级别（介于 `anytrading` 和 `mtsim` 之间），适合中级用户使用。更多相关信息和文档请参见 [这里](https:\u002F\u002Fgithub.com\u002Fopendilab\u002FDI-engine\u002Ftree\u002Fmain\u002Fdizoo\u002Fgym_anytrading\u002Fenvs)。","# gym-anytrading 快速上手指南\n\n`gym-anytrading` 是一个基于 OpenAI Gym 构建的强化学习交易环境集合，旨在简化外汇（FOREX）和股票（Stock）市场算法的开发与测试流程。它提供了高度抽象且灵活的接口，支持自定义数据源和交易策略。\n\n## 环境准备\n\n在开始之前，请确保您的开发环境满足以下要求：\n\n*   **操作系统**：Linux, macOS, 或 Windows\n*   **Python 版本**：建议 Python 3.8 及以上\n*   **核心依赖**：\n    *   `gymnasium` (或 `gym`)\n    *   `pandas` (用于数据处理)\n    *   `numpy`\n    *   `matplotlib` (用于可视化渲染)\n\n> **提示**：国内用户安装依赖时若遇网络问题，可指定清华或阿里镜像源：\n> `pip install -i https:\u002F\u002Fpypi.tuna.tsinghua.edu.cn\u002Fsimple \u003Cpackage_name>`\n\n## 安装步骤\n\n您可以通过 PyPI 直接安装，也可以从源代码安装以获取最新功能。\n\n### 方式一：通过 PIP 安装（推荐）\n\n```bash\npip install gym-anytrading\n```\n\n### 方式二：从仓库源码安装\n\n```bash\ngit clone https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\ncd gym-anytrading\npip install -e .\n\n# 或者直接使用 zip 包安装\npip install --upgrade --no-deps --force-reinstall https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Farchive\u002Fmaster.zip\n```\n\n## 基本使用\n\n`gym-anytrading` 的核心设计理念是简化动作空间（仅保留 **Buy=1** 和 **Sell=0**）和持仓方向（仅保留 **Long=1** 和 **Short=0**），以便强化学习代理更高效地学习。\n\n### 1. 创建默认环境\n\n库内置了默认的欧元\u002F美元（FOREX）和谷歌股票（Stocks）数据集。\n\n```python\nimport gymnasium as gym\nimport gym_anytrading\n\n# 创建外汇环境\nenv = gym.make('forex-v0')\n\n# 或者创建股票环境\n# env = gym.make('stocks-v0')\n```\n\n### 2. 创建自定义环境\n\n您可以传入自己的 Pandas DataFrame 数据集，并配置时间窗口、数据范围等参数。\n\n```python\nfrom gym_anytrading.datasets import FOREX_EURUSD_1H_ASK\n\n# 使用自定义参数创建环境\ncustom_env = gym.make(\n    'forex-v0',\n    df=FOREX_EURUSD_1H_ASK,       # 传入数据集 (pandas DataFrame)\n    window_size=10,               # 观察窗口大小 (当前及前 N-1 个时刻)\n    frame_bound=(10, 300),        # 数据切片范围 (起始索引必须 >= window_size)\n    unit_side='right'             # 外汇交易基准侧 ('left' 或 'right')\n)\n```\n\n### 3. 运行一个简单的交互循环\n\n以下示例展示了如何重置环境、执行随机动作并渲染结果。\n\n```python\nimport matplotlib.pyplot as plt\n\n# 重置环境\nobservation = env.reset(seed=2023)\n\ndone = False\nwhile not done:\n    # 采样一个随机动作 (0=Sell, 1=Buy)\n    action = env.action_space.sample()\n    \n    # 执行一步\n    observation, reward, terminated, truncated, info = env.step(action)\n    done = terminated or truncated\n\n# 渲染整个交易过程\nplt.cla()\nenv.render_all()\nplt.show()\n\n# 打印最终信息\nprint(\"Info:\", info)\n```\n\n### 关键概念说明\n\n*   **动作空间 (Action Space)**：离散值 `0` (卖出\u002F平仓) 和 `1` (买入\u002F开仓)。库中移除了 \"Hold\" 动作以减少学习复杂度。\n*   **持仓方向 (Positions)**：内部自动管理 `Short` (做空) 和 `Long` (做多) 状态。\n*   **渲染可视化**：\n    *   **红色区域**：代表 Short (做空) 持仓期间。\n    *   **绿色区域**：代表 Long (做多) 持仓期间。\n    *   初始状态默认为 **Short**。\n\n通过以上步骤，您即可快速搭建起一个用于强化学习交易策略训练的基础环境。如需更复杂的奖励函数或手续费逻辑，可通过继承 `TradingEnv` 类进行自定义扩展。","某量化团队正在尝试利用强化学习开发一个自动化的外汇（FOREX）交易策略，希望模型能根据历史 K 线数据自主决定买卖时机。\n\n### 没有 gym-anytrading 时\n- **环境搭建耗时极长**：开发者需从零编写符合 OpenAI Gym 标准的交易环境代码，包括状态空间定义、奖励函数计算及步进逻辑，往往耗费数周时间。\n- **动作空间设计冗余**：初期容易陷入误区，定义“买入、卖出、持有、入场、离场”等复杂动作，导致智能体难以收敛，甚至因“持有”动作被过度规避而学不到有效策略。\n- **头寸管理逻辑混乱**：在处理做多（Long）与做空（Short）的切换时，缺乏标准化的位置状态定义，容易引发逻辑漏洞或回测结果失真。\n- **复用性与扩展性差**：若后续想从外汇市场切换到股票市场测试，必须重新修改大量底层代码，无法快速验证策略在不同市场的泛化能力。\n\n### 使用 gym-anytrading 后\n- **即插即用的高效开发**：直接调用 `ForexEnv` 或 `StocksEnv` 接口，几分钟内即可构建出标准化的训练环境，将精力集中于算法优化而非基建。\n- **动作空间精简高效**：遵循工具推荐的最佳实践，仅保留 `Buy` 和 `Sell` 两个核心动作，大幅降低学习难度，显著缩短模型收敛时间。\n- **头寸状态清晰明确**：内置标准化的 `Long` 和 `Short` 位置逻辑，自动处理复杂的持仓盈亏计算，确保回测数据的准确性与一致性。\n- **灵活的市场切换**：凭借统一的抽象基类 `TradingEnv`，只需更换数据源即可在外汇与股票环境间无缝切换，快速对比策略表现。\n\ngym-anytrading 通过标准化和简化交易环境的复杂性，让开发者能从繁琐的基建工作中解放出来，专注于强化学习策略的核心迭代与验证。","https:\u002F\u002Foss.gittoolsai.com\u002Fimages\u002FAminHP_gym-anytrading_958f99fa.png","AminHP","Mohammad Amin Haghpanah","https:\u002F\u002Foss.gittoolsai.com\u002Favatars\u002FAminHP_915627fc.png",null,"@koala-team","mdan.hagh@gmail.com","https:\u002F\u002Fgithub.com\u002FAminHP",[80],{"name":81,"color":82,"percentage":83},"Python","#3572A5",100,2378,485,"2026-04-08T02:25:50","MIT",1,"","未说明",{"notes":92,"python":90,"dependencies":93},"该工具是基于 OpenAI Gym (现兼容 gymnasium) 的强化学习交易环境库，主要用于外汇和股票市场的算法开发与测试。安装可通过 pip 或源码进行。核心依赖包括用于数据处理的 pandas、数值计算的 numpy 以及用于可视化的 matplotlib。README 中未明确指定具体的操作系统、GPU、内存或 Python 版本要求，通常此类库在标准的 Python 数据科学环境中即可运行。用户需自行准备交易数据（DataFrame 格式），也可使用库内自带的示例数据集。",[94,95,96,97],"gymnasium","pandas","numpy","matplotlib",[14],[100,101,102,103,104,105,106,107,108,109],"openai-gym","reinforcement-learning","q-learning","dqn","trading","trading-environments","forex","stocks","gym-environments","trading-algorithms","2026-03-27T02:49:30.150509","2026-04-11T10:04:15.813736",[113,118,123,128,133,138],{"id":114,"question_zh":115,"answer_zh":116,"source_url":117},29298,"如何使用该项目进行日内交易（DayTrade）训练？如何加载特定时间段的数据？","不需要一次性加载所有数据（例如 1.5 亿行）。建议将整个周期按天拆分，将每一天视为一个独立的 episode（回合）。具体做法是：从 CSV 文件中读取特定日期的起始行，然后选取接下来的 n 行（例如 [9:30, 17:30] 的交易时段）放入环境中。对每一天重复此操作，利用所有天的数据来训练和改进模型。你可以通过重写某些方法来实现，或者简单地按天切割数据。","https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fissues\u002F16",{"id":119,"question_zh":120,"answer_zh":121,"source_url":122},29299,"如何将训练结果导出并与 quantstats 结合生成报告？","维护者计划发布新版本以更好地支持该功能，并提供混合使用 `gym-anytrading`、`stable_baselines` 和 `quantstats` 的示例代码。目前用户可以通过自定义代码提取回报率（returns）变量，将其格式化为 quantstats 期望的输入格式（通常是 Pandas Series），然后调用 `qs.reports.html()` 生成报告。建议查看项目示例文件夹中关于整合这两个库的最新代码示例。","https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fissues\u002F18",{"id":124,"question_zh":125,"answer_zh":126,"source_url":127},29300,"为什么模型显示高奖励（Reward）但实际利润（Profit）却是亏损的？","这通常是对交易执行机制的误解。在源代码中，实际的交易只在持仓状态发生变化时发生（例如从空仓变为持有，或从持有变为空仓）。连续执行多次相同的动作（如连续 100 次“买入”）并不会产生 100 次交易，只有当动作导致状态改变时才会计算盈亏。因此，奖励函数可能基于每一步的潜在收益计算，而实际利润仅由状态切换时的价差决定。","https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fissues\u002F8",{"id":129,"question_zh":130,"answer_zh":131,"source_url":132},29301,"如何实现连续动作空间（例如输出 0 到 1 之间的数值代表投资比例）？","原项目主要设计用于离散动作空间。如果需要连续动作空间（如输出 -1 到 +1 之间的值，其中负数代表卖出信号，正数代表买入信号，绝对值代表净资产的百分比），维护者推荐尝试其新项目 `gym-mtsim` (https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-mtsim)，该项目原生支持此功能。如果必须在此项目中实现，则需要 fork 仓库并修改环境逻辑，根据动作值计算买卖差额，并重新设计回报计算方式（参考相关科学论文中的加法或乘法回报模型）。","https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fissues\u002F11",{"id":134,"question_zh":135,"answer_zh":136,"source_url":137},29302,"升级 gym 和 gym-anytrading 后出现 'Environment stocks doesn't exist' 错误或兼容性问题怎么办？","这通常是由于版本不兼容导致的。如果遇到此类错误，建议尝试回退到旧版本的稳定组合，例如：\n- gym==0.22.0\n- gym-anytrading==1.2.0\n- stable-baselines3==1.1.0\n- torch==1.9.0\n此外，确保安装最新版本的 `stable-baselines3` 也可能解决问题。如果使用了 `gymnasium`，请注意观察接口变化（如 `step` 返回值的结构变化）。对于涉及 GPU 的训练，如果新旧机器硬件驱动不同，可能需要设置环境变量 `os.environ['CUDA_VISIBLE_DEVICES'] = '-1'` 强制使用 CPU 以排除硬件加速带来的兼容性差异。","https:\u002F\u002Fgithub.com\u002FAminHP\u002Fgym-anytrading\u002Fissues\u002F93",{"id":139,"question_zh":140,"answer_zh":141,"source_url":117},29303,"可以在数据框（DataFrame）中添加技术指标作为特征列吗？","可以。用户可以在预处理数据时计算技术指标（如 RSI、MACD 等），并将它们作为额外的列添加到传递给环境的 `signal_features` 中。例如，可以使用 `pandas` 计算指标，转换为 numpy 数组，然后使用 `np.append` 将其拼接到原有的价格特征数组（如 Open, High, Low, Close, Volume）之后，只要确保最终返回的特征数组形状与环境定义的观察空间一致即可。",[143,147,151,155,159,163,167],{"id":144,"version":145,"summary_zh":75,"released_at":146},198096,"v2.0.0","2023-08-27T12:09:06",{"id":148,"version":149,"summary_zh":75,"released_at":150},198097,"v1.3.2","2022-09-11T20:49:47",{"id":152,"version":153,"summary_zh":75,"released_at":154},198098,"v1.3.1","2022-06-25T10:44:55",{"id":156,"version":157,"summary_zh":75,"released_at":158},198099,"v1.3.0","2022-05-21T01:00:15",{"id":160,"version":161,"summary_zh":75,"released_at":162},198100,"v1.2.0","2020-07-31T16:22:39",{"id":164,"version":165,"summary_zh":75,"released_at":166},198101,"v1.1.0","2020-05-26T11:11:35",{"id":168,"version":169,"summary_zh":75,"released_at":170},198102,"v1.0.0","2019-09-22T21:36:01"]