//------------------------------------------------------------------------
// 简称: DualMA_Pro
// 名称: 增强型双均线交易系统
// 类别: 策略应用
// 类型: 内建应用
//------------------------------------------------------------------------
Params
Numeric FastLength(5); // 短期EMA周期
Numeric SlowLength(20); // 长期EMA周期
Numeric ATRPeriod(14); // ATR周期
Numeric RiskPerTrade(0.005); // 单笔交易风险比例 (0.5%)
Numeric MaxLeverage(2); // 最大杠杆倍数
Bool UseVolatilityFilter(True); // 启用波动率过滤器
Vars
Series<Numeric> EMA_Fast; // 快速EMA
Series<Numeric> EMA_Slow; // 慢速EMA
Series<Numeric> ATR; // 真实波动幅度
Series<Numeric> PositionSize; // 动态仓位
Numeric EntryPrice; // 入场价格
Numeric DailyMaxLoss(0.015); // 单日最大损失(1.5%)
Numeric DailyLoss; // 当日累计亏损
Bool TradingEnabled(True); // 交易开关
Events
OnReady()
{
SetBackBarMaxCount(Max(FastLength, SlowLength) + ATRPeriod);
return 0;
}
OnBar(ArrayRef<Integer> indexs)
{
// 重置每日风险指标
If(Date != Date[1])
{
DailyLoss = 0;
TradingEnabled = True;
}
// 计算指标
EMA_Fast = XAverage(Close, FastLength);
EMA_Slow = XAverage(Close, SlowLength);
ATR = AvgTrueRange(ATRPeriod);
// 波动率过滤条件
Numeric VolatilityRatio = ATR/Close;
If(UseVolatilityFilter && VolatilityRatio > 0.05)
{
Commentary("波动率过高,暂停交易");
Return;
}
// 动态仓位计算 (凯利公式改良版)
Numeric RiskCapital = Portfolio_CurrentCapital() * RiskPerTrade;
PositionSize = RiskCapital / (ATR[1] * ContractUnit() * BigPointValue());
PositionSize = Min(PositionSize, MaxLeverage * Portfolio_CurrentCapital()/Close);
// 信号生成
If(TradingEnabled)
{
// 多头入场
If(EMA_Fast.CrossOver(EMA_Slow))
{
EntryPrice = Open;
Buy(PositionSize, EntryPrice);
}
// 空头入场
Else If(EMA_Fast.CrossUnder(EMA_Slow))
{
EntryPrice = Open;
SellShort(PositionSize, EntryPrice);
}
}
// 多头仓位管理
If(MarketPosition == 1)
{
// 动态追踪止损
Numeric TrailingStop = Highest(High, 3) - 1.5*ATR[1];
Numeric HardStop = EntryPrice * (1 - 2*RiskPerTrade);
Numeric StopLevel = Max(TrailingStop, HardStop);
// 分阶段止盈
If(High >= EntryPrice * 1.03)
{
Sell(Floor(PositionSize*0.5), Open); // 盈利3%平半仓
}
If(Low <= StopLevel)
{
Sell(0, Min(Open, StopLevel)); // 全平
}
}
// 空头仓位管理
If(MarketPosition == -1)
{
// 动态追踪止损
Numeric TrailingStop = Lowest(Low, 3) + 1.5*ATR[1];
Numeric HardStop = EntryPrice * (1 + 2*RiskPerTrade);
Numeric StopLevel = Min(TrailingStop, HardStop);
// 分阶段止盈
If(Low <= EntryPrice * 0.97)
{
BuyToCover(Floor(PositionSize*0.5), Open); // 盈利3%平半仓
}
If(High >= StopLevel)
{
BuyToCover(0, Max(Open, StopLevel)); // 全平
}
}
// 账户级风控
Numeric CurrentLoss = Portfolio_CurrentDayProfit();
If(CurrentLoss < -DailyMaxLoss*Portfolio_CurrentCapital())
{
Commentary("触发单日最大亏损,停止交易");
Sell(0, Open);
BuyToCover(0, Open);
TradingEnabled = False;
}
return 0;
}
//------------------------------------------------------------------------
// 编译版本 GS2023.08
// 版权声明 改进版本保留所有权利
//------------------------------------------------------------------------
代码编译出现以下错误:
级别,描述,行号,错误号,策略类型,名称,作者
警告,FOR,WHILE,IF,ELSE中包含序列函数,可能存在潜在的逻辑错误。请确认代码无误,80,2002,用户函数,DualMA_atr,sparc8
警告,FOR,WHILE,IF,ELSE中包含序列函数,可能存在潜在的逻辑错误。请确认代码无误,99,2002,用户函数,DualMA_atr,sparc8
错误,缺少RETURN语句,51,1012,用户函数,DualMA_atr,sparc8
请各位大侠帮忙排除,谢谢!
你这不会又是ai写的吧
用不了的,这语法乱七八糟基本就是瞎写
你好,deepseek写的,你们有pdf的文档吗?让它学习一下,谢谢!!
目前没有离线文件