PINE LIBRARY

TrailingStopLibrary

173
Library "TrailingStopLibrary"
专业移动止盈库 - 为Pine Script策略提供完整的追踪止盈功能。支持做多做空双向交易,基于风险回报比智能激活,提供收盘价和高低价两种判断模式。包含完整的状态管理、调试信息和易用的API接口。适用于股票、外汇、加密货币等各种市场的风险管理。
version 1.0
Author runto2006

new_config(enabled, activation_ratio, pullback_percent, price_type)
  创建移动止盈配置对象
  Parameters:
    enabled (bool): (bool) 是否启用移动止盈,默认true
    activation_ratio (float): (float) 激活盈亏比,默认4.0,表示盈利4倍止损距离时激活
    pullback_percent (float): (float) 回撤百分比,默认1.0,表示回撤1%时触发止盈
    price_type (string): (string) 价格类型,默认"close"。"close"=收盘价模式,"hl"=高低价模式
  Returns: Config 配置对象

new_state()
  创建移动止盈状态对象
  Returns: State 初始化的状态对象

reset(state)
  重置移动止盈状态
  Parameters:
    state (State): (State) 要重置的状态对象
  Returns: void

calc_activation_target(entry_price, stop_price, activation_ratio, is_long)
  计算激活目标价格
  Parameters:
    entry_price (float): (float) 入场价格
    stop_price (float): (float) 止损价格
    activation_ratio (float): (float) 激活盈亏比
    is_long (bool): (bool) 是否为多头持仓
  Returns: float 激活目标价格,如果输入无效则返回na

get_check_price(price_type, is_long, for_activation)
  获取用于判断的价格
  Parameters:
    price_type (string): (string) 价格类型:"close"或"hl"
    is_long (bool): (bool) 是否为多头持仓
    for_activation (bool): (bool) 是否用于激活判断,影响高低价的选择方向
  Returns: float 当前判断价格

check_activation(config, state, entry_price, stop_price, is_long, has_position)
  检查是否应该激活移动止盈
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    entry_price (float): (float) 入场价格
    stop_price (float): (float) 止损价格
    is_long (bool): (bool) 是否为多头持仓
    has_position (bool): (bool) 是否有持仓
  Returns: bool 是否成功激活

update_tracking(config, state, is_long)
  更新移动止盈的追踪价格
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    is_long (bool): (bool) 是否为多头持仓
  Returns: void

check_trigger(config, state, entry_price, is_long)
  检查是否触发移动止盈
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    entry_price (float): (float) 入场价格
    is_long (bool): (bool) 是否为多头持仓
  Returns: bool 是否触发止盈

process(config, state, entry_price, stop_price, is_long, has_position)
  一体化处理移动止盈逻辑
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    entry_price (float): (float) 入场价格
    stop_price (float): (float) 止损价格
    is_long (bool): (bool) 是否为多头持仓
    has_position (bool): (bool) 是否有持仓
  Returns: bool 是否触发止盈

get_trigger_price(config, state, is_long)
  获取当前触发价格
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    is_long (bool): (bool) 是否为多头持仓
  Returns: float 触发价格,未激活时返回na

get_pullback_percent(config, state, entry_price, is_long)
  计算当前回撤百分比
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    entry_price (float): (float) 入场价格
    is_long (bool): (bool) 是否为多头持仓
  Returns: float 当前回撤百分比,未激活时返回na

get_status_info(config, state, entry_price, is_long)
  获取状态信息字符串(用于调试)
  Parameters:
    config (Config): (Config) 移动止盈配置
    state (State): (State) 移动止盈状态
    entry_price (float): (float) 入场价格
    is_long (bool): (bool) 是否为多头持仓
  Returns: string 详细的状态信息

Config
  移动止盈配置对象
  Fields:
    enabled (series bool): (bool) 是否启用移动止盈功能
    activation_ratio (series float): (float) 激活盈亏比 - 盈利达到止损距离的多少倍时激活追踪
    pullback_percent (series float): (float) 回撤百分比 - 从最优价格回撤多少百分比时触发止盈
    price_type (series string): (string) 价格判断类型 - "close"使用收盘价,"hl"使用高低价

State
  移动止盈状态对象
  Fields:
    activated (series bool): (bool) 是否已激活追踪止盈
    highest_price (series float): (float) 激活后记录的最高价格
    lowest_price (series float): (float) 激活后记录的最低价格
    activation_target (series float): (float) 激活目标价格

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.