跟踪市场情绪交易策略的时间序列的历史记录。错误:类型'numpy.bool_'的参数不可迭代

时间:2019-04-08 15:40:29

标签: python pandas quantitative-finance trading

Problem visualization 你好!

我正在为我的投资类开发市场情绪交易策略。基本上,该策略目前包括在1区和3区卖出市场指数,并在2区和4区买入(持有)市场指数。区分为三行:上(平均+标准差),下(平均) -标准差)和之前计算出的市场情绪指标的平均水平。

我要做的是通过进入第4区后购买市场指数并仅在达到第1区时退出市场来增强程序,反之亦然。因此,区域2和3的结果基于趋势的来源(如果趋势通过区域1或区域4进入)。

我怎么可能这样做?

感谢您的帮助!

 for i in range(data.shape[0]) :


        Zone_1 = data.loc[i,"Market Sentiment"] > data.loc[i,'Upper Bound']
        Zone_2 = data.loc[i,"Market Sentiment"] > data.loc[i,'Average Sentiment'] and data.loc[i,"Market Sentiment"] <= data.loc[i,'Upper Bound']
        Zone_3 = data.loc[i,"Market Sentiment"] > data.loc[i,'Lower Bound'] and data.loc[i,"Market Sentiment"] <= data.loc[i,'Average Sentiment']
        Zone_4 = data.loc[i,"Market Sentiment"] <= data.loc[i,'Lower Bound']

        if Zone_1:
            tracker = "n"
            list1.append(data.loc[i,'risk free rate']) 

        elif Zone_2 and data.loc[i-1,"Market Sentiment"] in Zone_1:
            tracker = 'down'
            list1.append(data.loc[i,'risk free rate']) 
        elif Zone_2 and data.loc[i-1,"Market Sentiment"] in Zone_2:
            if data.loc[i-1,'tracker'] == 'down':
                list1.append(data.loc[i,'HS300 Index Return'])
            else:
                list1.append(data.loc[i,'risk free rate'])

        elif Zone_2 and data.loc[i-1,'Market Sentiment'] in Zone_3:
            tracker = 'up'
            list1.append(data.loc[i,'HS300 Index Return'])

        elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_2:
            tracker = 'down'
            list1.append(data.loc[i,"risk free rate"])

        elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_3:
            if data.loc[i-1,'tracker'] == 'down':
                list1.append(data.loc[i,'risk free rate'])
            else:
                list1.append(data.loc[i,'HS300 Index Return'])
            list1.append(data.loc[i,'HS300 Index Return'])

        elif Zone_3 and data.loc[i-1,'Market Sentiment'] in Zone_4:
            tracker = 'up'
            list1.append(data.loc[i,'HS300 Index Return'])

        elif Zone_4:
            list1.append(data.loc[i,"HS300 Index Return"]) 

    data['Strategy Return'] = list1
    data['Tracker'] = tracker
    pd.options.display.float_format = '{:.4f}'.format
    return data

0 个答案:

没有答案