{"id":62,"date":"2025-09-17T16:32:28","date_gmt":"2025-09-17T08:32:28","guid":{"rendered":"http:\/\/www.028168.xyz\/?p=62"},"modified":"2025-09-17T16:33:00","modified_gmt":"2025-09-17T08:33:00","slug":"%e5%9d%87%e7%ba%bf","status":"publish","type":"post","link":"http:\/\/www.028168.xyz\/index.php\/2025\/09\/17\/%e5%9d%87%e7%ba%bf\/","title":{"rendered":"\u5747\u7ebf\u4ea4\u53c9"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# -*- coding: utf-8 -*-\n&quot;&quot;&quot;\nCreated on Wed Sep 17 15:37:16 2025\n\n@author: cdlei\n&quot;&quot;&quot;\n\n\nimport backtrader as bt\nimport pandas as pd\n\n\nsymbol='515800'\n\ndef read_data(symbol):\n    file=&quot;.\/data\/&quot;+symbol+&quot;.csv&quot;\n    daily_price = pd.read_csv(file,parse_dates=&#x5B;'date'])\n    data=daily_price.set_index('date')\n    return data\n    \n\n\nclass TestStrategy(bt.Strategy):\n    \n     \n    params=(('period1',5),\n           ('period2',10),) \n    \n    def log(self, txt, dt=None):\n       ''' Logging function fot this strategy'''\n       dt = dt or self.datas&#x5B;0].datetime.date(0)\n       print('%s, %s' % (dt.isoformat(), txt))\n    \n    def __init__(self):\n        # \u6253\u5370\u6570\u636e\u96c6\u548c\u6570\u636e\u96c6\u5bf9\u5e94\u7684\u540d\u79f0\n        \n        \n        # print(&quot;-------------self.datas-------------&quot;)\n        # print(self.datas)\n        # print(&quot;-------------self.data-------------&quot;)\n        # print(self.data._name, self.data)\n        # print(&quot;-------------self.data0-------------&quot;)\n        # print(self.data0._name, self.data0)\n        # print(&quot;-------------self.datas&#x5B;0]-------------&quot;)\n        # print(self.datas&#x5B;0]._name, self.datas&#x5B;0])\n        \n        # print(self.data.close)\n        # print(self.data.open)\n        \n        #\u8ba1\u7b97\u5747\u7ebf\n        \n        self.ma1 = bt.indicators.SMA(self.data.close, period=self.p.period1)\n        self.ma2 = bt.indicators.SMA(self.data.close, period=self.p.period2)\n        \n        #\u8ba1\u7b972\u6761\u5747\u7ebf\u4ea4\u53c9\u4fe1\u53f7\uff1ama2 \u4e0a\u7a7f ma1 \u65f6\uff0c\u53d6\u503c\u4e3a +1\uff1b ma2 \u4e0b\u7a7f ma1 \u65f6\uff0c\u53d6\u503c\u4e3a -1\n        self.crossover = bt.indicators.CrossOver(self.ma2, self.ma1) \n        # \u521d\u59cb\u5316\u8ba2\u5355\n        self.order = None\n        \n    def next(self):\n        \n        \n        # \u6253\u5370\u6bcf\u65e5\u7684\u8d44\u91d1\u548c\u6301\u4ed3\u60c5\u51b5\n        print('date', self.data0.datetime.date(0))\n        print('\u5f53\u524d\u53ef\u7528\u8d44\u91d1', self.broker.getcash())\n        print('\u5f53\u524d\u603b\u8d44\u4ea7', self.broker.getvalue())\n        print('\u5f53\u524d\u6301\u4ed3\u91cf', self.broker.getposition(self.data).size)\n        print('\u5f53\u524d\u6301\u4ed3\u6210\u672c', self.broker.getposition(self.data).price)\n       \n        # \u53d6\u6d88\u4e4b\u524d\u672a\u6267\u884c\u7684\u8ba2\u5355\n        if self.order:  \n            self.cancel(self.order)  \n            \n   \n        \n        # \u68c0\u67e5\u662f\u5426\u6709\u6301\u4ed3\n        if not self.position:  \n            # 10\u65e5\u5747\u7ebf\u4e0a\u7a7f5\u65e5\u5747\u7ebf\uff0c\u4e70\u5165\n            if self.crossover &gt; 0:             \n                self.order = self.buy(size=10000) # \u4ee5\u4e0b\u4e00\u65e5\u5f00\u76d8\u4ef7\u4e70\u516510000\u80a1\n        # # 10\u65e5\u5747\u7ebf\u4e0b\u7a7f5\u65e5\u5747\u7ebf\uff0c\u5356\u51fa\n        elif self.crossover &lt; 0:            \n            self.order = self.close() # \u5e73\u4ed3\uff0c\u4ee5\u4e0b\u4e00\u65e5\u5f00\u76d8\u4ef7\u5356\u51fa\n            \n    def notify_order(self, order):\n        # \u672a\u88ab\u5904\u7406\u7684\u8ba2\u5355\n        if order.status in &#x5B;order.Submitted, order.Accepted]:\n            return\n        # \u5df2\u88ab\u5904\u7406\u7684\u8ba2\u5355\n        if order.status in &#x5B;order.Completed, order.Canceled, order.Margin]:\n            if order.isbuy():\n                self.log(\n                    '\u6267\u884c\u4e70\u5165, ref:%.0f\uff0cPrice: %.4f, Size: %.2f, Cost: %.4f, Comm %.4f, Stock: %s' %\n                    (order.ref,\n                     order.executed.price,\n                     order.executed.size,\n                     order.executed.value,\n                     order.executed.comm,\n                     order.data._name))\n            else:  # Sell\n                self.log('\u6267\u884c\u5356\u51fa, ref:%.0f, Price: %.4f, Size: %.2f, Cost: %.4f, Comm %.4f, Stock: %s' %\n                        (order.ref,\n                        order.executed.price,\n                         order.executed.size,\n                        order.executed.value,\n                        order.executed.comm,\n                        order.data._name))\n                  \n    \n#\u4f7f\u7528\u793a\u4f8b\n\nif __name__ == '__main__':\n    \n    #\u8bfb\u53d6\u6570\u636e\n    data=read_data(symbol)   \n    #\u4fee\u6539\u683c\u5f0f\n    datafeed = bt.feeds.PandasData(dataname=data)   \n    cerebro=bt.Cerebro()  \n    #\u6dfb\u52a0\u7b56\u7565\n    cerebro.addstrategy(TestStrategy)   \n    #\u52a0\u8f7d\u6570\u636e\n    cerebro.adddata(datafeed)   \n    #\u8bbe\u7f6e\u521d\u59cb\u8d44\u91d1\n    cerebro.broker.set_cash(100000)\n    \n    #\u8bbe\u7f6e\u6ed1\u70b9\n    #cerebro.broker.set_slippage_perc(perc=0.0001) \n    \n    #\u8fd0\u884c\u56de\u6d4b\n    cerebro.run()\n    \n    #\u8f93\u51fa\u6700\u7ec8\u8d44\u4ea7\n    print(f'\u6700\u7ec8\u8d44\u4ea7\u4ef7\u503c\uff1a{cerebro.broker.getvalue():.2f}')\n    \n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-aside","hentry","category-7","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":2,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":64,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions\/64"}],"wp:attachment":[{"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.028168.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}