动态周期的移动均线改写出错?请教!


(189) #1
克隆策略
In [31]:
import pandas as pd 
import numpy as np
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open','amount'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(['000001.SZA','600600.SHA','600519.SHA'], start_date, end_date,fields=['list_date','open', 'high', 'low', 'close', 'amount'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池
df=df.sort_values(['instrument_x','date']).reset_index(drop=True)

def get_lowest_days(price,window):
    return price.rolling(window).apply(lambda x:window-np.argmin(x)-1)
df['低点']=get_lowest_days(df['close_x'],10)
df['动态均线周期']=df['低点']+2#选择过去10天的最低点距离今天的天数+2为均线周期,比如最低点在今天,则周期是2天线,最低点昨天则3天线等
#求动态周期的均线(pd.rolling_mean会出错,因为window必须是INT)
def get_chma(df):
    for i in range(len(df)):
        sum_temp,count_p=0,0#计数器,一个计算求和天数,一个计算累计close求和
        while count_p<=df.ix(i,'动态均线周期'):
            count_p+=1
            sum_temp+=df.ix[-(len(df)-i-cout_p),'close_x']#累加
        df.ix[i,'动态均线']=sum_temp/df.ix(i,'动态均线周期')
    return df

df=df.apply(get_chma)

        
df.ix[:,['instrument_x','close_x','低点','动态均线周期','动态均线']].tail()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-31-39133cbd5147> in <module>()
     25     return df
     26 
---> 27 df=df.get_chma
     28 
     29 

AttributeError: 'DataFrame' object has no attribute 'get_chma'

(189) #2
克隆策略
In [1]:
import pandas as pd 
import numpy as np
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open','amount'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(['000001.SZA','600600.SHA','600519.SHA'], start_date, end_date,fields=['list_date','open', 'high', 'low', 'close', 'amount'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池
df=df.sort_values(['instrument_x','date']).reset_index(drop=True)

def LLVBARS(price,window):
    return price.rolling(window).apply(lambda x:window-np.argmin(x)-1)
df['低点X']=LLVBARS(df.close_x,10)+2

def MA_ch(ser_,peri_):#变动周期均线
    ser_.fillna(0)
    peri_.fillna(0)
    lst_p=list(peri_)
    ma_ch=[]
    for i in lst_p:#低点X的list遍历
        ma_ch.append(pd.rolling_mean(ser_, int(i)))
    return ma_ch
df['ma_']=MA_ch(df.close_x,df.低点X)

df
Out[1]:
high_x open_x close_x instrument_x amount_x date low_x list_date high_y open_y close_y instrument_y amount_y low_y 低点X
0 1436.101196 1436.101196 1420.622192 000001.SZA 2.351547e+09 2015-05-04 1399.983643 1991-04-03 4795.915039 4757.637207 4787.741211 000300.SHA 5.654534e+11 4699.397949 NaN
1 1420.622192 1411.162842 1363.866211 000001.SZA 3.685372e+09 2015-05-05 1332.908325 1991-04-03 4785.191895 4785.191895 4596.836914 000300.SHA 6.600202e+11 4572.982910 NaN
2 1401.703613 1360.426392 1351.827026 000001.SZA 3.107566e+09 2015-05-06 1332.048340 1991-04-03 4700.909180 4626.230957 4553.327148 000300.SHA 5.860340e+11 4511.761230 NaN
3 1365.586060 1351.827026 1347.527222 000001.SZA 1.968942e+09 2015-05-07 1332.908325 1991-04-03 4546.337891 4520.817871 4470.088867 000300.SHA 4.271900e+11 4467.458984 NaN
4 1374.185425 1350.967041 1363.006226 000001.SZA 2.163109e+09 2015-05-08 1332.908325 1991-04-03 4559.058105 4515.554199 4558.403809 000300.SHA 4.520907e+11 4445.591797 NaN
5 1386.224609 1358.706543 1378.485107 000001.SZA 3.349067e+09 2015-05-11 1332.048340 1991-04-03 4690.954102 4582.090820 4690.532227 000300.SHA 5.832604e+11 4535.148926 NaN
6 1391.384277 1374.185425 1372.465576 000001.SZA 3.166864e+09 2015-05-12 1345.807373 1991-04-03 4748.003906 4692.122070 4747.420898 000300.SHA 6.071233e+11 4660.016113 NaN
7 1397.403809 1391.384277 1365.586060 000001.SZA 2.807446e+09 2015-05-13 1353.546875 1991-04-03 4758.001953 4746.712891 4718.436035 000300.SHA 5.904798e+11 4678.845215 NaN
8 1380.205078 1375.905273 1366.445923 000001.SZA 2.283605e+09 2015-05-14 1349.247192 1991-04-03 4735.284180 4717.955078 4700.777832 000300.SHA 5.132197e+11 4670.167969 NaN
9 1365.586060 1358.706543 1328.608643 000001.SZA 2.718885e+09 2015-05-15 1313.129639 1991-04-03 4690.754883 4690.754883 4617.470215 000300.SHA 5.066950e+11 4592.049805 2.0
10 1320.009155 1307.110107 1305.390137 000001.SZA 2.327558e+09 2015-05-18 1287.331421 1991-04-03 4623.382812 4582.058105 4575.140137 000300.SHA 4.340831e+11 4565.061035 2.0
11 1353.546875 1304.530273 1340.647705 000001.SZA 2.959557e+09 2015-05-19 1294.210938 1991-04-03 4740.004883 4577.637207 4731.222168 000300.SHA 5.242322e+11 4577.637207 3.0
12 1388.804443 1376.765259 1353.546875 000001.SZA 4.115473e+09 2015-05-20 1341.507690 1991-04-03 4843.100098 4751.571777 4754.918945 000300.SHA 5.899563e+11 4745.209961 4.0
13 1366.445923 1358.706543 1357.846558 000001.SZA 2.214025e+09 2015-05-21 1343.227539 1991-04-03 4841.615234 4768.688965 4840.976074 000300.SHA 5.053461e+11 4746.028809 5.0
14 1401.703613 1368.165894 1399.123779 000001.SZA 4.233676e+09 2015-05-22 1360.426392 1991-04-03 4951.991211 4895.924805 4951.334961 000300.SHA 7.191299e+11 4870.598145 6.0
15 1436.961182 1406.003296 1427.501831 000001.SZA 4.518317e+09 2015-05-25 1406.003296 1991-04-03 5099.838867 4949.109863 5099.495117 000300.SHA 7.570858e+11 4949.109863 7.0
16 1436.101196 1430.081543 1429.221680 000001.SZA 4.607494e+09 2015-05-26 1399.983643 1991-04-03 5199.294922 5140.797852 5198.917969 000300.SHA 8.022958e+11 5063.145996 8.0
17 1454.159912 1429.221680 1417.182495 000001.SZA 3.888992e+09 2015-05-27 1410.302979 1991-04-03 5226.844238 5216.169922 5181.434082 000300.SHA 7.778263e+11 5126.860840 9.0
18 1419.762329 1418.902344 1332.908325 000001.SZA 4.404155e+09 2015-05-28 1326.888672 1991-04-03 5213.183105 5174.101074 4834.006836 000300.SHA 8.732074e+11 4831.329102 10.0
19 1347.527222 1333.768188 1317.429321 000001.SZA 3.103544e+09 2015-05-29 1299.370605 1991-04-03 4924.303223 4839.529785 4840.829102 000300.SHA 6.360081e+11 4665.520996 11.0
20 1374.185425 1318.289307 1367.305908 000001.SZA 3.365459e+09 2015-06-01 1306.250122 1991-04-03 5079.318848 4862.755859 5076.179199 000300.SHA 6.378437e+11 4835.773926 3.0
21 1367.305908 1366.445923 1356.126709 000001.SZA 2.991565e+09 2015-06-02 1334.628174 1991-04-03 5162.556152 5091.271973 5161.870117 000300.SHA 6.584528e+11 5047.081055 4.0
22 1371.605591 1356.126709 1361.286377 000001.SZA 3.103861e+09 2015-06-03 1332.908325 1991-04-03 5186.966797 5176.597168 5143.589844 000300.SHA 6.720565e+11 5059.495117 5.0
23 1425.781860 1363.006226 1407.723145 000001.SZA 5.971846e+09 2015-06-04 1346.667358 1991-04-03 5194.232910 5156.012207 5181.416016 000300.SHA 7.351707e+11 4863.464844 6.0
24 1444.700562 1436.101196 1401.703613 000001.SZA 4.681142e+09 2015-06-05 1378.485107 1991-04-03 5288.339844 5254.904785 5230.551758 000300.SHA 8.552244e+11 5104.178223 7.0
25 1500.596802 1403.423462 1486.837646 000001.SZA 8.596942e+09 2015-06-08 1392.244263 1991-04-03 5370.608887 5259.408203 5353.750977 000300.SHA 9.494980e+11 5202.595215 8.0
26 1500.596802 1492.857300 1460.179565 000001.SZA 5.601776e+09 2015-06-09 1434.381348 1991-04-03 5380.425781 5379.470215 5317.460938 000300.SHA 8.714318e+11 5251.208008 9.0
27 1455.879883 1444.700562 1434.381348 000001.SZA 3.679461e+09 2015-06-10 1421.482178 1991-04-03 5374.829590 5254.304199 5309.111816 000300.SHA 7.115093e+11 5209.679688 10.0
28 1442.120728 1434.381348 1417.182495 000001.SZA 2.811190e+09 2015-06-11 1406.003296 1991-04-03 5329.294922 5305.140625 5306.589844 000300.SHA 6.605849e+11 5248.162598 11.0
29 1430.081543 1417.182495 1419.762329 000001.SZA 3.131997e+09 2015-06-12 1402.563477 1991-04-03 5351.647949 5329.278320 5335.115234 000300.SHA 6.942131e+11 5283.085938 10.0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1821 51.650024 51.650024 50.813519 600600.SHA 8.559708e+07 2017-09-21 50.780716 1993-08-27 3860.797607 3843.257812 3837.817627 000300.SHA 1.441329e+11 3837.384033 2.0
1822 50.961140 50.829922 50.501881 600600.SHA 6.838984e+07 2017-09-22 50.190243 1993-08-27 3842.251953 3826.458984 3837.730469 000300.SHA 1.283609e+11 3816.822510 2.0
1823 50.469078 50.469078 50.173840 600600.SHA 6.231608e+07 2017-09-25 50.124634 1993-08-27 3835.389404 3829.218994 3817.791504 000300.SHA 1.359802e+11 3811.119141 2.0
1824 50.337860 50.173840 50.042625 600600.SHA 3.857503e+07 2017-09-26 49.878605 1993-08-27 3821.692871 3810.333252 3820.781494 000300.SHA 1.010363e+11 3808.382812 2.0
1825 50.255852 49.944210 50.124634 600600.SHA 4.140206e+07 2017-09-27 49.812996 1993-08-27 3832.565918 3818.964600 3821.199951 000300.SHA 1.092995e+11 3818.814697 3.0
1826 50.354263 50.124634 50.173840 600600.SHA 4.847072e+07 2017-09-28 49.862202 1993-08-27 3827.330811 3821.590332 3822.538574 000300.SHA 1.156463e+11 3813.753174 4.0
1827 50.600292 50.173840 50.534687 600600.SHA 4.556769e+07 2017-09-29 50.042625 1993-08-27 3852.223633 3825.202148 3836.501221 000300.SHA 1.160568e+11 3825.202148 5.0
1828 51.272778 50.698704 51.010345 600600.SHA 1.042153e+08 2017-10-09 50.698704 1993-08-27 3918.839111 3910.114990 3882.207520 000300.SHA 1.719415e+11 3876.032227 6.0
1829 51.305580 51.125160 51.157963 600600.SHA 7.848040e+07 2017-10-10 51.010345 1993-08-27 3900.741699 3883.003662 3889.863770 000300.SHA 1.579797e+11 3855.466553 7.0
1830 51.994469 51.141560 51.568012 600600.SHA 1.108751e+08 2017-10-11 51.043148 1993-08-27 3913.069580 3886.818359 3902.686768 000300.SHA 1.441527e+11 3885.497070 8.0
1831 51.912457 51.256374 51.321983 600600.SHA 9.095938e+07 2017-10-12 51.108757 1993-08-27 3917.443604 3902.015625 3912.953613 000300.SHA 1.231472e+11 3897.547119 9.0
1832 55.110855 52.486526 53.880703 600600.SHA 4.782177e+08 2017-10-13 52.486526 1993-08-27 3927.174316 3912.975098 3921.001709 000300.SHA 1.143149e+11 3908.888428 10.0
1833 54.110329 53.601868 53.470650 600600.SHA 1.634975e+08 2017-10-16 52.814568 1993-08-27 3947.423828 3926.275391 3913.446289 000300.SHA 1.508238e+11 3913.322510 11.0
1834 53.437847 53.437847 52.650547 600600.SHA 8.366728e+07 2017-10-17 52.502930 1993-08-27 3924.614014 3910.789307 3913.068848 000300.SHA 1.067085e+11 3900.475830 11.0
1835 53.060600 52.716156 52.535732 600600.SHA 7.468923e+07 2017-10-18 52.371716 1993-08-27 3944.410645 3914.815186 3944.162598 000300.SHA 1.264204e+11 3914.815186 11.0
1836 54.175938 52.634148 53.552662 600600.SHA 1.442491e+08 2017-10-19 51.994469 1993-08-27 3944.034912 3940.583008 3931.249512 000300.SHA 1.331899e+11 3923.645508 11.0
1837 53.405041 53.241020 52.847374 600600.SHA 6.580626e+07 2017-10-20 52.289703 1993-08-27 3928.960693 3918.526611 3926.852051 000300.SHA 9.452963e+10 3910.197510 11.0
1838 53.372238 52.666950 53.175415 600600.SHA 6.745890e+07 2017-10-23 52.617744 1993-08-27 3936.272949 3930.891357 3930.798096 000300.SHA 1.002809e+11 3920.656250 11.0
1839 53.798691 53.388641 53.273827 600600.SHA 6.232117e+07 2017-10-24 52.748962 1993-08-27 3959.495117 3926.229980 3959.395264 000300.SHA 1.201921e+11 3922.028809 10.0
1840 53.405041 53.273827 52.962189 600600.SHA 7.076242e+07 2017-10-25 52.552135 1993-08-27 3978.089355 3958.166992 3976.948975 000300.SHA 1.068203e+11 3954.001221 11.0
1841 53.962711 53.060600 53.208218 600600.SHA 9.463869e+07 2017-10-26 53.060600 1993-08-27 4013.598877 3980.047852 3993.575195 000300.SHA 1.704886e+11 3973.553467 8.0
1842 53.880703 53.306629 53.798691 600600.SHA 1.080451e+08 2017-10-27 52.896580 1993-08-27 4024.464600 3992.553955 4021.967529 000300.SHA 1.529748e+11 3991.354004 9.0
1843 55.078049 53.782288 54.963238 600600.SHA 1.901969e+08 2017-10-30 52.896580 1993-08-27 4033.339355 4022.164062 4009.721680 000300.SHA 1.934898e+11 3960.269775 10.0
1844 57.013493 54.372761 56.701851 600600.SHA 2.646409e+08 2017-10-31 54.143135 1993-08-27 4009.282715 3995.393555 4006.718018 000300.SHA 1.428972e+11 3983.256592 11.0
1845 57.210316 56.390213 55.980164 600600.SHA 1.970787e+08 2017-11-01 55.438896 1993-08-27 4032.458008 4007.911621 3996.622070 000300.SHA 1.664771e+11 3991.323486 10.0
1846 56.062172 55.553711 55.668522 600600.SHA 6.798904e+07 2017-11-02 55.242069 1993-08-27 3999.226318 3994.094482 3997.134277 000300.SHA 1.519814e+11 3971.376221 11.0
1847 56.521431 55.750534 54.864822 600600.SHA 9.769105e+07 2017-11-03 54.667999 1993-08-27 3999.337402 3992.349609 3992.697998 000300.SHA 1.581421e+11 3963.026367 9.0
1848 55.734131 54.996040 55.684925 600600.SHA 1.793459e+08 2017-11-06 53.946308 1993-08-27 4021.638184 3989.182373 4020.889648 000300.SHA 1.589119e+11 3975.406982 10.0
1849 57.899200 55.274876 57.538357 600600.SHA 2.568668e+08 2017-11-07 55.274876 1993-08-27 4064.217773 4022.401855 4054.246582 000300.SHA 1.939910e+11 4021.364014 11.0
1850 57.505550 57.210316 56.537830 600600.SHA 1.269236e+08 2017-11-08 56.258999 1993-08-27 4089.145752 4051.493408 4048.005615 000300.SHA 2.009497e+11 4040.577148 11.0

1851 rows × 15 columns

又写了一个,请教出错问题,右边的预览显示不对


(189) #3
克隆策略
In [2]:
import pandas as pd 
import numpy as np
from pandas import Series,DataFrame
start_date = '2015-05-01' # 开始日期
end_date = '2017-11-08' # 结束日期
bm_price = D.history_data(['000300.SHA'], start_date, end_date, fields=['close','low','high','open','amount'])
df = D.history_data(D.instruments(),start_date,end_date,fields=['in_sse50'])
instruments = list(set(df[df['in_sse50']==1]['instrument']))
stock_raw_data = D.history_data(['000001.SZA','600600.SHA','600519.SHA'], start_date, end_date,fields=['list_date','open', 'high', 'low', 'close', 'amount'])
df = pd.merge(stock_raw_data,bm_price, how="outer", on="date")#合并指数基准及股票池
df=df.sort_values(['instrument_x','date']).reset_index(drop=True)

def LLVBARS(price,window):
    return price.rolling(window).apply(lambda x:window-np.argmin(x)-1)
df['低点X']=LLVBARS(df.close_x,10)+2
df=df.fillna(0)
df['低点X']=df['低点X'].map(lambda x:int(x))


def MA_ch(ser_,peri_):#变动周期均线
    malist=list(peri_)
    ma_=[]
    for i in range(len(malist)):
        ma_.append(pd.rolling_mean(ser_,malist[i]))
    return  ma_   

df['ma_']=MA_ch(df.close_x,df.低点X)

df.ix[:,['instrument_x','close_x','低点X','ma_']].head(30)
Out[2]:
instrument_x close_x 低点X ma_
0 000001.SZA 1420.622192 0 None
1 000001.SZA 1363.866211 0 None
2 000001.SZA 1351.827026 0 None
3 000001.SZA 1347.527222 0 None
4 000001.SZA 1363.006226 0 None
5 000001.SZA 1378.485107 0 None
6 000001.SZA 1372.465576 0 None
7 000001.SZA 1365.586060 0 None
8 000001.SZA 1366.445923 0 None
9 000001.SZA 1328.608643 2 None
10 000001.SZA 1305.390137 2 None
11 000001.SZA 1340.647705 3 None
12 000001.SZA 1353.546875 4 None
13 000001.SZA 1357.846558 5 None
14 000001.SZA 1399.123779 6 None
15 000001.SZA 1427.501831 7 None
16 000001.SZA 1429.221680 8 None
17 000001.SZA 1417.182495 9 None
18 000001.SZA 1332.908325 10 None
19 000001.SZA 1317.429321 11 None
20 000001.SZA 1367.305908 3 None
21 000001.SZA 1356.126709 4 None
22 000001.SZA 1361.286377 5 None
23 000001.SZA 1407.723145 6 None
24 000001.SZA 1401.703613 7 None
25 000001.SZA 1486.837646 8 None
26 000001.SZA 1460.179565 9 None
27 000001.SZA 1434.381348 10 None
28 000001.SZA 1417.182495 11 None
29 000001.SZA 1419.762329 10 None

问提搞定了一半!
还有一半没搞定


(189) #4

已经搞定了,不用劳烦大神了