// 1. 简单金叉策略(只有买入信号)
const result = await client.quant.backtest({
startDate: '2023-01-01',
endDate: '2024-01-01',
symbols: ['000001'],
entryFormula: 'CROSS(MA(CLOSE, 5), MA(CLOSE, 20))'
});
console.log(`总收益: ${result.total_return.toFixed(2)}`);
console.log(`收益率: ${(result.total_return_pct * 100).toFixed(2)}%`);
console.log(`最大回撤: ${(result.max_drawdown * 100).toFixed(2)}%`);
console.log(`胜率: ${(result.win_rate * 100).toFixed(2)}%`);
console.log(`盈亏比: ${result.profit_factor.toFixed(2)}`);
console.log(`交易次数: ${result.total_trades}`);
// 2. 买入和卖出信号都指定
const result2 = await client.quant.backtest({
startDate: '2023-01-01',
endDate: '2024-01-01',
symbols: ['000001'],
entryFormula: 'CROSS(MA(CLOSE, 5), MA(CLOSE, 20))', // 金叉买入
exitFormula: 'CROSSDOWN(MA(CLOSE, 5), MA(CLOSE, 20))' // 死叉卖出
});
// 3. RSI 超卖超买策略
const result3 = await client.quant.backtest({
startDate: '2023-01-01',
endDate: '2024-01-01',
symbols: ['000001'],
entryFormula: 'RSI(14) < 30', // RSI 低于 30 买入
exitFormula: 'RSI(14) > 70' // RSI 高于 70 卖出
});
// 4. 使用 signalFactors 记录额外指标
const resultIndicator = await client.quant.backtest({
startDate: '2023-01-01',
endDate: '2024-01-01',
symbols: ['000001'],
entryFormula: 'CROSS(MA(CLOSE, 5), MA(CLOSE, 10))',
exitFormula: 'CROSS(MA(CLOSE, 10), MA(CLOSE, 5))',
signalFactors: {
ma5: 'MA(CLOSE, 5)',
ma10: 'MA(CLOSE, 10)'
}
});
// 5. 自定义策略
const customCode = `
def handle_data(context, datas):
for data in datas:
symbol = data.name
position = context.portfolio.get_position(symbol)
if position is None:
if data.close > data.open: # 阳线买入
context.order_target_percent(symbol, 0.2)
else:
if data.close < data.open: # 阴线卖出
context.order_target_percent(symbol, 0)
`;
const resultCustom = await client.quant.backtest({
startDate: '2023-01-01',
endDate: '2024-01-01',
symbols: ['000001'],
strategyCode: customCode
});