我的一个策略需要在15分钟数据上计算macd,然后根据条件开仓,然后通过1分钟数据计算ma来止盈止损平仓。问题是我的代码在通过编译后,没有办法回测(回测一直显示进度0%),也无法在行情页面加载(一直闪烁加载0%-90%)
也不知道是不是两个数据源调用的方式不对,还请老师解惑。
以下为示例策略:
//------------------------------------------------------------------------
// 双图层策略示例:15分钟图生成入场信号,1分钟图用于止盈止损(适用于回测)
//------------------------------------------------------------------------
Params
Numeric Lot(1);
Vars
Numeric entryPrice(0);
Numeric stopLoss(0);
Numeric takeProfit(0);
Numeric ma;
Numeric atr;
Numeric atrMult(2.0);
Numeric tpGap(40);
Numeric barCount(0);
Global Integer layer15;
Global Integer i;
Bool buySignal(False);
Bool sellSignal(False);
Series<Numeric> diff;
Series<Numeric> dea;
Series<Numeric> macd;
Events
OnReady()
{
layer15 = SubscribeBar("rb888.SHFE", "15m", 0);
Commentary("已订阅15分钟图层,ID = " + Text(layer15));
}
OnBar(ArrayRef<Integer> indexs)
{
barCount = barCount + 1;
For i = 0 To GetArraySize(indexs) - 1
{
// ===== 15分钟图:生成进场信号 =====
If (indexs[i] == layer15)
{
diff = EMA(Close, 12) - EMA(Close, 26);
dea = EMA(diff, 9);
macd = diff - dea;
Numeric macd0 = macd[0];
Numeric macd1 = macd[1];
Numeric macd2 = macd[2];
Numeric ma15 = Average(Close, 20);
buySignal = macd2 < macd1 && macd1 < macd0 && Close > ma15;
sellSignal = macd2 > macd1 && macd1 > macd0 && Close < ma15;
Commentary("15分钟信号更新:MACD = " + Text(macd0));
}
// ===== 1分钟主图:执行进出场逻辑 =====
If (indexs[i] == 0)
{
ma = Average(Close, 20);
atr = AvgTrueRange(14);
// 开仓逻辑(由15分钟信号控制)
If (MarketPosition == 0)
{
If (buySignal)
{
Buy(Lot, 0);
entryPrice = Close;
stopLoss = Close - atrMult * atr;
takeProfit = Close + tpGap;
Commentary("开多仓@" + Text(Close));
}
Else If (sellSignal)
{
SellShort(Lot, 0);
entryPrice = Close;
stopLoss = Close + atrMult * atr;
takeProfit = Close - tpGap;
Commentary("开空仓@" + Text(Close));
}
}
// 平多仓逻辑
If (MarketPosition == 1)
{
If (Close <= stopLoss || Close >= takeProfit)
{
Sell(Lot, 0);
Commentary("平多仓@" + Text(Close));
entryPrice = 0;
stopLoss = 0;
takeProfit = 0;
}
}
// 平空仓逻辑
If (MarketPosition == -1)
{
If (Close >= stopLoss || Close <= takeProfit)
{
BuyToCover(Lot, 0);
Commentary("平空仓@" + Text(Close));
entryPrice = 0;
stopLoss = 0;
takeProfit = 0;
}
}
}
}
}
学会一个个解决问题
Events
OnInit()
{
SubscribeBar("rb888.SHFE", "15m");
}
一个句话就是,订阅数据
那么加上原来图表的周期,就是双图层了
您好,昨晚已经通过企业微信回复您了。订阅数据的代码不要放在OnReady域,应该放在OnInit里。
我改了之后还是进入不了双图层,在行情页面添加策略,没有出现开平仓信号。回测里面也是没有开仓信号。