交易模拟,比较2数据框架:Python,Pandas,.loc .where

时间:2016-06-15 10:41:12

标签: pandas dataframe iteration trading

  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. 我的愿望结果是

  5. 1)每天比较收盘价和Cut_Off_Price

    2)如果收盘价低于Cut_Off_Price,则生成Cut_Off_Signal&#34; 1&#34;

    3)并从Cut_Off_Price下的第二次出现,然后忽略。    (我还没编码,但我打算在&#34; buy_list&#34;

    中删除这个价格

    上面的代码没有做任何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 
    

    面临错误信息  &#34; IndexingError:Unalignalbe布尔系列键提供&#34;

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

1 个答案:

答案 0 :(得分:0)

我不太明白你在第3点的意思,但对于1)和2)代码看起来像这样:

Dim SupportTable As TypedTable = MyTypedTable.Clone()
For each row in TableToLoad
    Dim NewTypedRow = SupportTable.NewRow()
    For Each col In Columns
        'Load every column
    Next
    SupportTable.AddTypedRow(NewTypedRow)
Next
TypedTable.Merge(SupportTable)
TypedTable.AcceptChanges()
'Load to database