ThsautoNetCore.Sdk 1.1.0

dotnet add package ThsautoNetCore.Sdk --version 1.1.0
                    
NuGet\Install-Package ThsautoNetCore.Sdk -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ThsautoNetCore.Sdk" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ThsautoNetCore.Sdk" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ThsautoNetCore.Sdk" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ThsautoNetCore.Sdk --version 1.1.0
                    
#r "nuget: ThsautoNetCore.Sdk, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ThsautoNetCore.Sdk@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ThsautoNetCore.Sdk&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ThsautoNetCore.Sdk&version=1.1.0
                    
Install as a Cake Tool

ThsautoNetCore SDK

同花顺自动化交易SDK,提供简单易用的API接口,支持股票买卖、查询、撤单等操作。这是一个功能完整、易于使用的同花顺客户端自动化库,支持OCR验证码识别和配置管理。

功能特性

  • ✅ 股票买入/卖出
  • ✅ 科创板买入/卖出
  • ✅ 账户余额查询
  • ✅ 持仓信息查询
  • ✅ 订单查询(未成交、已成交)
  • ✅ 撤单操作
  • ✅ OCR验证码自动识别
  • ✅ 重试机制保障稳定性
  • ✅ 异步操作支持
  • ✅ 客户端启动与绑定
  • ✅ 配置文件管理
  • ✅ 强类型数据模型

安装

dotnet add package ThsautoNetCore.Sdk

快速开始

1. 基本使用

using ThsautoNetCore.Sdk;
using ThsautoNetCore.Sdk.Extensions;

// 创建SDK实例
var sdk = SdkFactory.CreateDefault();

// 启动同花顺客户端(如果未运行)
if (!sdk.IsThsClientRunning())
{
    Console.WriteLine("同花顺客户端未运行,正在启动...");
    if (!sdk.LaunchThsClient())
    {
        Console.WriteLine("启动失败,请检查配置文件");
        return;
    }
    // 等待客户端启动完成
    await Task.Delay(5000);
}

// 绑定同花顺客户端
if (!sdk.BindClient())
{
    Console.WriteLine("绑定失败,请检查同花顺客户端是否已启动");
    return;
}

// 获取账户余额
var balance = sdk.GetBalance();
if (balance.IsSuccess())
{
    Console.WriteLine($"总资产: {balance.GetTotalAssets():C}");
    Console.WriteLine($"可用资金: {balance.GetAvailableBalance():C}");
}

// 查询持仓
var position = sdk.GetPosition();
if (position.IsSuccess())
{
    Console.WriteLine($"持仓数量: {position.Data.Count}");
}

### 2. 交易操作

```csharp
// 买入股票
var buyResult = sdk.Buy("000001", 100, 10.50); // 买入100股,价格10.50
if (buyResult.IsSuccess())
{
    Console.WriteLine($"买入成功,合同号: {buyResult.EntrustNo}");
}

// 卖出股票
var sellResult = sdk.Sell("000001", 100, 11.00); // 卖出100股,价格11.00
if (sellResult.IsSuccess())
{
    Console.WriteLine($"卖出成功,合同号: {sellResult.EntrustNo}");
}

// 撤单
var cancelResult = sdk.Cancel("合同编号");
if (cancelResult.IsSuccess())
{
    Console.WriteLine("撤单成功");
}

// 查询未成交订单(强类型)
var activeOrders = sdk.GetActiveOrdersList();
foreach (var order in activeOrders)
{
    Console.WriteLine($"订单: {order.StockName} ({order.StockCode}), 合同号: {order.ContractNumber}");
}

// 查询已成交订单(强类型)
var filledTrades = sdk.GetFilledTrades();
foreach (var trade in filledTrades)
{
    Console.WriteLine($"成交: {trade.StockName} ({trade.StockCode}), 数量: {trade.TradeAmount}");
}

// 查询持仓(强类型)
var positions = sdk.GetPositionList();
foreach (var position in positions)
{
    Console.WriteLine($"持仓: {position.StockName} ({position.StockCode}), 数量: {position.CurrentAmount}");
}

### 3. 自定义配置

```csharp
var config = new SdkConfiguration
{
    QueryRetryCount = 5,         // 查询重试次数
    TradeRetryCount = 3,         // 交易重试次数
    OcrRetryCount = 5,           // OCR重试次数
    ThsClientPath = "C:\\htzq\\xiadan.exe",  // 同花顺客户端路径
    ThsWindowTitle = "网上股票交易系统5.0",  // 同花顺窗口标题
    StartupTimeoutSeconds = 30     // 启动超时时间(秒)
};

var sdk = SdkFactory.CreateWithConfiguration(config);

4. 使用交易助手

using ThsautoNetCore.Sdk.Helpers;

var tradingHelper = new TradingHelper(sdk);

// 异步买入
var result = await tradingHelper.QuickBuyAsync("000001", 100, 10.50);

// 检查资金是否充足
bool hasFunds = await tradingHelper.HasSufficientFundsAsync(10.50m, 100);

// 获取特定股票持仓
var position = await tradingHelper.GetStockPositionAsync("000001");

配置说明

属性 默认值 说明
QueryRetryCount 3 查询操作重试次数
TradeRetryCount 2 交易操作重试次数
OcrRetryCount 3 OCR识别重试次数
EnableDebug false 是否启用调试模式
ThsClientPath null 同花顺客户端可执行文件路径
ThsWindowTitle 网上股票交易系统5.0 同花顺客户端窗口标题
StartupTimeoutSeconds 30 客户端启动超时时间(秒)

注意事项

⚠️ 重要提醒

  • 此SDK通过模拟键盘鼠标操作与同花顺客户端交互,请确保同花顺客户端正常运行
  • 仅支持"网上股票交易系统5.0"版本
  • 所有操作会自动按顺序执行,SDK内部已实现同步机制,无需担心并发问题
  • 交易有风险,请在充分测试后再用于实盘操作
  • 建议在非交易时间进行开发测试
  • 请遵守相关法律法规,合规使用

支持

如果您遇到问题或有建议,请提交Issue或Pull Request。

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 94 1/24/2026
1.0.0 101 1/24/2026