quotation.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:Mercury 作者: ruyiqf 项目源码 文件源码
def history(self, databar, period, indicator):
        """Recieve mock data as databar dataframe format
        :databar: mock data bar, flexible, means will be changed by indicator, one dimension or several dimensions
        :period: time interval, flexbile too
        :indicator: include ['sma','macd','atr' ...]
        """
        if indicator == 'sma':
            try:
                sma0 = talib.SMA(np.array(databar.low), timeperiod = period)
                sma1 = talib.SMA(np.array(databar.close), timeperiod = period)
                sma2 = talib.SMA(np.array(databar.high), timeperiod = period)
                return pd.DataFrame({'sma0':sma0, 'sma1':sma1, 'sma2':sma2}, index=pd.DatetimeIndex(databar.date))
            except KeyError:
                print('Pls check databar whether is dataframe')
                return None

        elif indicator == 'atr':
            try:
                atr = talib.ATR(np.array(databar.high), np.array(databar.low), np.array(databar.close), timeperiod = period)
                return pd.DataFrame({'atr':atr}, index=pd.DatetimeIndex(databar.date))
            except KeyError:
                print('Pls check databar whether is dataframe')
                return None
        elif indicator == 'macd':
            try:
                macd, macdsignal, macdhist = talib.MACD(databar, 
                                                        fastperiod = period['fastperiod'],
                                                        slowperiod = period['slowperiod'],
                                                        signalperiod = period['signalperiod'])

                return pd.DataFrame({'macd':macd, 'macdsignal':macdsignal, 'macdhist':macdhist}, index=pd.DatetimeIndex(databar.date))
            except KeyError:
                print('Pls check databar whether is dataframe')
                return None
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号