【问题标题】:Trading Simulation, compare 2 data frame : Python, Pandas, .loc .where交易模拟,比较 2 个数据框:Python、Pandas、.loc .where
【发布时间】:2016-06-15 10:41:12
【问题描述】:
  1. 以下是我现在正在处理的数据 data image for easy understanding

  2. 以下是代码

    buying_list = data[data['Buying']==1]
    selling_list = data[data['Selling']==1]
    data['Cut_Off_Signal']=0
    
    
    # Buying & Selling & Cut off signal process
    def test(data):
        data_index = list(data.index)
        buying_list_index = list(buying_list.index)
        for i in range(len(data_index)):
            for j in range(len(buying_list_index)):
                if buying_list_index[j]<=data_index[i]:
                    #data.loc[(data['Cut_Off_Price'][j] < data['Close'][i]) & (data['Cut_Off_Price'][j] >= 1), 'Cut_Off_Signal'] = 1
                    data['Cut_Off_Signal'][i] = np.where((data['Cut_Off_Price'][j] < data['Close'][i]) & (data['Cut_Off_Price']>=1)  , 1, 0)
                    #data.loc[(data['Cut_Off_Price'][:i] < data['Close'][i]) & (data['Cut_Off_Price']!= 0), 'Cut_Off_Signal'] = 1
            return data
    
  3. 数据

        Date    Open    High    Low Close   Volume  Adj Close   Buying  Cut_Off_Price   Selling Cut_Off_Signal
    2015-10-13  256000  257000  255000  256000  161200  245982.6    0   0   0   0
    2015-10-14  257000  260000  256000  257500  147700  247423.91   0   0   0   0
    2015-10-15  257500  260500  256000  259000  139700  248865.21   0   0   0   0
    2015-10-16  258000  260000  256500  258000  120400  247904.34   0   0   0   0
    2015-10-19  258000  261500  257000  260500  89200   250306.52   0   0   0   0
    2015-10-20  258500  260500  257500  259500  93400   249345.65   0   0   0   0
    2015-10-21  260000  262000  259000  260000  93700   249826.08   0   0   0   0
    2015-10-22  259500  260000  250000  251500  192200  241658.69   0   0   0   0
    2015-10-23  252500  254500  249500  250000  147600  240217.39   0   0   0   0
    2015-10-26  252000  255500  251500  254000  160900  244060.87   0   0   0   0
    2015-10-27  254000  258500  251500  252000  149000  242139.13   1   228000  0   0
    2015-10-28  253000  254500  248500  249000  128000  239256.52   0   0   0   0
    2015-10-29  247500  250000  240000  242500  349000  233010.87   1   215050  0   0
    2015-10-30  243500  245500  241000  241000  250200  231569.56   0   0   0   0
    2015-11-02  243500  244000  235500  238500  541300  229167.39   0   0   0   0
    2015-11-03  237000  237500  224500  230000  1054600 221000  0   0   0   0
    2015-11-04  227500  237000  225500  231000  539400  221960.87   0   0   0   0
    2015-11-05  233000  234500  230500  230500  189300  221480.43   0   0   0   0
    2015-11-06  230500  231000  226000  227000  226700  218117.39   0   0   0   1
    2015-11-09  227000  231000  226500  229000  173100  220039.13   0   0   0   0
    2015-11-10  228500  229000  226000  227000  175000  218117.39   0   0   0   0
    2015-11-11  225500  233000  222500  229000  342700  220039.13   0   0   0   0
    2015-11-12  230000  234500  230000  232000  210700  222921.74   0   0   0   0
    2015-11-13  231000  235000  230000  232000  202700  222921.74   0   0   0   0
    2015-11-16  228000  234500  228000  233500  191300  224363.04   0   0   0   0
    2015-11-17  233500  234500  230500  231500  215400  222441.3    0   0   0   0
    2015-11-18  231000  232000  228000  230000  207000  221000  0   0   0   0
    2015-11-19  231500  233500  229500  232000  141900  222921.74   0   0   0   0
    2015-11-20  230000  233500  230000  233000  105600  223882.61   0   0   0   0
    2015-11-23  232000  232500  231000  232500  98700   223402.17   0   0   0   0
    2015-11-24  231000  233500  230500  233000  132100  223882.61   0   0   0   0
    
  4. 我想要的结果是

1) 每天比较收盘价和Cut_Off_Price

2) 如果收盘价低于 Cut_Off_Price,则将 Cut_Off_Signal 设为“1”

3) 并且从第二次出现在 Cut_Off_Price 下,然后忽略。 (我还没有编码,但我打算在“buying_list”中删除这个价格

以上代码没有产生任何 Cut_Off_Signal。能给个建议吗?

4 我尝试解决这个问题,但仍然失败。

    def test(data)
        for i in range(len(list(data.index))):
        data.loc[(data[:i][data['Buying']==1)['Cut_Off_Price']<=data['Close'][i],'Cut_Off_Signal']=1
        return data 

面临的错误信息是 "IndexingError: Unalignalbe boolean Series key provided"

我真的不知道是什么问题。非常感谢您的建议。

【问题讨论】:

    标签: pandas dataframe iteration trading


    【解决方案1】:

    我不太明白您在第 3) 点下的意思,但是对于 1) 和 2),代码可能如下所示:

    data.loc[data.Cut_Off_Price>data.Close, 'Cut_Off_Signal'] = 1
    

    【讨论】:

    • 我想每天查看价格。如果我应用你的建议,那么它会在第一天起作用。那是个问题。
    • 我的代码所做的是,它检查每一行,该行中的收盘价是否小于该行中的 cut_off_price。对于出现这种情况的每一行,它将 1 写入 cut_off_signal 列。对我来说,这听起来就像您要求的那样,因为每一行代表一天。
    猜你喜欢
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    相关资源
    最近更新 更多