Pine Script中strategy.long与strategy.short含义及示例代码作用是什么?
Pine Script中strategy.long与strategy.short的含义及作用
基础含义
strategy.long是Pine Script策略模块的内置常量,代表多头入场方向,底层固定取值为1,无需开发者手动定义赋值。strategy.short是对应的方向常量,代表空头入场方向,底层固定取值为-1。
示例代码中的作用
你给出的代码片段如下:
strategy.entry("LONG", strategy.long, when = "some_condition") strategy.close("LONG", when = "some_condition") strategy.entry("SHORT", strategy.short, when = "some_condition") strategy.close("SHORT", when = "some_condition")
两个常量的核心作用都是给strategy.entry开仓函数指定交易方向:
- 第一行传入
strategy.long,指定当some_condition条件触发时,执行多头开仓操作,这笔订单的唯一标识为LONG。 - 第三行传入
strategy.short,指定当对应条件触发时,执行空头开仓操作,这笔订单的唯一标识为SHORT。 - 后续的
strategy.close函数通过匹配订单ID即可定向平掉对应仓位,不需要再传入方向参数。
使用内置方向常量而非直接写1/-1的好处是代码可读性更强,也能避免手动填值出错的问题。
内容的提问来源于stack exchange,提问作者Abhinav Manthri
相关产品推荐
相关产品推荐

