如何在Pine Script回测统计表格中添加首笔交易日期?
解决方案:在回测统计表格添加首笔交易时间
问题说明
需要在Pine Script策略的回测统计表格末尾新增一行,展示首笔交易的日期和时间,尝试过strategy.closedtrades相关函数未解决,希望在回测界面呈现该信息。
实现步骤
- 获取首笔交易时间:通过
strategy.closedtrades获取已关闭交易总数,若存在交易,取最早一笔(索引为strategy.closedtrades - 1)的入场时间;若无交易则显示提示文本。 - 填充表格行:原表格已定义为2列11行,直接使用第11行(索引10)来展示首笔交易时间。
- 格式化时间显示:将时间转换为易读的字符串格式,并处理无交易的容错场景。
修改后的完整代码
// *** BACKTESTER DISPLAY *** // Declare performance tracking variables drawTester = input.bool(true, "Draw Tester", group="Draw Backtester on Chart") var balance = strategy.initial_capital var drawdown = 0.0 var maxDrawdown = 0.0 var maxBalance = 0.0 var totalWins = 0 var totalLoss = 0 // Prepare stats table var table testTable1 = table.new(position.bottom_left, 2, 11, border_width=2) f_fillCell1(_table, _column, _row, _title, _bgcolor, _txtcolor) => _cellText = _title + "\n" table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, height = 3, text_halign="left") // Prepare stats table f_fillCell2(_table, _column, _row, _value, _bgcolor, _txtcolor) => _cellText = _value table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, height = 3, text_halign="right") // Custom function to truncate (cut) excess decimal places truncate(_number, _decimalPlaces) => _factor = math.pow(10, _decimalPlaces) int(_number * _factor) / _factor // Draw stats table var bgcolor = color.new(color.black,0) if drawTester if barstate.islastconfirmedhistory // Update table dollarReturn = strategy.netprofit f_fillCell1(testTable1, 0, 0, "Total Trades:", bgcolor, color.white) f_fillCell2(testTable1, 1, 0, str.tostring(strategy.closedtrades), bgcolor, color.white) f_fillCell1(testTable1, 0, 1, "Win Rate %:", bgcolor, color.white) f_fillCell2(testTable1, 1, 1, str.tostring(truncate((strategy.wintrades/strategy.closedtrades)*100,2)), bgcolor, color.white) f_fillCell1(testTable1, 0, 2, "Starting $:", bgcolor, color.white) f_fillCell2(testTable1, 1, 2, str.tostring(strategy.initial_capital), bgcolor, color.white) f_fillCell1(testTable1, 0, 3, "Ending $:", bgcolor, color.white) f_fillCell2(testTable1, 1, 3, str.tostring(truncate(strategy.initial_capital + strategy.netprofit,2)), bgcolor, color.white) f_fillCell1(testTable1, 0, 4, "Avg Win $:", bgcolor, color.white) f_fillCell2(testTable1, 1, 4, str.tostring(truncate(strategy.grossprofit / strategy.wintrades, 2)), bgcolor, color.white) f_fillCell1(testTable1, 0, 5, "Avg Loss $:", bgcolor, color.white) f_fillCell2(testTable1, 1, 5, str.tostring(truncate(strategy.grossloss / strategy.losstrades, 2)), bgcolor, color.white) f_fillCell1(testTable1, 0, 6, "Profit Factor:", bgcolor, color.white) f_fillCell2(testTable1, 1, 6, str.tostring(truncate(strategy.grossprofit / strategy.grossloss,2)), strategy.grossprofit > strategy.grossloss ? color.green : color.red, color.white) f_fillCell1(testTable1, 0, 7, "Max Runup:", bgcolor, color.white) f_fillCell2(testTable1, 1, 7, str.tostring(truncate(strategy.max_runup, 2 )), bgcolor, color.white) f_fillCell1(testTable1, 0, 8, "Return:", bgcolor, color.white) f_fillCell2(testTable1, 1, 8, (dollarReturn > 0 ? "+" : "") + str.tostring(truncate((dollarReturn / strategy.initial_capital)*100,2)) + "%", dollarReturn > 0 ? color.green : color.red, color.white) f_fillCell1(testTable1, 0, 9, "Max DD:", bgcolor, color.white) f_fillCell2(testTable1, 1, 9, str.tostring(truncate((strategy.max_drawdown / strategy.equity) * 100 ,2)) + "%", color.red, color.white) // 新增:首笔交易时间 firstTradeText = "无交易" if strategy.closedtrades > 0 firstTradeIdx = strategy.closedtrades - 1 firstTradeTime = strategy.closedtrades.entry_time(firstTradeIdx) firstTradeText = str.tostring(firstTradeTime, "yyyy-MM-dd HH:mm") f_fillCell1(testTable1, 0, 10, "首笔交易时间:", bgcolor, color.white) f_fillCell2(testTable1, 1, 10, firstTradeText, bgcolor, color.white) // --- END TESTER CODE --- ///////////////
关键说明
strategy.closedtrades.entry_time(n):获取第n笔已关闭交易的入场时间,n=0对应最近关闭的交易,strategy.closedtrades -1对应最早的首笔交易。str.tostring(firstTradeTime, "yyyy-MM-dd HH:mm"):将时间格式化为易读的字符串,可根据需求调整格式(如yyyy/MM/dd)。- 增加无交易场景的容错处理,避免空值报错。
内容的提问来源于stack exchange,提问作者Kevin.G
相关产品推荐
相关产品推荐

