<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # [技術指標] CMO > 來源:https://uqer.io/community/share/5590d8bdf9f06cb5604f1881 ## 指標介紹 + CMO(Chande Momentum Oscillator)動量震蕩指標是由Tushar S. Chande提出的類似于RSI的指標 + `CMOn`是一個n天滾動指標,在這n天中的第i天計算每天的 收盤價 - 前收盤價 ,如果為正則賦給`upi`(`dni`為0),為負則將絕對值賦給`dni`(`upi`為0) + 其計算公式為: ![](https://box.kancloud.cn/2016-07-30_579cbb0404d63.jpg) ## 策略思路 + 計算上證50成分股當中所有股票過去n天的CMO + CMO大于0時買入,小于0時賣出 + 根據一定的調倉原則進行調倉,細節見代碼 ## 可進一步挖掘的點 + 考慮CMO的形態,如上/下穿0線作為買賣信號 + 擴大股票池范圍,觀察CMO與股票池的關系,比如區分大小盤股觀察CMO的有效性 + 股票權重的分配方式 + 其他調倉原則 ## 先來看看最簡單的情況 ```py import numpy as np start = '2010-01-01' end = '2015-06-20' benchmark = 'SH50' universe = set_universe('SH50') capital_base = 1000000 window = 35 # 參數,CMO指標計算周期 def initialize(account): pass def handle_data(account): clp = account.get_attribute_history('closePrice', window) prc = account.get_attribute_history('preClosePrice', window) p = account.referencePrice # 計算CMO CMO = {} for s in account.universe: diff = clp[s] - prc[s] u = sum(n for n in diff if n > 0) d = sum(-n for n in diff if n < 0) if u + d == 0: continue CMO[s] = (u - d) / (u + d) * 100 # 根據CMO賣出目前持有股票 v = account.cash for s,a in account.valid_secpos.items(): if CMO.get(s, 0) < 0 and s in account.universe: order_to(s, 0) v += a * p[s] # 根據CMO確定買入列表 buylist = [] for s in account.universe: if CMO.get(s, 0) > 0 and not np.isnan(p[s]) and s not in account.valid_secpos: buylist.append(s) # 根據買入列表和可用現金買入股票 if v > account.referencePortfolioValue * 0.33: # 為了避免調倉過于頻繁,僅當可用現金超過賬戶市值1/3時買入 for s in buylist: order(s, v / len(buylist) / p[s]) ``` ![](https://box.kancloud.cn/2016-07-30_579cbb0417324.jpg) 上面的策略實現了策略思路所表述的意思,使用了非常簡單的調倉原則,其表現還不錯 但是,其中的關鍵參數 `window` 為什么設置為 35 呢? 這當然不是拍腦袋拍出來的,而是通過參數調試出來的: ``` window annualized_return sharpe max_drawdown 10 0.0052 -0.1377 0.4944 15 0.1817 0.5284 0.3388 20 0.1925 0.5850 0.3404 25 0.1914 0.5835 0.2956 30 0.2094 0.6053 0.4516 35 0.2156 0.6468 0.3856 40 0.0610 0.0970 0.5264 45 0.1980 0.5728 0.4872 50 0.1730 0.4632 0.5166 ``` 從上面的調試結果中可以看到,當 `window = 35` 時,夏普和最大回撤相對而言最好,因此有了最上面的那個策略 然而調試完了`window`,這個策略就沒有優化空間了嗎?不,我們還可以根據這個策略的表現來分析一下這個策略的缺陷在哪里,并加以改進 因為該策略的調倉原則是買入所有產生的信號,并沒有對持倉進行限制,這會造成兩個方面的影響: 1. 倉位中的股票可能會有很多只,這樣資金會比較分散,削弱信號的效果 1. 如果買入信號比較少,而賣出信號比較多的話,現金的利用率會比較低 那么到底是否存在上述問題呢?我們可以通過最大持倉數量和現金走勢來加以判斷 ```py x = map(len, bt['security_position'].tolist()) max(x) 42 ``` ```py bt.cash.plot() <matplotlib.axes.AxesSubplot at 0x6053cd0> ``` ![](https://box.kancloud.cn/2016-07-30_579cbb042affd.png) 從上面的兩個cell中可以看出這兩個問題還是比較明顯的。為了解決這兩個問題,我們對策略進行優化:一是限制最大持倉位10只股票,二是每次賣出的現金都平均分配給目前倉位中的股票和即將買入的股票 ```py import numpy as np from heapq import nlargest start = '2010-01-01' end = '2015-06-20' benchmark = 'SH50' universe = set_universe('SH50') capital_base = 1000000 max_n = 10 # 參數,最大持倉數量 window = 15 # 參數,CMO指標計算周期 def initialize(account): pass def handle_data(account): clp = account.get_attribute_history('closePrice', window) prc = account.get_attribute_history('preClosePrice', window) p = account.referencePrice # 計算CMO CMO = {} for s in account.universe: diff = clp[s] - prc[s] u = sum(n for n in diff if n > 0) d = sum(-n for n in diff if n < 0) if u + d == 0: continue CMO[s] = (u - d) / (u + d) * 100 # 根據CMO賣出目前持有股票 n = len(account.valid_secpos) sellist = [] for s,a in account.valid_secpos.items(): if CMO.get(s, 0) < 0 and s in account.universe: order_to(s, 0) n -= 1 sellist.append(s) if n >= max_n: # 如果超過最大持倉,則不買入 return # 根據CMO確定買入列表 buylist = [] for s in account.universe: if CMO.get(s, 0) > 0 and not np.isnan(p[s]) and s not in account.valid_secpos: buylist.append(s) # 根據最大持倉數量確定買入列表數量,按CMO排序選較大的部分 if len(buylist) + n > max_n: buylist = nlargest(max_n - n, buylist, key=CMO.get) # 將資金重新分配到新買入的與已持有的股票中 buylist += [s for s in account.valid_secpos if s not in sellist] amount = {} for s in buylist: amount[s] = account.referencePortfolioValue / len(buylist) / p[s] - account.valid_secpos.get(s, 0) # 根據應調數量買賣股票,先賣出后買入 for s in sorted(amount, key=amount.get): order(s, amount[s]) ``` ![](https://box.kancloud.cn/2016-07-30_579cbb043f6e2.jpg) ``` window annualized_return sharpe max_drawdown 10 0.0830 0.1789 0.4101 15 0.2606 0.8783 0.3182 20 0.1726 0.5180 0.3689 25 0.2190 0.6762 0.3508 30 0.2067 0.6376 0.3725 35 0.1676 0.5040 0.3550 40 0.1416 0.4086 0.4478 45 0.1927 0.5926 0.4001 50 0.1183 0.3215 0.4030 ``` 從上面的圖表可以看出其表現相比最初的策略有了不少改善。其中 `window = 15` 是最適合目前調倉原則的參數。 但是這些優化的初衷是為了解決股票數量和資金利用率的問題,我們仍然通過最大持倉數量和現金走勢來判斷 ```py x = map(len, bt['security_position'].tolist()) max(x) 10 ``` ```py bt.cash.plot() <matplotlib.axes.AxesSubplot at 0x5a48fd0> ``` !](img/8DjEAXQMXIa20AAAAASUVORK5CYII=.png) 以上是對CMO這個技術指標進行的一些簡單的回測,并且針對策略本身的特點進行了一定的優化。在最前面列出了一些可挖掘的點,如果想進行更深入的研究還是有很多東西可以做的。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看