请问,我用AI写的一个简单的MACD程序化交易代码,编译是通过的但是在软件上做回测的时候却没有任何的历史成交记录,请问是什么原因导致的?请大神不吝赐教。 以下是代码:
Params
Numeric FastLength(12); // MACD快速周期
Numeric SlowLength(26); // MACD慢速周期
Numeric SignalLength(9); // MACD信号周期
Numeric StopProfitLossRate(0.3); // 固定止盈止损比例,设置为0.3%
Numeric PositionRatio(0.3); // 开仓仓位比例,设置为30%
Vars
Series<Numeric> DIF; // MACD的DIF线
Series<Numeric> DEA; // MACD的DEA线
Series<Numeric> MACD; // MACD柱状图
Numeric OpenPrice; // 当日开盘价
Numeric StopProfitPrice; // 止盈价格
Numeric StopLossPrice; // 止损价格
Numeric TotalEquity; // 账户权益
Numeric PositionSize; // 开仓数量
Numeric LastHigh; // 上一个股价高点
Numeric LastDIFHigh; // 上一个DIF高点
Numeric LastLow; // 上一个股价低点
Numeric LastDIFLow; // 上一个DIF低点
Numeric GoldenCrossBar; // 记录MACD金叉的K线序号
Numeric DeadCrossBar; // 记录MACD死叉的K线序号
Bool IsTopDivergence; // 是否出现顶背离
Bool IsBottomDivergence; // 是否出现底背离
Events
OnBar(ArrayRef<Integer> indexs)
{
// 确保有足够数据进行指标计算
if (CurrentBar < Max(Max(FastLength, SlowLength), SignalLength)) {
return;
}
// 计算MACD指标
DIF = XAverage(Close, FastLength) - XAverage(Close, SlowLength);
DEA = XAverage(DIF, SignalLength);
MACD = DIF - DEA;
OpenPrice = Open[CurrentBar - CurrentBar % (15 / 5)]; // 获取当日开盘价,假设15分钟为一个交易时段
TotalEquity = Portfolio_CurrentCapital() + Portfolio_UsedMargin();
PositionSize = IntPart(TotalEquity * PositionRatio / Close);
// 处理顶背离
if (High > LastHigh && DIF < LastDIFHigh) {
IsTopDivergence = True;
} else {
IsTopDivergence = False;
}
LastHigh = High;
LastDIFHigh = DIF;
// 处理底背离
if (Low < LastLow && DIF > LastDIFLow) {
IsBottomDivergence = True;
} else {
IsBottomDivergence = False;
}
LastLow = Low;
LastDIFLow = DIF;
// 处理MACD金叉和死叉
if (CrossOver(DIF, DEA)) {
GoldenCrossBar = CurrentBar;
}
if (CrossUnder(DIF, DEA)) {
DeadCrossBar = CurrentBar;
}
// 顶背离且MACD死叉后第二根K线开盘价进场做空
if (IsTopDivergence && CurrentBar == DeadCrossBar + 2) {
SellShort(PositionSize, Open[1]);
}
// 底背离且MACD金叉后第二根K线开盘价进场做多
if (IsBottomDivergence && CurrentBar == GoldenCrossBar + 2) {
Buy(PositionSize, Open[1]);
}
// 计算止盈止损价格
StopProfitPrice = OpenPrice * (1 + StopProfitLossRate / 100);
StopLossPrice = OpenPrice * (1 - StopProfitLossRate / 100);
// 多仓止盈止损处理
if (MarketPosition == 1) {
if (High >= StopProfitPrice) {
Sell(0, StopProfitPrice);
}
if (Low <= StopLossPrice) {
Sell(0, StopLossPrice);
}
}
// 空仓止盈止损处理
if (MarketPosition == -1) {
if (Low <= StopProfitPrice) {
BuyToCover(0, StopProfitPrice);
}
if (High >= StopLossPrice) {
BuyToCover(0, StopLossPrice);
}
}
}
没信号说明判断逻辑有问题,至于什么问题,要问谁设计并实现的。到底是设计的逻辑就有问题,还是实现的步骤里有问题?这个就要问设计者和实现者。既然是ai写的,那就是只能问题ai了。