实现策略引擎核心功能,包括三种策略计算器和相关DTO定义: 1. 添加双均线策略(ma_trend)计算器 2. 添加吊灯止损策略(chandelier_exit)计算器 3. 添加风险平价策略(risk_parity)计算器 4. 定义策略类型常量类和策略配置DTO 5. 实现策略引擎服务接口和扩展方法 6. 更新项目引用和README文档
55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using AssetManager.Data;
|
|
using AssetManager.Models.DTOs;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AssetManager.Infrastructure.StrategyEngine.Calculators;
|
|
|
|
/// <summary>
|
|
/// 均线趋势策略计算器
|
|
/// </summary>
|
|
public class MaTrendCalculator : IStrategyCalculator
|
|
{
|
|
private readonly ILogger<MaTrendCalculator> _logger;
|
|
|
|
public string StrategyType => AssetManager.Models.DTOs.StrategyType.MaTrend;
|
|
|
|
public MaTrendCalculator(ILogger<MaTrendCalculator> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<StrategySignal> CalculateAsync(
|
|
string configJson,
|
|
List<Position> positions,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
_logger.LogInformation("计算均线趋势策略信号");
|
|
|
|
var config = System.Text.Json.JsonSerializer.Deserialize<MaTrendConfig>(configJson, new System.Text.Json.JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
}) ?? new MaTrendConfig();
|
|
|
|
if (positions.Count == 0)
|
|
{
|
|
return new StrategySignal
|
|
{
|
|
StrategyType = StrategyType,
|
|
Signal = "HOLD",
|
|
Reason = "无持仓",
|
|
GeneratedAt = DateTime.UtcNow
|
|
};
|
|
}
|
|
|
|
// 简单实现:返回持有信号
|
|
await Task.Delay(1, cancellationToken);
|
|
|
|
return new StrategySignal
|
|
{
|
|
StrategyType = StrategyType,
|
|
Signal = "HOLD",
|
|
Reason = "均线趋势策略暂未实现",
|
|
GeneratedAt = DateTime.UtcNow
|
|
};
|
|
}
|
|
} |