注册麦语言指标

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

register_indicator

接口说明

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

register_indicator(indicator_name, script, param_list)

参数

参数名类型说明默认值范围
indicator_namestring指标名----
scriptstring麦语言脚本----
param_listlist麦语言脚本参数列表----

返回

无返回值

示例说明

def custom_indicator(self):
        self.register_indicator(indicator_name='MA', script='''MA1:MA(CLOSE,P1),COLORFF8D1E;MA2:MA(CLOSE,P2),COLOR0CAEE6;''', param_list=['P1', 'P2'])

示例返回值

--

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

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

注册方法如下:

1、如下图所示在指标管理中找到对应指标的麦语言脚本

1 处的指标名称填写为 indicator_name 参数
2 处的指标脚本作为 script 参数,指标脚本通常会有很多行代码,建议使用3个单引号将它括起来
3 处的参数列表,需要逐个以字符串类型放入列表中,作为 param_list 参数

指标管理

2、将以上参数分别记录在 register_indicator 接口的 indicator_name, script, param_list 中

self.register_indicator(indicator_name='OSC', 
        script='''osc:100*(close-ma(close,n)),linethick1,color0caee6;
        oscema:expmema(osc,m),linethick1,colorff8d1e;''', 
        param_list=['N', 'M'])

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

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):  # 定义自编指标
        self.register_indicator(indicator_name='OSC', 
        script='''osc:100*(close-ma(close,n)),linethick1,color0caee6;
        oscema:expmema(osc,m),linethick1,colorff8d1e;''', 
        param_list=['N', 'M'])

    def handle_data(self):  # 策略的主函数。驱动标的的行情更新,或者到达指定时间,会触发handle_data()函数
        self.v0 = get_MyLang_indicator(indicator_name="OSC", variable_name="OSC", symbol=self.驱动标的1, params={"N": 20.000}, bar_type=BarType.K_60M, select=1, session_type=THType.ALL)
        print("self.v0 = ", self.v0)