zoukankan      html  css  js  c++  java
  • python数据分析——双均线策略

    import tushare as ts
    import pandas as pd
    from pandas import DataFrame,Series
    df = pd.read_csv('maotai.csv',index_col='date',parse_dates=['date'])
    df.drop(labels='Unnamed: 0',axis=1,inplace=True)
    df
    
    Out[1]:
     openclosehighlowvolumecode
    date      
    2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519
    2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519
    2001-08-29 5.777 5.684 5.781 5.640 53252.75 600519
    2001-08-30 5.668 5.796 5.860 5.624 48013.06 600519
    2001-08-31 5.804 5.782 5.877 5.749 23231.48 600519
    2001-09-03 5.812 5.779 5.870 5.757 22112.09 600519
    2001-09-04 5.782 5.852 5.949 5.762 37006.77 600519
    2001-09-05 5.876 5.849 5.924 5.813 26066.95 600519
    2001-09-06 5.835 5.734 5.854 5.704 28997.03 600519
    2001-09-07 5.702 5.574 5.773 5.570 31552.25 600519
    2001-09-10 5.531 5.734 5.757 5.470 18878.89 600519
    2001-09-11 5.749 5.670 5.773 5.656 11390.81 600519
    2001-09-12 5.520 5.621 5.656 5.515 25045.19 600519
    2001-09-13 5.626 5.607 5.671 5.577 10986.68 600519
    2001-09-14 5.626 5.671 5.702 5.593 7672.83 600519
    2001-09-17 5.637 5.599 5.670 5.546 8983.97 600519
    2001-09-18 5.606 5.663 5.710 5.601 10773.26 600519
    2001-09-19 5.671 5.768 5.768 5.634 8650.53 600519
    2001-09-20 5.765 5.720 5.788 5.702 11173.35 600519
    2001-09-21 5.668 5.634 5.718 5.624 7879.72 600519
    2001-09-24 5.634 5.632 5.685 5.624 4068.60 600519
    2001-09-25 5.668 5.660 5.709 5.632 3488.45 600519
    2001-09-26 5.642 5.637 5.699 5.624 4956.26 600519
    2001-09-27 5.637 5.734 5.777 5.624 8778.04 600519
    2001-09-28 5.765 5.795 5.812 5.702 17088.47 600519
    2001-10-08 5.781 5.715 5.809 5.663 6552.17 600519
    2001-10-09 5.718 5.827 5.859 5.718 9558.52 600519
    2001-10-10 5.827 5.640 5.848 5.629 17548.69 600519
    2001-10-11 5.626 5.585 5.702 5.570 12306.84 600519
    2001-10-12 5.609 5.624 5.726 5.320 20010.70 600519
    ... ... ... ... ... ... ...
    2019-01-16 658.000 661.100 665.650 653.600 42083.00 600519
    2019-01-17 663.000 659.250 669.700 655.800 30284.00 600519
    2019-01-18 665.000 683.610 690.200 661.500 68609.00 600519
    2019-01-21 690.000 686.980 698.880 681.880 47051.00 600519
    2019-01-22 686.000 671.000 686.000 665.220 42122.00 600519
    2019-01-23 667.220 665.660 674.500 663.130 33624.00 600519
    2019-01-24 670.300 672.500 677.830 664.680 29155.00 600519
    2019-01-25 675.880 679.700 684.900 669.930 28986.00 600519
    2019-01-28 684.000 677.000 693.980 676.030 36102.00 600519
    2019-01-29 677.500 685.000 688.000 668.000 30342.00 600519
    2019-01-30 684.990 676.000 686.220 676.000 22699.00 600519
    2019-01-31 680.000 689.600 689.600 677.560 36696.00 600519
    2019-02-01 697.040 692.670 699.000 689.610 30520.00 600519
    2019-02-11 695.000 725.300 729.500 688.180 58140.00 600519
    2019-02-12 723.000 717.920 725.490 715.200 35091.00 600519
    2019-02-13 723.000 735.250 736.160 717.220 35677.00 600519
    2019-02-14 731.200 737.160 743.660 730.140 32258.00 600519
    2019-02-15 734.880 724.780 734.880 723.020 23237.00 600519
    2019-02-18 731.000 740.000 744.950 731.000 37745.00 600519
    2019-02-19 740.960 730.760 742.500 726.660 29501.00 600519
    2019-02-20 735.000 722.000 735.000 716.060 37882.00 600519
    2019-02-21 720.000 720.250 730.480 717.000 29850.00 600519
    2019-02-22 717.000 726.010 728.490 714.100 25908.00 600519
    2019-02-25 730.750 742.330 744.440 717.990 82622.00 600519
    2019-02-26 747.800 727.350 749.180 726.000 57964.00 600519
    2019-02-27 733.000 740.700 758.020 731.000 65942.00 600519
    2019-02-28 737.900 755.010 763.420 736.950 56986.00 600519
    2019-03-01 761.500 789.300 790.000 761.000 63840.00 600519
    2019-03-04 795.000 781.860 798.330 777.220 84585.00 600519
    2019-03-05 785.000 779.780 789.550 775.880 44830.00 600519

    4174 rows × 6 columns

    In [6]:
    # 收盘价的5日均值和30日均值
    ma5 = df['close'].rolling(5).mean()
    ma30 =  df['close'].rolling(30).mean()
    df['ma5'] = ma5
    df['ma30'] = ma30
    
     

    s1 = ma5 < ma30 T->F金叉 F->T死叉 s2 = ma5 >= ma30 s1 T T F F T T F F
    s2 F F T T F F T T T F T T T F T F T F F F T F

    ~(s1 | s2.shift(1))

    In [9]:
    # 金叉时间
    s1 = ma5 < ma30 
    s2 = ma5 >= ma30 
    df.loc[~(s1 | s2.shift(1))].index
    
    Out[9]:
    DatetimeIndex(['2001-08-27', '2001-08-28', '2001-08-29', '2001-08-30',
                   '2001-08-31', '2001-09-03', '2001-09-04', '2001-09-05',
                   '2001-09-06', '2001-09-07',
                   ...
                   '2017-07-24', '2017-09-18', '2017-12-15', '2018-03-16',
                   '2018-05-09', '2018-07-18', '2018-07-25', '2018-09-20',
                   '2018-12-04', '2019-01-03'],
                  dtype='datetime64[ns]', name='date', length=120, freq=None)
    In [11]:
    # 死叉时间
    df.loc[s1&s2.shift(1)].index
    
    Out[11]:
    DatetimeIndex(['2002-01-17', '2002-01-30', '2002-03-29', '2002-07-29',
                   '2002-12-27', '2003-03-17', '2003-04-22', '2003-06-20',
                   '2003-06-30', '2003-08-04', '2004-02-27', '2004-05-11',
                   '2004-06-07', '2004-08-20', '2004-11-23', '2005-04-20',
                   '2005-05-16', '2005-06-15', '2005-09-27', '2006-07-10',
                   '2006-07-31', '2006-08-24', '2006-09-13', '2007-02-08',
                   '2007-04-23', '2007-05-09', '2007-07-12', '2007-09-12',
                   '2007-11-12', '2007-11-22', '2008-01-31', '2008-03-18',
                   '2008-05-23', '2008-08-12', '2008-12-31', '2009-03-12',
                   '2009-04-30', '2009-08-20', '2009-09-02', '2009-10-20',
                   '2009-12-18', '2010-01-22', '2010-02-26', '2010-06-23',
                   '2010-10-15', '2010-11-02', '2010-12-24', '2011-03-02',
                   '2011-03-30', '2011-09-08', '2011-12-08', '2012-07-24',
                   '2012-08-02', '2012-08-15', '2012-09-21', '2012-11-07',
                   '2012-12-25', '2013-01-18', '2013-03-18', '2013-06-21',
                   '2013-07-12', '2013-10-25', '2013-11-26', '2013-12-04',
                   '2014-04-01', '2014-04-30', '2014-08-22', '2014-09-16',
                   '2014-10-13', '2014-11-21', '2015-01-19', '2015-06-17',
                   '2015-07-17', '2015-09-28', '2015-11-26', '2015-12-10',
                   '2016-01-05', '2016-08-05', '2016-08-18', '2016-11-21',
                   '2017-07-06', '2017-09-08', '2017-11-29', '2018-02-05',
                   '2018-03-27', '2018-06-28', '2018-07-23', '2018-07-31',
                   '2018-10-15', '2018-12-25'],
                  dtype='datetime64[ns]', name='date', freq=None)
    In [12]:
    #如果我从假如我从2010年1月1日开始,初始资金为100000元,金叉尽量买入,死叉全部卖出,则到今天为止,我的炒股收益率如何?
    df = df['2010':'2019']
    df
    
    Out[12]:
     openclosehighlowvolumecodema5ma30
    date        
    2010-01-04 109.760 108.446 109.760 108.044 44304.88 600519 108.5402 109.306267
    2010-01-05 109.116 108.127 109.441 107.846 31513.18 600519 108.4178 109.256500
    2010-01-06 107.840 106.417 108.165 106.129 39889.03 600519 107.8512 109.191000
    2010-01-07 106.417 104.477 106.691 103.302 48825.55 600519 107.1672 108.971067
    2010-01-08 104.655 103.379 104.655 102.167 36702.09 600519 106.1692 108.833000
    2010-01-11 104.400 102.926 105.230 102.422 24461.03 600519 105.0652 108.749633
    2010-01-12 103.028 105.708 106.040 102.492 31063.40 600519 104.5814 108.573933
    2010-01-13 104.649 103.022 105.389 102.741 37924.44 600519 103.9024 108.237867
    2010-01-14 103.379 107.552 107.974 103.379 46454.64 600519 104.5174 108.076200
    2010-01-15 107.533 108.401 110.641 107.533 45938.50 600519 105.5218 107.990467
    2010-01-18 108.484 109.110 109.926 108.420 21461.53 600519 106.7586 107.992167
    2010-01-19 109.116 108.337 109.441 108.165 17818.91 600519 107.2844 107.848367
    2010-01-20 108.427 105.881 108.580 105.804 20972.95 600519 107.8562 107.664600
    2010-01-21 105.842 106.397 107.450 105.613 17257.48 600519 107.6252 107.535267
    2010-01-22 106.314 104.738 106.755 103.309 25432.94 600519 106.8926 107.316367
    2010-01-25 104.560 105.957 106.761 103.704 23239.15 600519 106.2620 107.127700
    2010-01-26 107.380 106.378 108.593 105.038 32889.16 600519 105.8702 106.964333
    2010-01-27 105.951 104.643 107.068 104.400 19316.57 600519 105.6226 106.774167
    2010-01-28 104.566 107.974 108.708 104.336 30267.52 600519 105.9380 106.769933
    2010-01-29 108.452 107.552 108.612 107.004 37172.82 600519 106.5008 106.816700
    2010-02-01 107.769 107.776 108.216 106.576 29655.94 600519 106.8646 106.893933
    2010-02-02 107.208 106.263 108.484 106.117 15493.53 600519 106.8416 106.867333
    2010-02-03 106.066 105.887 107.272 104.783 23034.65 600519 107.0904 106.847333
    2010-02-04 105.868 107.591 108.006 105.376 22475.33 600519 107.0138 106.866900
    2010-02-05 106.959 109.282 109.684 106.570 26234.30 600519 107.3598 106.841367
    2010-02-08 109.282 109.269 112.058 108.816 31496.10 600519 107.6584 106.858167
    2010-02-09 109.760 109.193 110.609 108.822 14151.24 600519 108.2444 106.873300
    2010-02-10 109.760 109.652 110.137 108.931 6398.14 600519 108.9974 106.886700
    2010-02-11 109.633 110.641 111.318 109.505 14945.05 600519 109.6074 106.978167
    2010-02-12 111.018 110.456 111.164 109.888 9346.40 600519 109.8422 107.047733
    ... ... ... ... ... ... ... ... ...
    2019-01-16 658.000 661.100 665.650 653.600 42083.00 600519 640.0660 596.694000
    2019-01-17 663.000 659.250 669.700 655.800 30284.00 600519 648.1620 598.594667
    2019-01-18 665.000 683.610 690.200 661.500 68609.00 600519 657.7080 601.045000
    2019-01-21 690.000 686.980 698.880 681.880 47051.00 600519 670.1840 604.217667
    2019-01-22 686.000 671.000 686.000 665.220 42122.00 600519 672.3880 607.017667
    2019-01-23 667.220 665.660 674.500 663.130 33624.00 600519 673.3000 609.965333
    2019-01-24 670.300 672.500 677.830 664.680 29155.00 600519 675.9500 612.949000
    2019-01-25 675.880 679.700 684.900 669.930 28986.00 600519 675.1680 615.972333
    2019-01-28 684.000 677.000 693.980 676.030 36102.00 600519 673.1720 618.480667
    2019-01-29 677.500 685.000 688.000 668.000 30342.00 600519 675.9720 621.548667
    2019-01-30 684.990 676.000 686.220 676.000 22699.00 600519 678.0400 624.398000
    2019-01-31 680.000 689.600 689.600 677.560 36696.00 600519 681.4600 627.686667
    2019-02-01 697.040 692.670 699.000 689.610 30520.00 600519 684.0540 631.515667
    2019-02-11 695.000 725.300 729.500 688.180 58140.00 600519 693.7140 636.542333
    2019-02-12 723.000 717.920 725.490 715.200 35091.00 600519 700.2980 641.559333
    2019-02-13 723.000 735.250 736.160 717.220 35677.00 600519 712.1480 647.134333
    2019-02-14 731.200 737.160 743.660 730.140 32258.00 600519 721.6600 652.846667
    2019-02-15 734.880 724.780 734.880 723.020 23237.00 600519 728.0820 658.336667
    2019-02-18 731.000 740.000 744.950 731.000 37745.00 600519 731.0220 664.236667
    2019-02-19 740.960 730.760 742.500 726.660 29501.00 600519 733.5900 668.928333
    2019-02-20 735.000 722.000 735.000 716.060 37882.00 600519 730.9400 673.029000
    2019-02-21 720.000 720.250 730.480 717.000 29850.00 600519 727.5580 677.370667
    2019-02-22 717.000 726.010 728.490 714.100 25908.00 600519 727.8040 681.504333
    2019-02-25 730.750 742.330 744.440 717.990 82622.00 600519 728.2700 686.065667
    2019-02-26 747.800 727.350 749.180 726.000 57964.00 600519 727.5880 690.151000
    2019-02-27 733.000 740.700 758.020 731.000 65942.00 600519 731.3280 694.303667
    2019-02-28 737.900 755.010 763.420 736.950 56986.00 600519 738.2800 698.845000
    2019-03-01 761.500 789.300 790.000 761.000 63840.00 600519 750.9380 703.959000
    2019-03-04 795.000 781.860 798.330 777.220 84585.00 600519 758.8440 709.201000
    2019-03-05 785.000 779.780 789.550 775.880 44830.00 600519 769.3300 713.194333

    2221 rows × 8 columns

    In [13]:
    df['ma5']=df['close'].rolling(5).mean()
    df['ma30']=df['close'].rolling(30).mean()
    
     
    C:jupyterlibsite-packagesipykernel_launcher.py:1: SettingWithCopyWarning: 
    A value is trying to be set on a copy of a slice from a DataFrame.
    Try using .loc[row_indexer,col_indexer] = value instead
    
    See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
      """Entry point for launching an IPython kernel.
    C:jupyterlibsite-packagesipykernel_launcher.py:2: SettingWithCopyWarning: 
    A value is trying to be set on a copy of a slice from a DataFrame.
    Try using .loc[row_indexer,col_indexer] = value instead
    
    See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
      
    
    In [15]:
    sr1 = df['ma5'] < df['ma30']
    sr2 = df['ma5'] >= df['ma30']
    death_cross = df[sr1 & sr2.shift(1)].index
    golden_cross = df[~(sr1 | sr2.shift(1))].index
    
    In [16]:
    first_money = 100000
    money = first_money
    hold = 0#持有多少股
    sr1 = pd.Series(1, index=golden_cross)
    sr2 = pd.Series(0, index=death_cross)
    #根据时间排序
    sr = sr1.append(sr2).sort_index()
    
    for i in range(0, len(sr)):
        p = df['open'][sr.index[i]] # 开盘价
        if sr[i] == 1:
            #金叉
            buy = (money // (100 * p))
            hold += buy*100
            money -= buy*100*p
        else:
            money += hold * p
            hold = 0
    
            
    p = df['open'][-1]
    now_money = hold * p + money
    
    print(now_money - first_money)
    
     
    1086009.8999999994
  • 相关阅读:
    CSS3_02之2D、3D动画
    CSS3_01之选择器、Hack
    css_04之显示、定位
    css_03之文本、浮动
    css_02之盒模型、渐变
    javascript获取非行间样式的方法(可以兼容各个浏览器)
    gulp的用法
    grunt的用法
    grunt
    git用法
  • 原文地址:https://www.cnblogs.com/bilx/p/11644700.html
Copyright © 2011-2022 走看看