Advertisement

金融时间序列

阅读量:
复制代码
 import numpy as np

    
 import pandas as pd

使用cu201711为素材

index:

复制代码
 In [323]: cu201711.index

    
 Out[323]: 
    
 Int64Index([    2,     3,     4,     5,     6,     7,     8,     9,    10,
    
            11,
    
         ...
    
          9992,  9993,  9994,  9995,  9996,  9997,  9998,  9999, 10000,
    
         10001],
    
        dtype='int64', name='序号', length=12609)

重新编号index:

复制代码
    In [337]:cu201711.index=np.arange(12609)
复制代码
复制代码
 in[49]: cu201711.values[1]

    
 Out[49]: 
    
 array(['time', 'open', 'high', 'low', 'close', 'volume', 'amt', 'chg',
    
    'pct_chg', 'oi', 'BIAS', 'BOLL', 'DMI', 'EXPMA', 'KDJ', 'MA',
    
    'MACD', 'RSI'], dtype=object)
复制代码
 In [339]: cu201711.index

    
 Out[339]:
    
 Int64Index([ 0, 1, 2, 3, 4, 5, 6, 7, 8,
    
 9,
    
 ...
    
 12599, 12600, 12601, 12602, 12603, 12604, 12605, 12606, 12607,
    
 12608],
    
 dtype='int64', length=12609)

columns:

复制代码
 In [324]: cu201711.columns

    
 Out[324]: 
    
 Index(['time', 'open', 'high', 'low', 'close', 'volume', 'amt', 'chg',
    
    'pct_chg', 'oi', 'BIAS', 'BOLL', 'DMI', 'EXPMA', 'KDJ', 'MA', 'MACD',
    
    'RSI'],
    
       dtype='object')

通过index取行,index为数字

复制代码
 In [340]: cu201711.loc[3]

    
 Out[340]: 
    
 time       2017-11-01 00:03:00
    
 open                     53660
    
 high                     53660
    
 low                      53620
    
 close                    53630
    
 volume                     780
    
 amt                  209197200
    
 chg                        -30
    
 pct_chg                -0.0559
    
 oi                      159544
    
 BIAS                   -0.0668
    
 BOLL                   53656.9
    
 DMI                    47.8261
    
 EXPMA                  53658.9
    
 KDJ                    13.8889
    
 MA                       53654
    
 MACD                   -3.0436
    
 RSI                          0
    
 Name: 3, dtype: object
复制代码
 In [343]: cu201711.loc[[3,9]]

    
 Out[343]: 
    
               time   open   high    low  close volume        amt  chg  \
    
 3  2017-11-01 00:03:00  53660  53660  53620  53630    780  209197200  -30   
    
 9  2017-11-01 00:09:00  53640  53640  53630  53630     60   16091500  -10   
    
  
    
   pct_chg      oi    BIAS     BOLL      DMI    EXPMA      KDJ     MA    MACD  \
    
 3 -0.0559  159544 -0.0668  53656.9  47.8261  53658.9  13.8889  53654 -3.0436   
    
 9 -0.0186  159496  -0.028  53652.7  42.8571  53642.4       35  53634 -8.6156   
    
  
    
   RSI  
    
 3   0  
    
 9  50
复制代码
 In [345]: cu201711.loc[cu201711.index[1:3]]

    
 Out[345]: 
    
               time   open   high    low  close volume       amt chg  \
    
 1  2017-11-01 00:01:00  53660  53660  53660  53660      2    536600   0   
    
 2  2017-11-01 00:02:00  53660  53660  53650  53660     68  18243100   0   
    
  
    
   pct_chg      oi    BIAS     BOLL DMI    EXPMA     KDJ     MA    MACD  \
    
 1       0  159652 -0.0171  53656.5  35    53665  8.3333  53668  0.0612   
    
 2       0  159678 -0.0171  53657.3  40  53664.2  8.3333  53664   -0.34   
    
  
    
    RSI  
    
 1  33.3333  
    
 2  33.3333

删除0,1行

复制代码
    cu201711.drop([0,1],inplace=True)

time列赋值给index:

复制代码
    cu201711.index=cu201711['time']

删除time列

复制代码
    In [74]: cu201711.drop('time', axis=1,inplace=True)
复制代码
 In [127]: %time cu201711['return'] = np.log(cu201711['close'] / cu201711['close'].shift(1))

    
 Traceback (most recent call last):
    
  
    
   File "<timed exec>", line 1, in <module>
    
  
    
 AttributeError: 'float' object has no attribute 'log'

解决办法:

复制代码
 In [138]: %time cu201711['return'] = np.log(cu201711['close'].astype('int') / cu201711['close'].astype('int').shift(1))

    
 Wall time: 101 ms
    
  
    
 In [139]: cu201711.tail()
    
 Out[139]: 
    
                   open   high    low  close volume        amt  chg  \
    
 time                                                                     
    
 2017-12-01 14:56:00  52970  52990  52960  52980    294   77865600   10   
    
 2017-12-01 14:57:00  52970  52980  52960  52960    302   79984800  -20   
    
 2017-12-01 14:58:00  52950  52970  52930  52940    470  124437000  -20   
    
 2017-12-01 14:59:00  52950  52960  52920  52940    846  223921300    0   
    
 2017-12-01 15:00:00  52950  52950  52950  52950      4    1059000   10   
    
  
    
                 pct_chg      oi    BIAS     BOLL      DMI    EXPMA  \
    
 time                                                                     
    
 2017-12-01 14:56:00  0.0189  161890  0.0456  52915.8  58.9744  52948.5   
    
 2017-12-01 14:57:00 -0.0377  161816  0.0079  52918.5     57.5  52950.3   
    
 2017-12-01 14:58:00 -0.0378  161788 -0.0236  52920.4  59.5238  52948.7   
    
 2017-12-01 14:59:00       0  161764 -0.0189  52922.3  51.2821  52947.4   
    
 2017-12-01 15:00:00  0.0189  161762 -0.0016  52925.4  42.8571  52947.8   
    
  
    
                      KDJ     MA     MACD      RSI  ret_loop    return  
    
 time                                                                       
    
 2017-12-01 14:56:00  70.8995  52962  24.3747  83.3333  0.000189  0.000189  
    
 2017-12-01 14:57:00  71.4286  52966  23.4833  71.4286 -0.000378 -0.000378  
    
 2017-12-01 14:58:00  57.1429  52964  20.9219       50 -0.000378 -0.000378  
    
 2017-12-01 14:59:00  38.0952  52958  18.6767  42.8571  0.000000  0.000000  
    
 2017-12-01 15:00:00  33.3333  52954  17.5025  33.3333  0.000189  0.000189

删除ret_loop列

复制代码
 In [140]: del cu201711['ret_loop']

    
  
    
 In [141]: cu201711.tail()
    
 Out[141]: 
    
                   open   high    low  close volume        amt  chg  \
    
 time                                                                     
    
 2017-12-01 14:56:00  52970  52990  52960  52980    294   77865600   10   
    
 2017-12-01 14:57:00  52970  52980  52960  52960    302   79984800  -20   
    
 2017-12-01 14:58:00  52950  52970  52930  52940    470  124437000  -20   
    
 2017-12-01 14:59:00  52950  52960  52920  52940    846  223921300    0   
    
 2017-12-01 15:00:00  52950  52950  52950  52950      4    1059000   10   
    
  
    
                 pct_chg      oi    BIAS     BOLL      DMI    EXPMA  \
    
 time                                                                     
    
 2017-12-01 14:56:00  0.0189  161890  0.0456  52915.8  58.9744  52948.5   
    
 2017-12-01 14:57:00 -0.0377  161816  0.0079  52918.5     57.5  52950.3   
    
 2017-12-01 14:58:00 -0.0378  161788 -0.0236  52920.4  59.5238  52948.7   
    
 2017-12-01 14:59:00       0  161764 -0.0189  52922.3  51.2821  52947.4   
    
 2017-12-01 15:00:00  0.0189  161762 -0.0016  52925.4  42.8571  52947.8   
    
  
    
                      KDJ     MA     MACD      RSI    return  
    
 time                                                             
    
 2017-12-01 14:56:00  70.8995  52962  24.3747  83.3333  0.000189  
    
 2017-12-01 14:57:00  71.4286  52966  23.4833  71.4286 -0.000378  
    
 2017-12-01 14:58:00  57.1429  52964  20.9219       50 -0.000378  
    
 2017-12-01 14:59:00  38.0952  52958  18.6767  42.8571  0.000000  
    
 2017-12-01 15:00:00  33.3333  52954  17.5025  33.3333  0.000189

全部评论 (0)

还没有任何评论哟~