首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用QuantConnect创建指示器扩展

如何使用QuantConnect创建指示器扩展
EN

Stack Overflow用户
提问于 2022-02-22 15:21:06
回答 1查看 72关注 0票数 0

我已经为百事可乐(PEP)和可口可乐(可口可乐)之间的比率创建了一个指标,并试图从这个指标创建一个5天简单移动平均(sma)使用IndicatorExtension,然后绘制结果。违规代码位于脚本的末尾:

代码语言:javascript
复制
import numpy as np
class SquareLightBrownPanda(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 1)  # Set Start Date
        self.SetEndDate(2021,1,1) # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        self.pep = self.AddEquity("PEP", Resolution.Daily).Symbol # Add data for PEP
        self.coke = self.AddEquity("COKE", Resolution.Daily).Symbol # Add data for COKE
        self.log_ratio_spread=0
        
    
        self.sma_pep = self.SMA(self.pep,5,Resolution.Daily) #create 5 simple moving average indicator
        self.sma_coke = self.SMA(self.coke,5,Resolution.Daily) #create 5 simple moving average indicator
        
        closing_price_pep = self.History(self.pep,5,Resolution.Daily)["close"] #historic information for the past 5 bars.  
        closing_price_coke = self.History(self.coke,5,Resolution.Daily)["close"] #historic information for the past 5 bars.  
        
        for time, price in closing_price_pep.loc[self.pep].items(): #iterate over time and price information in the closing price dataframe
            self.sma_pep.Update(time, price) #update sma indicator is ready to use from the beginning
            
        for time, price in closing_price_coke.loc[self.coke].items(): #iterate over time and price information in the closing price dataframe
            self.sma_coke.Update(time, price) #update sma indicator is ready to use from the beginning    
    
    
    def OnData(self, data):
        if not self.sma_pep.IsReady and not self.sma_coke.IsReady:
            return
        
        #save the high and low price of the last year - not efficient use rolling window
        hist_pep = self.History(self.pep, timedelta(365),Resolution.Daily)
        hist_coke = self.History(self.coke, timedelta(365),Resolution.Daily)
    
        price_pep = self.Securities[self.pep].Price ##save PEP's most recent price to the price variable
        price_coke = self.Securities[self.coke].Price ##save COKE's most recent price to the price variable
            
        log_ratio_pep = np.log(price_pep/self.sma_pep.Current.Value)
        log_ratio_coke = np.log(price_coke/self.sma_coke.Current.Value)
        
        self.log_ratio_spread = log_ratio_pep - log_ratio_coke
        
        #TRYING TO CREATE 5 DAY MOVING AVERAGE OF log_ratio_spread HERE
    
        self.log_ratio_spread_sma = IndicatorExtensions.SMA(self.log_ratio_spread,5,Resolution.Daily)
            
        self.Plot("Spread","log_ratio_spread_sma",self.log_ratio_spread_sma) 

但是,当我在QuantConnect中运行代码时,会得到以下错误:

代码语言:javascript
复制
Runtime Error: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the 'int'>) method. Please checkout the API documentation.
  at OnData
    ratio_spread = IndicatorExtensions.SMA(self.log_ratio_spread in main.py: line 53
     (Open Stacktrace)

它与发光线相关联:

代码语言:javascript
复制
self.log_ratio_spread_sma = IndicatorExtensions.SMA(self.log_ratio_spread,5,Resolution.Daily)

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2022-02-23 21:53:43

问题是IndicatorExtensions.SMA方法需要一个指示器对象,但是self.log_ratio_spread是十进制的。

若要解决此问题,请创建一个SimpleMovingAverage指示器,并使用日志比率差手动更新它。

代码语言:javascript
复制
log_ratio_pep = np.log(data[self.pep].Price / self.sma_pep.Current.Value)
log_ratio_coke = np.log(data[self.coke].Price / self.sma_coke.Current.Value)
log_ratio_spread = log_ratio_pep - log_ratio_coke        
self.log_ratio_spread_sma.Update(data.Time, log_ratio_spread)

有关完整的示例,请参见这个回程测试

最好的

德瑞克·梅尔钦

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71223855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档