// OBV背离交易策略
// 适用平台:开拓者(TB)
// 周期:15分钟
Params
Numeric FastMAPeriod(12); // 快速均线周期
Numeric SlowMAPeriod(26); // 慢速均线周期
Numeric OBVLookBack(20); // OBV回溯周期
Numeric ATRPeriod(14); // ATR周期
Numeric StopLossATRMultiple(2); // 止损ATR倍数
Numeric TakeProfitATRMultiple(3); // 止盈ATR倍数
Numeric TradeVolume(1); // 交易手数
Vars
Series<Numeric> OBVValue; // OBV值
Numeric OBVHigh; // OBV高点
Numeric OBVLow; // OBV低点
Numeric PriceHigh; // 价格高点
Numeric PriceLow; // 价格低点
Series<Numeric> ATRVal; // ATR值
Series<Numeric> FastMA; // 快速均线
Series<Numeric> SlowMA; // 慢速均线
Numeric ShortStopPrice; // 空头止损价
Numeric ShortTargetPrice; // 空头止盈价
Numeric LongStopPrice; // 多头止损价
Numeric LongTargetPrice; // 多头止盈价
Bool Condition_TopDivergence; // 顶背离条件
Bool Condition_BottomDivergence; // 底背离条件
Series<Bool> TrendUp; // 趋势向上(改为序列变量)
Series<Bool> TrendDown; // 趋势向下(改为序列变量)
Numeric MA_Diff; // 均线差值
Events
OnBar(ArrayRef<Integer> indexs)
{
// 计算技术指标
FastMA = Average(Close, FastMAPeriod);
SlowMA = Average(Close, SlowMAPeriod);
ATRVal = AvgTrueRange(ATRPeriod);
// 确定趋势方向
TrendUp = FastMA > SlowMA;
TrendDown = FastMA < SlowMA;
MA_Diff = Abs(FastMA - SlowMA);
// 计算OBV
If (CurrentBar == 0)
{
OBVValue = Vol;
} Else
{
If (Close > Close[1])
{
OBVValue = OBVValue[1] + Vol;
} Else If (Close < Close[1])
{
OBVValue = OBVValue[1] - Vol;
} Else
{
OBVValue = OBVValue[1];
}
}
// 寻找价格和OBV的高低点
If (CurrentBar >= OBVLookBack)
{
PriceHigh = Highest(High, OBVLookBack);
PriceLow = Lowest(Low, OBVLookBack);
OBVHigh = Highest(OBVValue, OBVLookBack);
OBVLow = Lowest(OBVValue, OBVLookBack);
// 检测顶背离条件
Condition_TopDivergence = (High[1] >= PriceHigh) And
(OBVValue[1] < OBVHigh) And
(High[1] > High[2]) And
(OBVValue[1] < OBVValue[2]);
// 检测底背离条件
Condition_BottomDivergence = (Low[1] <= PriceLow) And
(OBVValue[1] > OBVLow) And
(Low[1] < Low[2]) And
(OBVValue[1] > OBVValue[2]);
} Else
{
Condition_TopDivergence = False;
Condition_BottomDivergence = False;
}
// 空头入场:顶背离 + 趋势向下/盘整 + 看跌K线确认
If (Condition_TopDivergence And
(TrendDown[1] Or (MA_Diff[1] < (ATRVal[1] * 0.1))) And
Close[1] < Open[1] And
MarketPosition == 0 And Vol > 0)
{
SellShort(TradeVolume, Open);
ShortStopPrice = Open + StopLossATRMultiple * ATRVal[1];
ShortTargetPrice = Open - TakeProfitATRMultiple * ATRVal[1];
}
// 多头入场:底背离 + 趋势向上/盘整 + 看涨K线确认
If (Condition_BottomDivergence And
(TrendUp[1] Or (MA_Diff[1] < (ATRVal[1] * 0.1))) And
Close[1] > Open[1] And
MarketPosition == 0 And Vol > 0)
{
Buy(TradeVolume, Open);
LongStopPrice = Open - StopLossATRMultiple * ATRVal[1];
LongTargetPrice = Open + TakeProfitATRMultiple * ATRVal[1];
}
// 空头出场逻辑
If (MarketPosition == -1 And BarsSinceEntry >= 0 And Vol > 0)
{
// 止损出场
If (High >= ShortStopPrice)
{
BuyToCover(0, Max(Open, ShortStopPrice));
}
// 止盈出场
Else If (Low <= ShortTargetPrice)
{
BuyToCover(0, Min(Open, ShortTargetPrice));
}
// 趋势反转出场
Else If (TrendUp)
{
BuyToCover(0, Open);
}
}
// 多头出场逻辑
If (MarketPosition == 1 And BarsSinceEntry >= 0 And Vol > 0)
{
// 止损出场
If (Low <= LongStopPrice)
{
Sell(0, Min(Open, LongStopPrice));
}
// 止盈出场
Else If (High >= LongTargetPrice)
{
Sell(0, Max(Open, LongTargetPrice));
}
// 趋势反转出场
Else If (TrendDown)
{
Sell(0, Open);
}
}
}
这策略为什么不能用,帮我优化一下
什么叫不能用
是你自己写的吗?
