注册Python指标

本节内容仅用于代码策略,可视化策略不涉及以下内容。

register_indicator_Python

接口说明

使用此接口注册一个Python指标,以便策略调用。

register_indicator_Python(indicator_name, script)

参数

参数名类型说明默认值范围
indicator_namestring指标名----
scriptstringPython脚本----

返回

无返回值

示例说明

def custom_indicator(self):
    script='''
    
    indicator('MA5', '移动平均线')

    def ma(n=5):
        return close().sma(n)

    if __name__ == "__main__":
        n1 = input_parameter("n1", 5)
        plot(f"MA{n1}", ma(n1), color=Color.hex("#FF8D1E"))
        output_parameter(MA1=ma(n1))

    '''
    self.register_indicator_Python(indicator_name='MA5', script=script)

示例返回值

--

get_Python_indicator

接口说明

获取指定标的指定 K 线周期下的 Python 指标。

get_Python_indicator(indicator_name, variable_name, symbol, params, bar_type=BarType.K_60M,select=2, session_type = THType.ALL)

纯代码策略中,需要自行调用 register_indicator_Python 接口,将策略的Python脚本写到代码策略中,才能正常使用该指标。具体操作参考 register_indicator_Python()。

参数

参数名类型说明默认值范围
indicator_namestr指定指标名--None
variable_namestr指定指标的一个变量--None
symbolContract标的--None
paramsdict指标参数{}None
bar_typeBarTypeK 线周期BarType.K_60MNone
selectint选取倒数第几根 K 线数据21-500
session_typeTHType时段类型(仅对美股市场生效)THType.ALL--

返回

返回类型: float

示例说明

获取苹果最新一根 1 小时 K 线的 MA5 指标的 MA5 值。

get_Python_indicator(indicator_name='MA5', variable_name='MA5', symbol=Contract('US.AAPL'), params={"n1": 5.000}, bar_type=BarType.K_60M, select=1, session_type = THType.RTH)

示例返回值

151.30567

代码策略获取技术指标示例

代码策略中,如果希望使用 get_Python_indicator() 获取技术指标,需要在 custom_indicator() 约定函数中调用该接口,把策略的Python脚本写到代码策略中,才能正常使用 get_Python_indicator()。

注册方法如下:

1、如下图所示在指标管理中找到对应指标的Python脚本(需要切换至 Python 编辑模式)

1 处的指标名称填写为 indicator_name 参数
2 处的指标脚本作为 script 参数,指标脚本通常会有很多行代码,建议使用3个单引号将它括起来

py指标管理

2、将以上内容分别记录在 register_indicator_Python 接口的 indicator_name, script 中,script可以单独记录出来

script='''
    
    indicator('MA5', '移动平均线')

    def ma(n=5):
        return close().sma(n)

    if __name__ == "__main__":
        n1 = input_parameter("n1", 5)
        plot(f"MA{n1}", ma(n1), color=Color.hex("#FF8D1E"))
        output_parameter(MA1=ma(n1))

    '''
self.register_indicator_Python(indicator_name='MA5', script=script)

3、通过代码策略获取 MA5 指标的 MA5 变量,整个策略的完整代码如下

class Strategy(StrategyBase):

    def initialize(self): # 初始化
        declare_strategy_type(AlgoStrategyType.SECURITY)
        self.trigger_symbols()
        self.custom_indicator()
        self.global_variables()

    def trigger_symbols(self):  # 定义驱动标的
        self.驱动标的1 = declare_trig_symbol()

    def global_variables(self):  # 定义全局变量
        self.v0 = show_variable(0, GlobalType.INT)
        self.v1 = show_variable(1, GlobalType.INT)

    def custom_indicator(self):  # 定义Python自编指标
        script='''
    
    indicator('MA5', '移动平均线')

    def ma(n=5):
        return close().sma(n)

    if __name__ == "__main__":
        n1 = input_parameter("n1", 5)
        plot(f"MA{n1}", ma(n1), color=Color.hex("#FF8D1E"))
        output_parameter(MA1=ma(n1))

    '''
        self.register_indicator_Python(indicator_name='MA5', script=script)

    def handle_data(self):  # 策略的主函数。驱动标的的行情更新,或者到达指定时间,会触发handle_data()函数
        # indicator_name: 指标名称,script脚本里的indicator()
        # variable_name:变量名称,output_parameter()里的变量写法,例如MA{n1}
        self.v0 = get_Python_indicator(indicator_name="MA5", variable_name="MA5", symbol=self.驱动标的1, params={"n1": 5.000}, bar_type=BarType.K_60M, select=2, session_type=THType.ALL)
        print("self.v0 = ", self.v0)