Pine Script代码优化:解决request.security调用超限问题
问题描述
运行Pine Script代码时触发错误:Script requests too many securities: 50. The limit is 40。
代码通过多次调用request.security,获取s01至s06共6个交易对在5、15、30、60、120、240分钟这6种时间周期下的EMA交叉相关数据,需要简化代码减少request.security调用次数。原代码如下:
screener_func() => ema12 = ta.ema(close,12) ema26 = ta.ema(close,26) crossdown = ta.crossunder(expma12, expma26) crossup = ta.crossover(expma12, expma26) HitungBarEma = ta.barssince(crossdown or crossup) array.push(EmaPair1, EmaPair1_1) [EmaPair1_1] = request.security(s01, '5', screener_func()) [EmaPair1_2] = request.security(s01, '15', screener_func()) [EmaPair1_3] = request.security(s01, '30', screener_func()) [EmaPair1_4] = request.security(s01, '60', screener_func()) [EmaPair1_5] = request.security(s01, '120', screener_func()) [EmaPair1_6] = request.security(s01, '240', screener_func()) [EmaPair2_1] = request.security(s02, '5', screener_func()) [EmaPair2_2] = request.security(s02, '15', screener_func()) [EmaPair2_3] = request.security(s02, '30', screener_func()) [EmaPair2_4] = request.security(s02, '60', screener_func()) [EmaPair2_5] = request.security(s02, '120', screener_func()) [EmaPair2_6] = request.security(s02, '240', screener_func()) [EmaPair3_1] = request.security(s03, '5', screener_func()) [EmaPair3_2] = request.security(s03, '15', screener_func()) [EmaPair3_3] = request.security(s03, '30', screener_func()) [EmaPair3_4] = request.security(s03, '60', screener_func()) [EmaPair3_5] = request.security(s03, '120', screener_func()) [EmaPair3_6] = request.security(s03, '240', screener_func()) [EmaPair4_1] = request.security(s04, '5', screener_func()) [EmaPair4_2] = request.security(s04, '15', screener_func()) [EmaPair4_3] = request.security(s04, '30', screener_func()) [EmaPair4_4] = request.security(s04, '60', screener_func()) [EmaPair4_5] = request.security(s04, '120', screener_func()) [EmaPair4_6] = request.security(s04, '240', screener_func()) [EmaPair5_1] = request.security(s05, '5', screener_func()) [EmaPair5_2] = request.security(s05, '15', screener_func()) [EmaPair5_3] = request.security(s05, '30', screener_func()) [EmaPair5_4] = request.security(s05, '60', screener_func()) [EmaPair5_5] = request.security(s05, '120', screener_func()) [EmaPair5_6] = request.security(s05, '240', screener_func()) [EmaPair6_1] = request.security(s06, '5', screener_func()) [EmaPair6_2] = request.security(s06, '15', screener_func()) [EmaPair6_3] = request.security(s06, '30', screener_func()) [EmaPair6_4] = request.security(s06, '60', screener_func()) [EmaPair6_5] = request.security(s06, '120', screener_func()) [EmaPair6_6] = request.security(s06, '240', screener_func())
优化方案
使用request.security_all一次性获取多个标的在同一周期的数据,将request.security类调用的总次数从36次降至6次(对应6个时间周期),完全符合40次的限制要求。
1. 定义标的和时间周期数组
// 存储需要查询的6个交易对 symbols = [s01, s02, s03, s04, s05, s06] // 存储需要查询的6个时间周期 timeframes = ['5', '15', '30', '60', '120', '240']
2. 编写单周期EMA交叉数据计算函数
修正原代码的变量名错误(expma12/expma26应为ema12/ema26),编写通用计算函数:
calc_ema_cross_data() => ema12 = ta.ema(close, 12) ema26 = ta.ema(close, 26) crossdown = ta.crossunder(ema12, ema26) crossup = ta.crossover(ema12, ema26) ta.barssince(crossdown or crossup)
3. 批量获取所有周期的标的数据
遍历每个时间周期,用request.security_all一次性获取当前周期下所有标的的数据:
// 存储最终结果:外层数组对应时间周期,内层数组对应标的数据 var all_results = array.new_array() for tf in timeframes // 单次调用获取当前周期下6个标的的EMA交叉数据 period_data = request.security_all(symbols, tf, calc_ema_cross_data()) array.push(all_results, period_data)
4. 访问目标数据
通过数组索引匹配原代码的变量逻辑:
// 示例:获取s01在5分钟周期的数据(s01是symbols第0位,5分钟是timeframes第0位) EmaPair1_1 = array.get(array.get(all_results, 0), 0) // 获取s02在15分钟周期的数据(s02是symbols第1位,15分钟是timeframes第1位) EmaPair2_2 = array.get(array.get(all_results, 1), 1) // 原代码变量对应关系: // EmaPairX_Y → symbols[X-1],timeframes[Y-1] // 比如EmaPair3_4(s03的60分钟数据)→ array.get(array.get(all_results, 3), 2)
内容的提问来源于stack exchange,提问作者EMPTY
相关产品推荐
相关产品推荐

