{"id":4793,"date":"2024-03-02T12:01:01","date_gmt":"2024-03-02T04:01:01","guid":{"rendered":""},"modified":"2024-03-02T12:01:01","modified_gmt":"2024-03-02T04:01:01","slug":"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565","status":"publish","type":"post","link":"https:\/\/mushiming.com\/4793.html","title":{"rendered":"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565"},"content":{"rendered":"

        \u4f7f\u7528Python\u7684\u7b2c\u4e09\u65b9\u6a21\u5757pandas<\/strong>,matplotlib<\/strong>\u548ctushare<\/strong>(\u83b7\u53d6\u80a1\u7968\u6570\u636e\u7684\u63a5\u53e3)\u6765\u7ed8\u5236\u53cc\u5747\u7ebf\u5e76\u627e\u51fa\u91d1\u53c9\u4e0e\u6b7b\u53c9\u3002<\/h4>\n

        \u53cc\u79fb\u52a8\u5e73\u5747\u7ebf_\u767e\u5ea6\u767e\u79d1<\/p>\n

        \u91d1\u53c9_\u767e\u5ea6\u767e\u79d1<\/p>\n

\u6b7b\u4ea1\u4ea4\u53c9\uff08\u80a1\u5e02\u672f\u8bed\uff09_\u767e\u5ea6\u767e\u79d1     <\/p>\n

    \n
  • \n

    \u9996\u5148\u7ed8\u5236\u53cc\u5747\u7ebf\u6211\u4eec\u8981\u5148\u5f97\u5230\u80a1\u7968\u7684\u6570\u636e\uff0c\u793a\u4f8b\u4e2d\u6211\u7528code=600006(\u4e1c\u98ce\u6c7d\u8f66)\u7684\u80a1\u7968\u4ee3\u7801\u6765\u6f14\u793a\u64cd\u4f5c\u8fc7\u7a0b\u3002<\/h4>\n<\/li>\n<\/ul>\n

    \u9996\u5148\u5bf9\u83b7\u53d6\u7684\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u5904\u7406\uff0c\u5177\u4f53\u4e3a\uff1a<\/p>\n

      \n
    1. \u6539\u53d8date\u5217\u7684\u6570\u636e\u7c7b\u578b<\/li>\n
    2. \u5220\u9664\u6ca1\u6709\u7528\u7684code\u5217<\/li>\n<\/ol>\n

      \u5982\u679c\u60f3\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u628a\u6570\u636e\u6301\u4e45\u5316\u5b58\u50a8<\/p>\n

      df=ts.get_k_data(code='600006',start='2018-01-01') #\u901a\u8fc7\u63a5\u53e3\u83b7\u53d6\u8305\u53f0\u80a1\u7968\u6570\u636e\ndf.drop(labels='code',axis=1,inplace=True) #\u53ef\u4ee5\u5220\u9664\u6ca1\u6709\u7528\u7684\u5217\n# df.to_csv('maotai.csv')  #\u53ef\u5c06\u6570\u636e\u8fdb\u884c\u6301\u4e45\u5316\u5b58\u50a8\n# df=pd.read_csv('maotai.csv')  #\u4f7f\u7528pandas\u6a21\u5757\u8bfb\u53d6\u6570\u636e\ndf['date']=pd.to_datetime(df['date']) #\u66f4\u6539date\u5217\u7684\u6570\u636e\u7c7b\u578b\ndf.set_index('date',inplace=True) #\u628a\u884c\u7d22\u5f15\u8bbe\u7f6e\u4e3adate\u5217\uff0c\u4e14\u76f4\u63a5\u5728\u6e90\u6570\u636e\u4e0a\u8fdb\u884c\u6539\u52a8<\/code><\/pre>\n

      \u7ed8\u5236\u53cc\u5747\u7ebf\uff08\u8fd9\u91cc\u4ee55\u5929\u5747\u7ebf\u548c30\u5929\u5747\u7ebf\u4e3a\u4f8b\uff09\uff1a<\/p>\n

      ma5=df['close'].rolling(5).mean()   #\u6c425\u65e5\u5747\u7ebf\nma30=df['close'].rolling(30).mean() #30\u65e5\u5747\u7ebf\nplt.plot(ma5[30:800])\nplt.plot(ma30[30:800]) #\u53c2\u6570\u53ef\u4ee5\u81ea\u5df1\u8bbe\u5b9a<\/code><\/pre>\n

      \"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565<\/p>\n

        \n
      • \n

         \u6c422018\u5e74\u81f3\u4eca\u8be5\u80a1\u7968\u7684\u91d1\u53c9\u4e0e\u6b7b\u53c9<\/h4>\n<\/li>\n<\/ul>\n

        \u5148\u5bf9\u6570\u636e\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\uff08\u56e0\u4e3a\u957f\u5747\u7ebf\u4e3a30\u5929\uff0c\u56e0\u6b64\u6211\u4eec\u7684\u6570\u636e\u524d30\u4e2a\u503c\u4e3a\u7a7a\uff0c\u9700\u8981\u5220\u9664\uff09<\/p>\n

        ma5=ma5[30:]\nma30=ma30[30:]\ndf=df[30:]<\/code><\/pre>\n

        \u5224\u65ad\u91d1\u53c9\u4e0e\u6b7b\u53c9\uff1a<\/strong><\/p>\n

        s1=ma5<ma30\ns2=ma5>ma30\ndeath=s1 & s2.shift(1) #\u5224\u5b9a\u6b7b\u53c9\u7684\u6761\u4ef6\ndeath_date=df.loc[death].index #\u6b7b\u53c9\u7684\u6240\u6709\u65f6\u95f4\ngolden=~(s1|s2.shift()) #\u5224\u5b9a\u91d1\u53c9\u7684\u6761\u4ef6\ngolden_date=df.loc[golden].index #\u91d1\u53c9\u7684\u6240\u6709\u65f6\u95f4<\/code><\/pre>\n

        s1\u662f\u6211\u4eec\u5224\u65adma5\u5c0f\u4e8ema30\u7684\u60c5\u51b5\uff0c\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u6570\u636e<\/p>\n

        s2\u662fma5\u5927\u4e8ema30\u7684\u60c5\u51b5\uff0c\u540c\u6837\u4e5f\u662f\u8fd4\u56de\u5e03\u5c14\u7c7b\u578b\u7684\u6570\u636e<\/p>\n

        \u5224\u65ad\u6b7b\u53c9\uff1a<\/p>\n

        \"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565<\/p>\n<\/p>\n

         \u901a\u8fc7\u56fe\u7247\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\u628as2\u6574\u4f53\u5411\u4e0b\u79fb\u52a8\u4e00\u683c\uff0c\u4ed6\u4e0es1\u91cd\u53e0\u7684True\u5373\u4f7f\u6211\u4eec\u60f3\u8981\u5f97\u5230\u7684\u6b7b\u53c9\u70b9\u3002<\/p>\n

        \u5373\uff1adeath=s1 & s2.shift(1)<\/p>\n

        \u5224\u65ad\u91d1\u53c9:<\/p>\n

        \"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565<\/p>\n

         \u540c\u6837\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u51fa\uff1as1 | s2.shift(1)\u5f97\u5230\u7684False\u5373\u4f7f\u6211\u4eec\u60f3\u8981\u5f97\u5230\u7684\u91d1\u53c9\u70b9\uff0c\u4f46\u662f\u7531\u4e8e\u76f8\u5173\u5224\u65ad\u6761\u4ef6\u7684\u56e0\u7d20\uff0c\u6211\u4eec\u9700\u8981\u5c06True\u4e0eFalse\u8fdb\u884c\u53cd\u8f6c\uff0c\u5373\uff1agolden=~(s1|s2.shift())<\/p>\n

        \u5982\u6b64\u6211\u4eec\u4fbf\u5f97\u5230\u4e86\u91d1\u53c9\u4e0e\u6b7b\u53c9\u3002<\/p>\n

        \u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

        # -*- coding: utf-8 -*-\nimport pandas as pd\nimport tushare as ts\nimport matplotlib.pyplot as plt\n\ndf=ts.get_k_data(code='600006',start='2018-01-01') #\u901a\u8fc7\u63a5\u53e3\u83b7\u53d6\u8305\u53f0\u80a1\u7968\u6570\u636e\ndf.drop(labels='code',axis=1,inplace=True) #\u53ef\u4ee5\u5220\u9664\u6ca1\u6709\u7528\u7684\u5217\n# df.to_csv('maotai.csv')  #\u53ef\u5c06\u6570\u636e\u8fdb\u884c\u6301\u4e45\u5316\u5b58\u50a8\n# df=pd.read_csv('maotai.csv')  #\u4f7f\u7528pandas\u6a21\u5757\u8bfb\u53d6\u6570\u636e\ndf['date']=pd.to_datetime(df['date']) #\u66f4\u6539date\u5217\u7684\u6570\u636e\u7c7b\u578b\ndf.set_index('date',inplace=True) #\u628a\u884c\u7d22\u5f15\u8bbe\u7f6e\u4e3adate\u5217\uff0c\u4e14\u76f4\u63a5\u5728\u6e90\u6570\u636e\u4e0a\u8fdb\u884c\u6539\u52a8\n\n'''\u7ed8\u52365\u65e5\u5747\u7ebf\u56fe\u4e0e30\u65e5\u5747\u7ebf\u56fe\u6765\u793a\u8303'''\n\nma5=df['close'].rolling(5).mean()   #\u6c425\u65e5\u5747\u7ebf\nma30=df['close'].rolling(30).mean() #30\u65e5\u5747\u7ebf\nplt.plot(ma5[30:800])\nplt.plot(ma30[30:800]) #\u53c2\u6570\u53ef\u4ee5\u81ea\u5df1\u8bbe\u5b9a\n\n\"\"\"\u6c422018\u5e74\u81f3\u4eca\u8be5\u80a1\u7968\u7684\u91d1\u53c9\u4e0e\u6b7b\u53c9\"\"\"\n'''\u56e0\u4e3a\u6211\u4eec\u957f\u5747\u7ebf\u662f30\u65e5\u5747\u7ebf\u6240\u4ee5\u6211\u4eec\u8981\u5148\u5bf9df,ma5,ma30\u8fdb\u884c\u5207\u7247\u64cd\u4f5c'''\nma5=ma5[30:]\nma30=ma30[30:]\ndf=df[30:]\n\ns1=ma5<ma30\ns2=ma5>ma30\ndeath=s1 & s2.shift(1) #\u5224\u5b9a\u6b7b\u53c9\u7684\u6761\u4ef6\ndeath_date=df.loc[death].index #\u6b7b\u53c9\u7684\u6240\u6709\u65f6\u95f4\ngolden=~(s1|s2.shift()) #\u5224\u5b9a\u91d1\u53c9\u7684\u6761\u4ef6\ngolden_date=df.loc[golden].index #\u91d1\u53c9\u7684\u6240\u6709\u65f6\u95f4\nif __name__=='__main__':\n    print(death_date)\n    print(golden_date)<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565Python\u80a1\u7968\u53cc\u5747\u7ebf\u7b56\u7565\uff0c\u7ed8\u5236\u53cc\u5747\u7ebf\u56fe\uff0c\u627e\u5230\u91d1\u53c9\u4e0e\u6b7b\u53c9\u70b9","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4793"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=4793"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/4793\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=4793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=4793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=4793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}