Params
Numeric lots1(1);
Numeric StopLossPct(0.015); // 止损比例 1.5%
Numeric TakeProfitPct(0.03); // 止盈比例 3.0% (盈亏比2.0)
Numeric FilterPct(0.01); // 放宽过滤条件 1.0%
Numeric MinVolatility(0.5); // 降低最小波动率 0.5%
Numeric BandWidth(0.02); // 放宽布林带宽度 2.0%
Vars
Series<Numeric> AvgValue1;
Series<Numeric> AvgValue2;
Series<Numeric> AvgValue;
Series<Numeric> A4;
Series<Numeric> A5;
Series<Bool> A8;
Series<Bool> A9;
Series<Bool> LBQ1(False);
Series<Bool> TrendFilter(False);
Series<Numeric> RSValue;
Series<Numeric> Momentum;
Numeric myEntryPrice;
Numeric myExitPrice;
Series<Numeric> StopLossPrice;
Series<Numeric> TakeProfitPrice;
Series<Numeric> PriceChange;
Series<Numeric> Volatility;
Numeric i;
Numeric UpSum;
Numeric DownSum;
Series<Numeric> MA_Distance;
// 简化波段指标
Series<Numeric> BollingerUpper;
Series<Numeric> BollingerLower;
Series<Numeric> BollingerMiddle;
Series<Numeric> KDJ_K;
Series<Numeric> KDJ_D;
// 简单的开仓标记
Bool LongOpened(False); // 多头开仓标记
Bool ShortOpened(False); // 空头开仓标记
Events
onBar(ArrayRef<Integer> indexs)
{
// 日期过滤条件
If(date >= 20990321) return;
If(date <= 20200131) return;
// 简化技术指标
AvgValue1 = AverageFC(Close, 10); // 短期均线
AvgValue2 = AverageFC(Close, 30); // 中期均线
AvgValue = AverageFC(Close, 60); // 长期均线
// 简化趋势过滤
Numeric FastMA = AverageFC(Close, 5);
Numeric SlowMA = AverageFC(Close, 20);
TrendFilter = FastMA > SlowMA;
MA_Distance = (FastMA - SlowMA) / SlowMA * 100;
// 简化布林带
BollingerMiddle = AverageFC(Close, 20);
Numeric StdDev = StandardDev(Close, 20, 1);
BollingerUpper = BollingerMiddle + BandWidth * StdDev;
BollingerLower = BollingerMiddle - BandWidth * StdDev;
// 简化KDJ计算
Numeric LowestLow = LowestFC(Low, 9);
Numeric HighestHigh = HighestFC(High, 9);
Numeric RSV = 0;
If(HighestHigh != LowestLow)
{
RSV = (Close - LowestLow) / (HighestHigh - LowestLow) * 100;
}
KDJ_K = (RSV + 2 * KDJ_K[1]) / 3;
KDJ_D = (KDJ_K + 2 * KDJ_D[1]) / 3;
// 简化动量指标
Momentum = (Close - Close[5]) / Close[5] * 100;
// 简化相对强度计算
UpSum = 0;
DownSum = 0;
For i = 1 To 10
{
If(Close[i] > Close[i+1])
{
UpSum = UpSum + (Close[i] - Close[i+1]);
}
Else
{
DownSum = DownSum + (Close[i+1] - Close[i]);
}
}
If(DownSum != 0)
{
RSValue = UpSum / DownSum * 100;
}
Else
{
RSValue = 100;
}
// 简化价格变化计算
PriceChange = (Close - Close[1]) / Close[1] * 100;
Volatility = (HighestFC(High, 10) - LowestFC(Low, 10)) / Close * 100;
// 放宽波动性条件
LBQ1 = Abs(Close - Close[10]) > Close * 0.01; // 10周期1%波动
// 简化高低点计算
A4 = HighestFC(High, 15);
A5 = LowestFC(Low, 15);
A8 = Close >= A4 * (1 - FilterPct);
A9 = Close <= A5 * (1 + FilterPct);
// ============ 多头交易逻辑 ============
// 平多仓条件(优先处理)
If(MarketPosition == 1)
{
If(Low <= StopLossPrice)
{
myExitPrice = Min(Open, StopLossPrice);
Sell(0, myExitPrice);
LongOpened = False; // 平仓后重置标记
PlotString("StopLoss", "SL", High + 5, Red);
Commentary("执行多头止损, 价格=" + Text(myExitPrice));
Return;
}
Else If(High >= TakeProfitPrice)
{
myExitPrice = Max(Open, TakeProfitPrice);
Sell(0, myExitPrice);
LongOpened = False; // 平仓后重置标记
PlotString("TakeProfit", "TP", High + 8, Green);
Commentary("执行多头止盈, 价格=" + Text(myExitPrice));
Return;
}
// 技术退出
Else If((Close < BollingerMiddle && KDJ_K < 40) ||
(Close < FastMA && TrendFilter == False))
{
myExitPrice = Open;
Sell(0, myExitPrice);
LongOpened = False; // 平仓后重置标记
PlotString("TechExit", "EX", High + 6, Blue);
Commentary("执行多头技术退出, 价格=" + Text(myExitPrice));
Return;
}
}
// 开多仓条件
If(MarketPosition == 0 && !LongOpened)
{
If((A8 || Close > BollingerUpper) &&
LBQ1 &&
(Close > AvgValue || TrendFilter) &&
RSValue > 20 &&
Momentum > -2 &&
PriceChange > -0.5 &&
Volatility > MinVolatility &&
KDJ_K > 15 && KDJ_K < 90)
{
myEntryPrice = Max(Open, Close[1]);
Buy(lots1, myEntryPrice);
StopLossPrice = myEntryPrice * (1 - StopLossPct);
TakeProfitPrice = myEntryPrice * (1 + TakeProfitPct);
LongOpened = True; // 开仓后设置标记
ShortOpened = False; // 确保空头标记重置
PlotString("BuySignal", "B", Low - 10, Green);
Commentary("执行开多仓, 价格=" + Text(myEntryPrice));
}
}
// ============ 空头交易逻辑 ============
// 平空仓条件(优先处理)
If(MarketPosition == -1)
{
If(High >= StopLossPrice)
{
myExitPrice = Max(Open, StopLossPrice);
BuyToCover(0, myExitPrice);
ShortOpened = False; // 平仓后重置标记
PlotString("StopLoss", "SL", Low - 5, Red);
Commentary("执行空头止损, 价格=" + Text(myExitPrice));
Return;
}
Else If(Low <= TakeProfitPrice)
{
myExitPrice = Min(Open, TakeProfitPrice);
BuyToCover(0, myExitPrice);
ShortOpened = False; // 平仓后重置标记
PlotString("TakeProfit", "TP", Low - 8, Green);
Commentary("执行空头止盈, 价格=" + Text(myExitPrice));
Return;
}
// 技术退出
Else If((Close > BollingerMiddle && KDJ_K > 60) ||
(Close > FastMA && TrendFilter))
{
myExitPrice = Open;
BuyToCover(0, myExitPrice);
ShortOpened = False; // 平仓后重置标记
PlotString("TechExit", "EX", Low - 6, Blue);
Commentary("执行空头技术退出, 价格=" + Text(myExitPrice));
Return;
}
}
// 开空仓条件
If(MarketPosition == 0 && !ShortOpened)
{
If((A9 || Close < BollingerLower) &&
LBQ1 &&
(Close < AvgValue || TrendFilter == False) &&
RSValue < 80 &&
Momentum < 2 &&
PriceChange < 0.5 &&
Volatility > MinVolatility &&
KDJ_K > 10 && KDJ_K < 85)
{
myEntryPrice = Min(Open, Close[1]);
SellShort(lots1, myEntryPrice);
StopLossPrice = myEntryPrice * (1 + StopLossPct);
TakeProfitPrice = myEntryPrice * (1 - TakeProfitPct);
ShortOpened = True; // 开仓后设置标记
LongOpened = False; // 确保多头标记重置
PlotString("SellSignal", "S", High + 10, Red);
Commentary("执行开空仓, 价格=" + Text(myEntryPrice));
}
}
// 绘制关键指标
PlotNumeric("Bollinger Upper", BollingerUpper, 0, LightGray);
PlotNumeric("Bollinger Lower", BollingerLower, 0, LightGray);
}