如何基于触发的多头信号计算一周远期收益率及数据展示?
问题描述
我需要为已有的多头信号计算一周远期收益率,目前已将所有信号触发日期存储于数组中,但无法完成涨跌幅的计算,请问如何从多头信号触发节点开始测算该收益率?以下为我编写的Pine Script代码,尝试定义多周期远期收益率、存储信号日期及对应收益率并通过表格展示,但计算逻辑存在问题:
// Defining future return intervals return1WeekFuture = close / close[7] - 1 return2WeeksFuture = close / close[14] - 1 return3WeeksFuture = close / close[21] - 1 return1MonthFuture = close / close[30] - 1 return2MonthsFuture = close / close[60] - 1 return3MonthsFuture = close / close[90] - 1 plotshape(long, style=shape.triangleup, size=size.small, location=location.belowbar, color=color.green) // Use 'varip' for the array to persist its contents across bars var counter = 0 varip dateArray = array.new_string(0) varip owArray = array.new_string(0) // Add the date to the array when 'long' condition is met if (long) counter := counter + 1 array.push(dateArray, str.tostring(year) + "-" + str.tostring(month) + "-" + str.tostring(dayofmonth)) array.push(owArray, str.tostring(return1WeekFuture)) // Display the date from the array in a table var table cond = table.new(position.bottom_right, 100, 100) if (array.size(dateArray) > 0) table.cell(cond, 1, 1, text= "DATE", text_color=color.white, text_size=size.small, text_halign=text.align_center, bgcolor = color.blue) for i = 0 to counter - 1 table.cell(cond, 1, i, text=array.get(dateArray, i), text_color=color.black, text_size=size.small, text_halign=text.align_center) table.cell(cond, 2, i, text=array.get(owArray, i), text_color=color.black, text_size=size.small, text_halign=text.align_center)
问题分析与修正方案
你的代码核心问题在于远期收益率计算逻辑错误:当前return1WeekFuture = close / close[7] - 1是用当前K线收盘价除以7根K线前的收盘价,计算的是当前K线相对7天前的收益率,而非从信号触发日往后推7天的远期收益率。
要实现从信号触发节点测算远期收益率,需按以下思路调整:
1. 记录信号触发时的关键数据
当多头信号触发时,存储触发日期、收盘价,以及需要跟踪的剩余K线计数(比如一周对应7根日线),此时不直接计算收益率(因为7天后的价格尚未出现)。
2. 跟踪信号进度并到期计算收益率
对每个未完成的信号,每过一根K线就递减剩余计数,当计数归0时,用当前收盘价除以信号触发时的收盘价,得到该信号的一周远期收益率。
修正后的完整代码
//@version=5 indicator("远期收益率计算", overlay=true) // 示例多头信号(替换为你实际的信号条件) long = ta.crossover(ta.sma(close, 5), ta.sma(close, 20)) // 存储信号相关数据的数组:日期、触发收盘价、远期收益率、剩余计数 varip signalDates = array.new_string() varip signalEntryPrices = array.new_float() varip signalWeekReturns = array.new_float() varip signalRemainingBars = array.new_int() // 信号触发时添加记录 if (long) array.push(signalDates, str.tostring(year) + "-" + str.tostring(month) + "-" + str.tostring(dayofmonth)) array.push(signalEntryPrices, close) array.push(signalWeekReturns, na) // 初始设为未计算 array.push(signalRemainingBars, 7) // 一周按7根日线计算,其他周期需调整数值 // 遍历更新信号进度,到期计算收益率 for i = 0 to array.size(signalRemainingBars) - 1 remaining = array.get(signalRemainingBars, i) if not na(remaining) and remaining > 0 array.set(signalRemainingBars, i, remaining - 1) // 计数归0时计算收益率 if remaining - 1 == 0 entryPrice = array.get(signalEntryPrices, i) array.set(signalWeekReturns, i, close / entryPrice - 1) // 绘制多头信号标记 plotshape(long, style=shape.triangleup, size=size.small, location=location.belowbar, color=color.green) // 展示信号结果表格 var resultTable = table.new(position.bottom_right, 3, 100, bgcolor=color.gray) // 表头 table.cell(resultTable, 0, 0, "信号日期", text_color=color.white, bgcolor=color.blue, text_halign=text.align_center) table.cell(resultTable, 1, 0, "触发价", text_color=color.white, bgcolor=color.blue, text_halign=text.align_center) table.cell(resultTable, 2, 0, "一周收益率", text_color=color.white, bgcolor=color.blue, text_halign=text.align_center) // 填充表格内容 for i = 0 to array.size(signalDates) - 1 table.cell(resultTable, 0, i+1, array.get(signalDates, i), text_color=color.black, text_halign=text.align_center) table.cell(resultTable, 1, i+1, str.tostring(array.get(signalEntryPrices, i), "%.2f"), text_color=color.black, text_halign=text.align_center) returnVal = array.get(signalWeekReturns, i) table.cell(resultTable, 2, i+1, na(returnVal) ? "待计算" : str.tostring(returnVal * 100, "%.2f%"), text_color=na(returnVal) ? color.orange : returnVal > 0 ? color.green : color.red, text_halign=text.align_center)
关键说明
- 周期适配:代码中用7根K线代表一周,若使用小时线,需将
array.push(signalRemainingBars, 7)改为168(7*24),根据你的实际K线周期调整数值。 - 数组同步:所有信号相关数组长度保持一致,确保索引对应正确。
- 实时更新:未到期的信号显示"待计算",到期后自动更新收益率,并用颜色区分正负收益。
内容的提问来源于stack exchange,提问作者reeds
相关产品推荐
相关产品推荐

