//------------------------------------------------------------------------
// 简称: TurtleTrader_CN
// 名称: 海龟交易系统-A股版(50万资金优化)
// 类别: 策略应用
// 类型: 内建应用
//------------------------------------------------------------------------
Params
// === 海龟核心参数 ===
Numeric nEntries(1); // 最大加仓次数(只加仓1次)
Numeric RiskRatio(1.5); // 风险比例(单笔1.5%总资金风险)
Numeric ATRLength(20); // ATR计算周期
Numeric boLength(20); // 短期突破周期
Numeric fsLength(50); // 长期突破周期
Numeric teLength(15); // 离市周期
Bool LastProfitableTradeFilter(False); // 入市过滤条件
// === A股特殊参数 ===
Numeric MaxPositionPercent(25); // 最大持仓比例
Numeric StopLossPercent(8); // 最大止损比例
// === 成交量过滤参数 ===
Numeric VolumeMaPeriod(30); // 成交量均线周期
Numeric VolumeThreshold(1.5); // 成交量阈值
Bool UseVolumeFilter(True); // 启用成交量过滤
Vars
// === 基础变量 ===
Numeric MinPoint; // 最小变动单位
Series<Numeric> TrueRange; // 真实波幅
Series<Numeric> AvgTR; // 平均真实波幅
Numeric N; // N值
Numeric TotalEquity; // 总资产
Numeric InitialUnits; // 初始开仓股数
Numeric AddUnits; // 加仓股数
Numeric MaxPositionValue; // 最大持仓市值
// === 通道指标 ===
Series<Numeric> DonchianHi; // 20日唐奇安通道上轨
Series<Numeric> DonchianLo; // 20日唐奇安通道下轨
Series<Numeric> fsDonchianHi; // 50日唐奇安通道上轨
Series<Numeric> fsDonchianLo; // 50日唐奇安通道下轨
// === 离市指标 ===
Numeric ExitHighestPrice; // 空头离市参考价
Numeric ExitLowestPrice; // 多头离市参考价
// === 成交量指标 ===
Series<Numeric> VolumeMA; // 成交量均线
Bool VolumeCondition; // 成交量条件
Series<Numeric> CurrentVolume; // 当前成交量
// === 交易执行变量 ===
Numeric myEntryPrice; // 开仓价格
Numeric myExitPrice; // 平仓价格
Bool SendOrderThisBar(False); // 当前K线是否已有交易
Series<Numeric> preEntryPrice(0); // 前次开仓价格
Series<Bool> PreBreakoutFailure(false); // 前次突破是否失败
// === A股特殊变量 ===
Numeric PositionValue; // 当前持仓市值
Bool CapitalCondition; // 资金条件
Events
OnBar(ArrayRef<Integer> indexs)
{
// ========== 1. 初始化 ==========
If(BarStatus == 0)
{
preEntryPrice = InvalidNumeric;
PreBreakoutFailure = false;
}
// ========== 2. A股基础设置 ==========
MinPoint = 0.01;
TotalEquity = Portfolio_CurrentCapital() + Portfolio_UsedMargin();
// ========== 3. 资金风险管理 ==========
MaxPositionValue = TotalEquity * MaxPositionPercent / 100; // 单票最大持仓
CapitalCondition = (TotalEquity >= 300000); // 资金低于30万停止开仓
// ========== 4. 技术指标计算 ==========
// 计算TrueRange
TrueRange = Max(Max(High - Low, Abs(High - Close[1])), Abs(Low - Close[1]));
AvgTR = XAverage(TrueRange, ATRLength);
N = AvgTR[1]; // 使用前一根K线的ATR
// 通道计算(使用前一根K线数据,避免未来函数)
DonchianHi = HighestFC(High[1], boLength);
DonchianLo = LowestFC(Low[1], boLength);
fsDonchianHi = HighestFC(High[1], fsLength);
fsDonchianLo = LowestFC(Low[1], fsLength);
// 离市通道
ExitLowestPrice = LowestFC(Low[1], teLength);
ExitHighestPrice = HighestFC(High[1], teLength);
// ========== 5. 成交量过滤条件 ==========
CurrentVolume = Vol(); // 使用Vol()函数获取成交量
VolumeMA = Average(CurrentVolume, VolumeMaPeriod);
If(UseVolumeFilter)
{
// 成交量条件:当前成交量 > 成交量均线 × 阈值
VolumeCondition = CurrentVolume > VolumeMA * VolumeThreshold;
}
Else
{
VolumeCondition = True;
}
// ========== 6. 头寸规模计算(按股数直接计算) ==========
If(N > 0 && CapitalCondition)
{
// 计算初始开仓股数(基于1.5%总资金风险)
Numeric RiskAmount = TotalEquity * RiskRatio / 100; // 单笔风险金额
Numeric TheoreticalShares = RiskAmount / N; // 理论股数
// 初始开仓股数:直接取整,向下取整到100股倍数
InitialUnits = IntPart(TheoreticalShares); // 直接取整
InitialUnits = Max(InitialUnits, 100); // 最少100股
InitialUnits = IntPart(InitialUnits / 100) * 100; // 转换为100股倍数
// 市值控制:单票最大持仓市值限制
Numeric MaxSharesByValue = IntPart(MaxPositionValue / Close / 100) * 100;
InitialUnits = Min(InitialUnits, MaxSharesByValue);
// 加仓股数 = 初始股数 × 1/2,四舍五入到100股整数倍
Numeric RawAddUnits = InitialUnits * 0.5;
AddUnits = IntPart((RawAddUnits + 50) / 100) * 100; // 四舍五入
If(AddUnits < 100) AddUnits = 100; // 至少100股
Commentary("仓位计算: 风险金额=" + Text(RiskAmount, 0) + "元, ATR=" + Text(N, 3));
Commentary("理论股数=" + Text(TheoreticalShares, 0) + "股, 初始=" + Text(InitialUnits) + "股, 加仓=" + Text(AddUnits) + "股");
}
Else
{
InitialUnits = 0;
AddUnits = 0;
Commentary("仓位计算: 条件不满足,无法开仓");
}
// ========== 7. 综合交易条件 ==========
Bool TradeCondition = CapitalCondition && VolumeCondition && (InitialUnits >= 100);
// ========== 8. 调试信息 ==========
Commentary("=== 50万资金海龟系统 - 1.5%风险版 ===");
Commentary("总资金:" + Text(TotalEquity, 0) + "元, 单笔风险:" + Text(TotalEquity * RiskRatio / 100, 0) + "元");
Commentary("ATR值:" + Text(N, 3) + "元, 20日通道:" + Text(DonchianHi, 2) + "/" + Text(DonchianLo, 2));
Commentary("成交量:" + Text(CurrentVolume, 0) + ", 均量:" + Text(VolumeMA, 0) + ", 量能条件:" + IIFString(VolumeCondition, "满足", "不满足"));
SendOrderThisBar = False;
// ========== 9. 入场逻辑(A股只能做多) ==========
// 9.1 20日突破系统(主要入场信号)
If(MarketPosition == 0 && TradeCondition && High > DonchianHi)
{
myEntryPrice = Max(Open, DonchianHi + MinPoint); // 突破价买入
myEntryPrice = Min(myEntryPrice, High); // 不超过当日最高价
// A股价格限制检查
If(myEntryPrice <= Close[1] * 1.10 && myEntryPrice >= Close[1] * 0.90)
{
preEntryPrice = myEntryPrice;
Buy(InitialUnits, myEntryPrice);
SendOrderThisBar = True;
PreBreakoutFailure = False;
Commentary("=== 20日突破买入 " + Text(InitialUnits) + "股,价格:" + Text(myEntryPrice, 2) + " ===");
Commentary("投入资金:" + Text(InitialUnits * myEntryPrice, 0) + "元");
}
}
// 9.2 50日突破系统(备用入场信号)
If(MarketPosition == 0 && TradeCondition && !SendOrderThisBar && High > fsDonchianHi)
{
myEntryPrice = Max(Open, fsDonchianHi + MinPoint);
myEntryPrice = Min(myEntryPrice, High);
If(myEntryPrice <= Close[1] * 1.10 && myEntryPrice >= Close[1] * 0.90)
{
preEntryPrice = myEntryPrice;
Buy(InitialUnits, myEntryPrice);
SendOrderThisBar = True;
PreBreakoutFailure = False;
Commentary("=== 50日突破买入 " + Text(InitialUnits) + "股,价格:" + Text(myEntryPrice, 2) + " ===");
Commentary("投入资金:" + Text(InitialUnits * myEntryPrice, 0) + "元");
}
}
// ========== 10. 持仓管理(A股只能做多) ==========
If(MarketPosition == 1)
{
// 计算当前持仓信息
PositionValue = AvgEntryPrice * CurrentContracts;
Numeric FloatingProfit = (Close - AvgEntryPrice) * CurrentContracts;
Numeric ProfitPercent = (Close - AvgEntryPrice) / AvgEntryPrice * 100;
Numeric CurrentLossAmount = 0;
Numeric LossRatio = 0;
If(FloatingProfit < 0)
{
CurrentLossAmount = Abs(FloatingProfit);
LossRatio = CurrentLossAmount / TotalEquity * 100;
}
Commentary("持仓状态: " + Text(CurrentContracts) + "股, 成本:" + Text(AvgEntryPrice, 2));
Commentary("浮动盈亏: " + Text(FloatingProfit, 0) + "元 (" + Text(ProfitPercent, 1) + "%)");
If(FloatingProfit < 0)
{
Commentary("当前亏损:" + Text(CurrentLossAmount, 0) + "元,占资金:" + Text(LossRatio, 2) + "%,止损线:" + Text(RiskRatio, 1) + "%");
}
// ========== 10.1 资金亏损比例止损(最高优先级) ==========
If(!SendOrderThisBar && FloatingProfit < 0)
{
// 资金亏损比例止损:亏损金额达到总资金的RiskRatio%就必须平仓
If(LossRatio >= RiskRatio)
{
myExitPrice = Max(Open, Close);
myExitPrice = Min(myExitPrice, Low);
If(myExitPrice >= Close[1] * 0.90)
{
Sell(0, myExitPrice);
Commentary("*** 资金止损!亏损" + Text(CurrentLossAmount, 0) + "元,达到总资金" + Text(LossRatio, 2) + "%,超过设定值" + Text(RiskRatio, 1) + "% ***");
Return;
}
}
}
// ========== 10.2 离市条件:价格跌破15日最低价(跟踪止损) ==========
If(!SendOrderThisBar && Low < ExitLowestPrice)
{
myExitPrice = Max(Open, ExitLowestPrice - MinPoint);
myExitPrice = Min(myExitPrice, Low);
If(myExitPrice >= Close[1] * 0.90)
{
Sell(0, myExitPrice);
Numeric ExitProfit = (myExitPrice - AvgEntryPrice) * CurrentContracts;
Commentary("*** 跟踪止损卖出,价格:" + Text(myExitPrice, 2) + ",盈亏:" + Text(ExitProfit, 0) + "元 ***");
Return;
}
}
// ========== 10.3 加仓逻辑(只加仓1次,使用AddUnits) ==========
If(!SendOrderThisBar && preEntryPrice != InvalidNumeric && AddUnits >= 100 && CurrentEntries < nEntries)
{
// 加仓条件:价格上涨0.5N
If(High >= preEntryPrice + 0.5 * N && CurrentEntries < nEntries)
{
myEntryPrice = preEntryPrice + 0.5 * N;
myEntryPrice = Min(myEntryPrice, High);
If(myEntryPrice <= Close[1] * 1.10)
{
preEntryPrice = myEntryPrice;
If(Buy(AddUnits, myEntryPrice))
{
SendOrderThisBar = True;
Commentary("=== 加仓 " + Text(AddUnits) + "股,价格:" + Text(myEntryPrice, 2) + " ===");
Commentary("总持仓:" + Text(CurrentContracts) + "股,总市值:" + Text(CurrentContracts * Close, 0) + "元");
}
}
}
}
// ========== 10.4 价格止损条件:价格跌破开仓价-2N 或 最大止损比例 ==========
If(!SendOrderThisBar)
{
Numeric StopLossPrice = preEntryPrice - 2 * N;
Numeric PercentStopPrice = preEntryPrice * (1 - StopLossPercent / 100);
Numeric FinalStopPrice = Max(StopLossPrice, PercentStopPrice);
If(Low <= FinalStopPrice)
{
myExitPrice = Max(Open, FinalStopPrice);
myExitPrice = Min(myExitPrice, Low);
If(myExitPrice >= Close[1] * 0.90)
{
Sell(0, myExitPrice);
PreBreakoutFailure = True;
Numeric LossPercent = (preEntryPrice - myExitPrice) / preEntryPrice * 100;
Numeric LossAmount = (preEntryPrice - myExitPrice) * CurrentContracts;
Commentary("*** 价格止损卖出,价格:" + Text(myExitPrice, 2) + ",亏损:" + Text(LossPercent, 1) + "% (" + Text(LossAmount, 0) + "元) ***");
}
}
}
}
// ========== 11. 最终状态输出 ==========
Commentary("系统状态: 加仓次数=" + Text(CurrentEntries) +
", 总持仓=" + Text(CurrentContracts) + "股" +
", 风险暴露=" + Text(CurrentContracts * Close / TotalEquity * 100, 1) + "%");
Commentary("=================================");
}
由于本人不太会代码,求帮帮我解决闪烁的问题,以及看下我这个策略还有没有其它问题,谢谢管理员
现在连不会程序的都可以做量化了么...
😅