### Install EasyTrader using pip Source: https://easytrader.readthedocs.io/zh-cn/latest/install This command installs the EasyTrader Python package using pip. Ensure you have pip installed and accessible in your system's PATH. This is the standard method for acquiring the library for use in Python projects. ```bash pip install easytrader ``` -------------------------------- ### Start Trading Server (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/remote Starts the EasyTrader trading server on a specified port. This Python script is run on the server that has direct access to the trading software. It listens for incoming connections to receive trading signals. ```python from easytrader import server server.run(port=1430) # 默认端口为 1430 ``` -------------------------------- ### Upgrade EasyTrader using pip Source: https://easytrader.readthedocs.io/zh-cn/latest/install This command upgrades the EasyTrader Python package to the latest version using pip. The -U flag ensures that if the package is already installed, it will be upgraded. This is useful for getting the newest features and bug fixes. ```bash pip install easytrader -U ``` -------------------------------- ### Setting Slippage for Trading Orders (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Sets the slippage tolerance for buy and sell orders. Slippage is the difference between the expected price of a trade and the price at which the trade is actually executed. A 5% slippage is set in this example. ```python follower.follow(***, slippage=0.05) # 设置滑点为 5% ``` -------------------------------- ### Initialize EasyTrader for Xueqiu with Custom Initial Assets Source: https://easytrader.readthedocs.io/zh-cn/latest/xueqiu Demonstrates how to initialize the EasyTrader interface for Xueqiu (xq) and specify custom initial assets for simulation. This is useful when the default conversion of portfolio net worth to initial capital is not suitable. ```python import easytrader # Assuming initial_assets is defined elsewhere # initial_assets = 1000000.0 # Example value trader = easytrader.use('xq', initial_assets=initial_assets) ``` -------------------------------- ### 自动登录客户端 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 对于非同花顺客户端或在通用同花顺客户端手动登录一次后,可以使用 `prepare()` 函数进行自动登录。参数可以通过函数直接传入,或通过配置文件指定。 ```python user.prepare(user='用户名', password='雪球、银河客户端为明文密码', comm_password='华泰通讯密码,其他券商不用') ``` ```python user.prepare('/path/to/your/yh_client.json') # 配置文件路径 ``` -------------------------------- ### 连接同花顺客户端 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 启动并连接同花顺客户端。对于券商专用客户端,需要手动打开并登录后,通过 `connect()` 函数传入客户端的 `xiadan.exe` 路径。通用同花顺客户端在手动登录一次并勾选保存密码后,可使用 `prepare()` 函数自动登录。 ```python user.connect(r'客户端xiadan.exe路径') # 类似 r'C:\htzqzyb2\xiadan.exe' ``` -------------------------------- ### Initialize Follower for Joinquant/Ricequant (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Initializes a follower instance to track strategies from joinquant or ricequant. It requires logging in with the respective platform's username and password. ```python target = 'jq' # joinquant target = 'rq' # ricequant follower = easytrader.follower(target) follower.login(user='rq/jq用户名', password='rq/jq密码') ``` -------------------------------- ### 执行限价买入操作 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.buy` 方法执行限价买入操作,需要指定股票代码、买入价格和数量。 ```python user.buy('600036', price=35.5, amount=100) ``` -------------------------------- ### 执行限价卖出操作 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.sell` 方法执行限价卖出操作,需要指定股票代码、卖出价格和数量。 ```python user.sell('600036', price=36.0, amount=100) ``` -------------------------------- ### 连接 QMT 客户端 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `connect` 方法连接到 QMT 客户端。需要提供 QMT 客户端的安装路径和资金账号。请注意,登录 QMT 客户端时必须勾选极简模式/独立交易模式。 ```python user.connect( miniqmt_path=r"D:\\国金证券QMT交易端\\userdata_mini", # QMT 客户端下的 miniqmt 安装路径 stock_account="你的资金账号", # 资金账号 trader_callback=None, # 默认使用 `easytrader.miniqmt.DefaultXtQuantTraderCallback` ) ``` -------------------------------- ### Initialize Trader for Strategy Tracking (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Initializes a trader instance using EasyTrader and prepares it for use with a configuration file. This is the first step in setting up a trading environment for strategy tracking. ```python xq_user = easytrader.use('xq') xq_user.prepare('xq.json') ``` -------------------------------- ### Initialize Follower for Xueqiu Portfolio Tracking (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Initializes a follower instance for tracking Xueqiu portfolios. It requires logging in using Xueqiu cookies obtained after login. ```python xq_follower = easytrader.follower('xq') xq_follower.login(cookies='雪球 cookies,登陆后获取,获取方式见 https://smalltool.github.io/2016/08/02/cookie/') ``` -------------------------------- ### 获取资金状况 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 通过 `user.balance` 属性获取当前的资金状况,包括总资产、持仓市值、可用资金和冻结资金等信息。 ```python user.balance ``` -------------------------------- ### 执行市价买入操作 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.market_buy` 方法执行市价买入操作,可以指定委托类型(ttype)。 ```python user.market_buy('600036', amount=100, ttype='对手方最优价格委托') ``` -------------------------------- ### 一键打新 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 调用 `user.auto_ipo()` 方法可以实现一键自动申购今日所有可申购的新股。 ```python user.auto_ipo() ``` -------------------------------- ### 获取原始交易对象 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 通过 `user.trader` 和 `user.account` 属性可以获取原始的 QMT 交易对象和账户对象,以便进行更高级的操作。 ```python # 获取 XtQuantTrader 对象 trader = user.trader # 获取 StockAccount 对象 account = user.account ``` -------------------------------- ### 引入 easytrader 库 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 在 Python 脚本中引入 easytrader 库,这是使用其功能的第一步。 ```python import easytrader ``` -------------------------------- ### 执行市价卖出操作 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.market_sell` 方法执行市价卖出操作,可以指定委托类型(ttype)。 ```python user.market_sell('600036', amount=100, ttype='对手方最优价格委托') ``` -------------------------------- ### 初始化 miniqmt 客户端 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 初始化 easytrader 的 miniqmt 客户端。此操作会创建一个客户端实例,为后续的交易操作做准备。 ```python user = easytrader.use('miniqmt') ``` -------------------------------- ### 获取持仓信息 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.position` 属性获取当前的持仓信息,包括证券代码、持仓数量、成本价、市值等。 ```python user.position ``` -------------------------------- ### 自定义临时文件目录 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 当使用 `Xls` 策略保存 `Grid` 数据时,如果系统默认的临时文件目录过长导致保存失败,可以通过 `user.grid_strategy_instance.tmp_folder` 设置一个自定义的目录。 ```python user.grid_strategy_instance.tmp_folder = 'C:\\custom_folder' ``` -------------------------------- ### 解决 Grid 数据拷贝问题 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 当同花顺客户端不允许通过剪切板拷贝 `Grid` 数据时,可以通过设置 `user.grid_strategy = grid_strategies.Xls` 来启用将 `Grid` 数据保存为 Excel 文件再读取的策略。 ```python from easytrader import grid_strategies user.grid_strategy = grid_strategies.Xls ``` -------------------------------- ### 安装 easytrader miniqmt 组件 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 安装 miniqmt 组件需要依赖 `xtquant` 库。使用 pip 命令进行安装,请确保已安装 Python 包管理器 pip。 ```shell pip install easytrader[miniqmt] ``` -------------------------------- ### 引入 EasyTrader 库 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 这是使用 EasyTrader 库的第一步,通过 `import` 语句将库引入到你的 Python 环境中。 ```python import easytrader ``` -------------------------------- ### 开启编辑器文本输入模式 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 当某些客户端无法通过默认方法输入文本时,可以通过 `enable_type_keys_for_editor()` 方法开启通过键盘输入的方式来绕过限制。 ```python user.enable_type_keys_for_editor() ``` -------------------------------- ### 刷新数据 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 通过 `user.refresh()` 方法可以刷新客户端的数据,以获取最新的持仓、资金等信息。 ```python user.refresh() ``` -------------------------------- ### 使用工具栏按钮刷新数据 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 如果通过菜单栏刷新数据较慢,可以配置 `user.refresh_strategy` 为 `refresh_strategies.Toolbar`,并指定刷新按钮在工具栏中的索引(默认为 4),以通过点击工具栏刷新按钮来加速刷新。 ```python from easytrader import refresh_strategies # refresh_btn_index 指的是刷新按钮在工具栏的排序,默认为第四个,根据客户端实际情况调整 user.refresh_strategy = refresh_strategies.Toolbar(refresh_btn_index=4) ``` -------------------------------- ### 查询今日可申购新股 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 使用 `easytrader.utils.stock.get_today_ipo_data()` 函数可以获取当日所有可申购的新股信息,包括股票代码、名称、发行价和申购代码。 ```python from easytrader.utils.stock import get_today_ipo_data get_today_ipo_data() ``` -------------------------------- ### 买入股票 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 使用 `user.buy()` 方法进行股票买入操作,需要指定证券代码、买入价格和买入数量。交易成功后会返回委托序号。 ```python user.buy('162411', price=0.55, amount=100) ``` -------------------------------- ### Python: 自定义交易回调处理 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 通过继承 XtQuantTraderCallback 类来创建自定义交易回调处理。您可以重写 on_disconnected、on_account_status、on_stock_order、on_stock_trade、on_order_error 和 on_cancel_error 方法来处理不同的回调事件。这允许您根据特定需求定制交易逻辑。使用自定义回调时,需要在连接时通过 trader_callback 参数传入自定义类的实例。 ```python from xtquant.xttrader import XtQuantTraderCallback class MyTraderCallback(XtQuantTraderCallback): def on_disconnected(self): print("连接断开") def on_account_status(self, status): print(f"账户状态: {status.account_id}, 状态: {status.status}") def on_stock_order(self, order): print(f"委托回调: {order.stock_code}, 状态: {order.order_status}") def on_stock_trade(self, trade): print(f"成交回调: {trade.stock_code}, 价格: {trade.traded_price}") def on_order_error(self, order_error): print(f"下单失败: {order_error.order_id}, 错误: {order_error.error_msg}") def on_cancel_error(self, cancel_error): print(f"撤单失败: {cancel_error.order_id}, 错误: {cancel_error.error_msg}") # 连接时使用自定义回调 user.connect( miniqmt_path=r"D:\\国金证券QMT交易端\\userdata_mini", stock_account="你的资金账号", trader_callback=MyTraderCallback() ) ``` -------------------------------- ### 卖出股票 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 使用 `user.sell()` 方法进行股票卖出操作,需要指定证券代码、卖出价格和卖出数量。交易成功后会返回委托序号。 ```python user.sell('162411', price=0.55, amount=100) ``` -------------------------------- ### 撤销委托 Source: https://easytrader.readthedocs.io/zh-cn/latest/miniqmt 使用 `user.cancel_entrust` 方法撤销指定的委托单,需要传入之前下单时返回的订单编号。 ```python user.cancel_entrust(123456) # 传入之前买入或卖出时返回的订单编号 ``` -------------------------------- ### 获取资金状况 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 通过 `user.balance` 属性可以获取当前的资金状况,包括可用资金、总资产、股份参考盈亏等信息。 ```python user.balance ``` -------------------------------- ### 退出客户端软件 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 使用 `user.exit()` 方法可以安全地退出 EasyTrader 和其连接的客户端软件。 ```python user.exit() ``` -------------------------------- ### Multi-User and Multi-Strategy Tracking (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Enables tracking multiple strategies across multiple users simultaneously. This requires providing lists of traders, strategies, and their corresponding total assets. ```python follower.follow(users=[xq_user, yh_user], strategies=['组合1', '组合2'], total_assets=[10000, 10000]) ``` -------------------------------- ### 设置同花顺客户端类型 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 根据你使用的同花顺客户端类型,选择相应的 `use()` 方法来初始化交易用户对象。这包括通用同花顺客户端、券商专用同花顺客户端、雪球组合、国金客户端和海通客户端。 ```python user = easytrader.use('universal_client') # 通用同花顺客户端是指同花顺官网提供的客户端软件内的下单程序,内含对多个券商的交易支持,适用于券商不直接提供同花顺客户端时的后备方案。 ``` ```python user = easytrader.use('ths') # 其他券商专用同花顺客户端是指对应券商官网提供的基于同花顺修改的软件版本,类似银河的双子星(同花顺版本),国金证券网上交易独立下单程序(核新PC版)等。 ``` ```python user = easytrader.use('xq') ``` ```python user = easytrader.use('gj_client') ``` ```python user = easytrader.use('htzq_client') ``` ```python user = easytrader.use('ht_client') ``` -------------------------------- ### Connect Follower to Trader for Joinquant (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Connects a joinquant follower to a trader instance and specifies the URL for joinquant's simulated trading. It also allows for extending the trade command expiration and disabling command caching. ```python follower.follow(xq_user, 'jq的模拟交易url') jq_follower.follow(user, '模拟交易url', trade_cmd_expire_seconds=100000000000, cmd_cache=False) ``` -------------------------------- ### Connect Follower to Trader for Ricequant (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Connects a ricequant follower to a trader instance using the provided run_id. This enables tracking of ricequant trading strategies. ```python follower.follow(xq_user, run_id) ``` -------------------------------- ### 获取持仓信息 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 通过 `user.position` 属性可以获取当前的股票持仓信息,包括证券代码、名称、当前持仓数量、参考市值、成本价、盈亏等。 ```python user.position ``` -------------------------------- ### Connect Xueqiu Follower to Trader for Portfolio Tracking (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Connects a Xueqiu follower to a trader instance, specifying the Xueqiu portfolio ID and total assets. This is used for tracking Xueqiu portfolios that use percentage-based adjustments. ```python xq_follower.follow(xq_user, 'xq组合ID,类似ZH123456', total_assets=100000) ``` -------------------------------- ### 雪球组合比例调仓 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 对于雪球组合,可以使用 `user.adjust_weight()` 方法来调整指定股票的目标持仓比例。例如,`user.adjust_weight('000001', 10)` 将平安银行的持仓比例调整到 10%。 ```python user.adjust_weight('股票代码', 目标比例) ``` -------------------------------- ### 配置文件格式示例 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 提供了不同券商客户端的 JSON 配置文件格式示例,包括银河/国金客户端、华泰客户端和雪球组合的配置方法。请使用 Notepad++ 或 Sublime Text 等编辑器编辑。 ```json { "user": "用户名", "password": "明文密码" } ``` ```json { "user": "华泰用户名", "password": "华泰明文密码", "comm_password": "华泰通讯密码" } ``` ```json { "cookies": "雪球 cookies,登陆后获取,获取方式见 https://smalltool.github.io/2016/08/02/cookie/", "portfolio_code": "组合代码(例:ZH818559)", "portfolio_market": "交易市场(例:us 或者 cn 或者 hk)" } ``` -------------------------------- ### Connect to Trading Server (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/remote Connects a quantitative strategy client to a running EasyTrader trading server. This Python script is run on the strategy side, allowing it to send trading commands like buy and sell orders to the server. It requires the server's IP address and port. ```python from easytrader import remoteclient user = remoteclient.use('使用客户端类型,可选 yh_client, ht_client, ths, xq等', host='服务器ip', port='服务器端口,默认为1430') user.buy(......) user.sell(......) ``` -------------------------------- ### 关闭 Debug 日志输出 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 在初始化 EasyTrader 用户时,将 `debug` 参数设置为 `False` 即可关闭 Debug 日志的输出。 ```python user = easytrader.use('yh', debug=False) ``` -------------------------------- ### 撤销委托 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 使用 `user.cancel_entrust()` 方法撤销指定的买入或卖出委托,需要传入委托获取的 `entrust_no`。 ```python user.cancel_entrust('buy/sell 获取的 entrust_no') ``` -------------------------------- ### 查询当日成交记录 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 通过 `user.today_trades` 属性可以查询当日所有已成交的交易记录,包括买卖标志、证券代码、成交价格和数量等。 ```python user.today_trades ``` -------------------------------- ### 查询当日委托记录 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 通过 `user.today_entrusts` 属性可以查询当日所有委托记录,包括委托状态(已成、已撤等)、委托价格和数量等。 ```python user.today_entrusts ``` -------------------------------- ### JSON 配置文件解码错误 Source: https://easytrader.readthedocs.io/zh-cn/latest/usage 如果在编辑 JSON 配置文件时使用了记事本,可能会导致 `JSONDecodeError`。请务必使用 Notepad++ 或 Sublime Text 等专业编辑器进行编辑。 ```python # raise JSONDecodeError("Expecting value", s, err.value) from None # JSONDecodeError: Expecting value ``` -------------------------------- ### Proportionally Rebalance Xueqiu Portfolio Stocks Source: https://easytrader.readthedocs.io/zh-cn/latest/xueqiu Introduces the `adjust_weight` function for performing proportional rebalancing of stocks within a Xueqiu portfolio. This function allows precise adjustment of a stock's weight in the portfolio. ```python import easytrader trader = easytrader.use('xq') # Example: Adjust the weight of a specific stock stock_code = "600000.XSHG" # Example stock code new_weight = 0.1 # Example: set to 10% of portfolio trader.adjust_weight(stock_code=stock_code, weight=new_weight) ``` -------------------------------- ### Market Order Tracking with EasyTrader (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Configures the follower to use market orders for tracking, which is currently supported only for the Galaxy (银河) client. This can help prevent insufficient trading amounts due to delayed order execution. ```python follower.follow(***, entrust_prop='market') ``` -------------------------------- ### Adjusting Order Interval for Strategy Tracking (Python) Source: https://easytrader.readthedocs.io/zh-cn/latest/follow Adjusts the interval between placing buy and sell orders to prevent issues like insufficient funds due to a sell order not being executed in time. The default interval is 0 seconds. ```python follower.follow(***, send_interval=30) # 设置下单间隔为 30 s ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.