Params
Numeric NPoints(5); // 止损点数偏移
Numeric Lots(1); // 交易手数
Numeric TrendPeriod(50); // 趋势过滤周期
Numeric ATRPeriod(14); // ATR周期
Numeric StopLossATR(1.5); // 止损ATR倍数
Numeric TakeProfitATR(2.0); // 止盈ATR倍数
Numeric MinBarsHold(2); // 最小持仓Bar数
Numeric MaxBarsHold(10); // 最大持仓Bar数
Vars
// 基础信号变量(普通变量)
Bool OutsideBar; // 外包线形态
Bool BullishOutSideBar; // 看涨外包信号
Bool BearishOutSideBar; // 看跌外包信号
Bool TrendFilterLong; // 多头趋势过滤
Bool TrendFilterShort; // 空头趋势过滤
Bool VolatilityOK; // 波动率过滤
Bool MomentumOK; // 动量过滤
// 技术指标变量
Numeric TrendMA; // 趋势均线
Numeric ATRValue; // ATR值
Numeric Momentum; // 动量指标
// 交易管理变量
Numeric EntryPrice; // 入场价格
series<Numeric> StopLossPrice; // 止损价格
series<Numeric> TakeProfitPrice; // 止盈价格
series<Numeric> EntryBar; // 入场Bar索引
series<bool> PositionOpened; // 是否已开仓
series<string> PositionType; // 仓位类型
Numeric FloatingProfit; // 浮动盈亏
Numeric MinPoint; // 最小变动价位
// === 简化方法:只对最终开仓信号使用序列变量 + 防重复开仓 ===
Series<Bool> FinalLongSignal; // 最终多头信号(序列变量)
Series<Bool> FinalShortSignal; // 最终空头信号(序列变量)
Series<Numeric> LastTradeBar; // 上次交易Bar(序列变量)
Events
OnInit()
{
// 策略初始化
Commentary("外包线策略 - 简化防闪烁版本");
PositionOpened = False;
PositionType = "";
EntryBar = 0;
MinPoint = MinMove * PriceScale;
// 初始化序列变量
FinalLongSignal = False;
FinalShortSignal = False;
LastTradeBar = -1;
}
OnBar(ArrayRef<Integer> indexs)
{
// 确保有足够的数据
If (CurrentBar < Max(TrendPeriod, ATRPeriod) + 2)
{
Return;
}
// 计算技术指标
TrendMA = Average(Close, TrendPeriod);
ATRValue = AvgTrueRange(ATRPeriod);
// 计算动量指标
Numeric FastMA = Average(Close, 5);
Numeric SlowMA = Average(Close, 10);
Momentum = FastMA - SlowMA;
//Commentary("Momentum:"+text(Momentum));
//Commentary("Momentum:"+text(FastMA - SlowMA));
// 计算基础信号(普通变量)
OutsideBar = (High > High[1]) && (Low < Low[1]);
BullishOutSideBar = OutsideBar && (Close > High[1]);
BearishOutSideBar = OutsideBar && (Close < Low[1]);
TrendFilterLong = Close > TrendMA;
TrendFilterShort = Close < TrendMA;
Numeric AvgATR = Average(ATRValue, 20);
VolatilityOK = ATRValue > AvgATR * 0.7;
MomentumOK = Abs(Momentum) > 0.001;
// 计算最终信号(序列变量)
FinalLongSignal = BullishOutSideBar && TrendFilterLong && VolatilityOK && MomentumOK;
FinalShortSignal = BearishOutSideBar && TrendFilterShort && VolatilityOK && MomentumOK;
// 检查是否需要重置仓位状态
If (MarketPosition == 0 && PositionOpened)
{
Commentary("检测到仓位已平,重置交易状态");
PositionOpened = False;
PositionType = "";
EntryBar = 0;
FloatingProfit = 0;
}
// 开仓逻辑 - 使用防重复开仓机制
If (MarketPosition == 0 && CurrentBar != LastTradeBar)
{
// 多头入场逻辑
If (FinalLongSignal[1])
{
Buy(Lots, Open);
EntryPrice = Open;
StopLossPrice = EntryPrice - StopLossATR * ATRValue;
TakeProfitPrice = EntryPrice + TakeProfitATR * ATRValue;
EntryBar = CurrentBar;
PositionOpened = True;
PositionType = "Long";
LastTradeBar = CurrentBar; // 记录交易Bar
Commentary("====================");
Commentary("多头入场 at Bar: " + Text(CurrentBar));
Commentary("入场价格: " + Text(EntryPrice));
Commentary("止损价格: " + Text(StopLossPrice));
Commentary("止盈价格: " + Text(TakeProfitPrice));
Commentary("====================");
}
// 空头入场逻辑
Else If (FinalShortSignal[1])
{
SellShort(Lots, Open);
EntryPrice = Open;
StopLossPrice = EntryPrice + StopLossATR * ATRValue;
TakeProfitPrice = EntryPrice - TakeProfitATR * ATRValue;
EntryBar = CurrentBar;
PositionOpened = True;
PositionType = "Short";
LastTradeBar = CurrentBar; // 记录交易Bar
Commentary("====================");
Commentary("空头入场 at Bar: " + Text(CurrentBar));
Commentary("入场价格: " + Text(EntryPrice));
Commentary("止损价格: " + Text(StopLossPrice));
Commentary("止盈价格: " + Text(TakeProfitPrice));
Commentary("====================");
}
}
// 仓位管理逻辑
If (MarketPosition != 0 && BarsSinceEntry > 0)
{
// 计算浮动盈亏
If (MarketPosition == 1) // 多单
{
FloatingProfit = (Open - EntryPrice) * Lots;
}
Else If (MarketPosition == -1) // 空单
{
FloatingProfit = (EntryPrice - Open) * Lots;
}
// 止损逻辑 - 使用动态止损
If (MarketPosition == 1 && Low <= StopLossPrice) // 多单止损
{
Sell(Lots, Min(Open, StopLossPrice));
Commentary("*** 多单动态止损出场 ***");
Commentary(" 出场价格: " + Text(StopLossPrice));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
Else If (MarketPosition == -1 && High >= StopLossPrice) // 空单止损
{
BuyToCover(Lots, Max(Open, StopLossPrice));
Commentary("*** 空单动态止损出场 ***");
Commentary(" 出场价格: " + Text(StopLossPrice));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
// 止盈逻辑 - 使用动态止盈
Else If (MarketPosition == 1 && High >= TakeProfitPrice) // 多单止盈
{
Sell(Lots, Min(Open, TakeProfitPrice));
Commentary("*** 多单动态止盈出场 ***");
Commentary(" 出场价格: " + Text(TakeProfitPrice));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
Else If (MarketPosition == -1 && Low <= TakeProfitPrice) // 空单止盈
{
BuyToCover(Lots, Max(Open, TakeProfitPrice));
Commentary("*** 空单动态止盈出场 ***");
Commentary(" 出场价格: " + Text(TakeProfitPrice));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
// 时间止损 - 防止过久持仓
Else If (BarsSinceEntry >= MaxBarsHold)
{
If (MarketPosition == 1)
{
Sell(Lots, Open);
Commentary("*** 多单时间止损出场 ***");
Commentary(" 持仓Bar数: " + Text(BarsSinceEntry));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
Else If (MarketPosition == -1)
{
BuyToCover(Lots, Open);
Commentary("*** 空单时间止损出场 ***");
Commentary(" 持仓Bar数: " + Text(BarsSinceEntry));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
}
}
}
//创建过timer吗?没创建过写这个有什么用?
OnTimer(Integer id, Integer intervalMillsecs)
{
// 定时输出策略状态
Commentary("外包线策略运行状态:");
Commentary(" 运行Bar数: " + Text(CurrentBar));
If (MarketPosition != 0)
{
Commentary(" 当前仓位: " + Text(MarketPosition));
Commentary(" 入场价格: " + Text(EntryPrice));
Commentary(" 动态止损: " + Text(StopLossPrice));
Commentary(" 动态止盈: " + Text(TakeProfitPrice));
Commentary(" 浮动盈亏: " + Text(FloatingProfit));
}
Else
{
Commentary(" 当前仓位: 无持仓");
Commentary(" 最后交易Bar: " + Text(LastTradeBar));
}
}/
//意义不明
OnExit()
{
Commentary("策略运行结束");
}