{"id":48,"date":"2021-10-05T20:05:30","date_gmt":"2021-10-05T12:05:30","guid":{"rendered":"http:\/\/www.qddlh.com\/?p=48"},"modified":"2021-10-05T20:08:23","modified_gmt":"2021-10-05T12:08:23","slug":"backtrader-%e5%bf%ab%e9%80%9f%e5%bc%80%e5%a7%8b%ef%bc%88%e5%8a%a0%e5%85%a5%e6%95%b0%e6%8d%ae%e6%ba%90%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.qddlh.com\/?p=48","title":{"rendered":"Backtrader-\u5feb\u901f\u5f00\u59cb\uff08\u52a0\u5165\u6570\u636e\u6e90\uff09"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u52a0\u5165\u6570\u636e\u6e90<\/h3>\n\n\n\n<p>\u8d5a\u94b1\u6709\u8da3\uff0c\u901a\u8fc7\u6570\u636e\u5206\u6790\uff0c\u4e0d\u8d39\u529b\u6c14\u8d5a\u94b1\u66f4\u6709\u8da3\u3002\u8fd9\u624d\u662f\u6211\u4eec\u7684\u76ee\u7684\u3002\uff08\u5b98\u65b9\u6587\u6863\u4f7f\u7528\u7684\u65f6\u56fd\u5916\u7684\u5728\u7ebf\u6570\u636e\u3002\u4e3a\u4e86\u65b9\u4fbf\u9a8c\u8bc1\u548c\u903b\u8f91\uff0c\u6211\u8fd9\u91cc\u51c6\u5907\u4e86\u4e00\u4efd\u56fd\u5185600000.SH\u6d66\u53d1\u94f6\u884c\u7684csv\u6570\u636e\uff0c\u53ef\u4ee5\u4e0b\u8f7d\u5230\u672c\u5730\u4f7f\u7528\uff09<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"http:\/\/www.qddlh.com\/wp-content\/uploads\/2021\/07\/600000.SH_stockinfo.csv\">600000.SH_stockinfo<\/a><a href=\"http:\/\/www.qddlh.com\/wp-content\/uploads\/2021\/07\/600000.SH_stockinfo.csv\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p>\u7ee7\u7eed\u5b8c\u5584\u4e0a\u9762\u7684\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from __future__ import (absolute_import, division, print_function,\n                        unicode_literals)\n\nimport datetime  # For datetime objects\nimport os.path  # To manage paths\nimport sys  # To find out the script name (in argv&#91;0])\n\n# Import the backtrader platform\nimport backtrader as bt\n\ndef get_dataframe():\n     # Get a pandas dataframe\n    datapath = '.\/600000.SH_stockinfo.csv'\n    tmpdatapath = '.\/600000.SH_stockinfo_tmp.csv'\n    print('-----------------------read csv---------------------------')\n    dataframe = pd.read_csv(datapath,\n                                skiprows=0,\n                                header=0,\n                                parse_dates=True,\n                                index_col=0)\n    dataframe.trade_date =  pd.to_datetime(dataframe.trade_date, format=\"%Y%m%d\")\n    dataframe&#91;'openinterest'] = '0'\n    feedsdf = dataframe&#91;&#91;'trade_date', 'open', 'high', 'low', 'close', 'vol', 'openinterest']]\n    feedsdf.columns =&#91;'datetime', 'open', 'high', 'low', 'close', 'volume', 'openinterest']\n    feedsdf.set_index(keys='datetime', inplace =True)\n    feedsdf.iloc&#91;::-1].to_csv(tmpdatapath)\n    feedsdf = pd.read_csv(tmpdatapath, skiprows=0, header=0, parse_dates=True, index_col=0)\n    if os.path.isfile(tmpdatapath):\n        os.remove(tmpdatapath)\n        print(tmpdatapath+\" removed!\")\n    return feedsdf\n\nif __name__ == '__main__':\n    # Create a cerebro entity\n    cerebro = bt.Cerebro()\n\n    # Datas are in a subfolder of the samples. Need to find where the script is\n    # because it could have been called from anywhere\n\n    # Get a pandas dataframe\n    #(\u83b7\u53d6dataframe\u683c\u5f0f\u80a1\u7968\u6570\u636e\uff0c\u8fd9\u91cc\u505a\u4e86\u5bf9csv\u683c\u5f0f\u7684\u89e3\u6790\uff0c\u6570\u636e\u8f6c\u6362\u4e3aCerebro\u9700\u8981\u7684\u6570\u636e\u683c\u5f0f)\n    feedsdf = get_dataframe()\n    \n    # Pass it to the backtrader datafeed and add it to the cerebro(\u52a0\u5165\u6570\u636e)\n    data = bt.feeds.PandasData(dataname=feedsdf)\n\n    # Add the Data Feed to Cerebro\n    cerebro.adddata(data)\n\n    # Set our desired cash start\n    cerebro.broker.setcash(100000.0)\n\n    # Print out the starting conditions\n    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())\n\n    # Run over everything\n    cerebro.run()\n\n    # Print out the final result\n    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u6267\u884c\u540e\uff0c\u8f93\u51fa\u5982\u4e0b\uff1a<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Starting Portfolio Value: 1000000.00\nFinal Portfolio Value: 1000000.00<\/code><\/pre>\n\n\n\n<p>\u6837\u4f8b\u4ee3\u7801\u53d8\u957f\uff0c\u8f93\u51fa\u7ed3\u679c\u6ca1\u6709\u53d8\u5316\u3002\u4e2d\u95f4\u589e\u52a0\u4e86\u4ee5\u4e0b\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<p>\u5982\u4f55\u8bfb\u53d6\u672c\u5730\u7684\u6570\u636e\u96c6<\/p>\n\n\n\n<p>\u5982\u4f55\u8f6c\u6362\u6570\u636e\u4e3acerebro\u9700\u8981\u7684\u683c\u5f0f<\/p>\n\n\n\n<p>\u5982\u4f55\u5c06\u6570\u636e\u96c6\u589e\u52a0\u5230cerebro<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u52a0\u5165\u6570\u636e\u6e90 \u8d5a\u94b1\u6709\u8da3\uff0c\u901a\u8fc7\u6570\u636e\u5206\u6790\uff0c\u4e0d\u8d39\u529b\u6c14\u8d5a\u94b1\u66f4\u6709\u8da3\u3002\u8fd9\u624d\u662f\u6211\u4eec\u7684\u76ee\u7684\u3002\uff08\u5b98\u65b9\u6587\u6863\u4f7f\u7528\u7684\u65f6\u56fd\u5916\u7684\u5728\u7ebf\u6570\u636e\u3002\u4e3a\u4e86\u65b9\u4fbf\u9a8c\u8bc1\u548c\u903b\u8f91\uff0c\u6211\u8fd9\u91cc\u51c6\u5907\u4e86\u4e00\u4efd\u56fd\u5185600000.SH\u6d66\u53d1\u94f6\u884c\u7684csv\u6570\u636e\uff0c\u53ef\u4ee5\u4e0b\u8f7d\u5230\u672c\u5730\u4f7f\u7528\uff09 \u7ee7\u7eed\u5b8c\u5584\u4e0a\u9762\u7684\u4ee3\u7801 \u6267\u884c\u540e\uff0c\u8f93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":30,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[11],"class_list":["post-48","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-btuserguide","tag-backtrader"],"_links":{"self":[{"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=48"}],"version-history":[{"count":1,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions\/49"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=\/wp\/v2\/media\/30"}],"wp:attachment":[{"href":"https:\/\/www.qddlh.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qddlh.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}