<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # MACD quantization trade > 來源:https://uqer.io/community/share/56265ed3f9f06c4ca62fb618 ```py import talib import copy from numpy import arange, array, isnan #本策略的目的在于對常見的MACD指標策略進行驗證 start = '2014-01-01' #start time end = '2015-10-19' #end time benchmark = 'HS300' #universe = ['000001.XSHE', '600000.XSHG'] universe = set_universe('HS300') # 證券池,支持股票和基金 capital_base = 1000000 freq = 'd' refresh_rate = 1 def dea_deviate_from_k_line(k_line, dea): #判斷k線和dea走勢是否背離,主要思路是 #從最新一天開始往回尋找幾個極值點,然后 #進行極值比較 #然后是從當前點到第一個極值點進行比對 #最后按照權重返回對應數值 #對數組進行翻轉 k_line_re = k_line[::-1] dea_re = dea[::-1] k_line_extremum = [] #獲取極值點的索引,只統計近似區間的情況 k_line_extremum_inx = {} dea_extremum = [] dea_extremum_inx = {} for c in xrange(1, len(k_line_re) - 1): if not isnan(k_line_re[c - 1]) and not isnan(k_line_re[c]) and not isnan(k_line_re[c + 1]): if k_line_re[c - 1] < k_line_re[c] and k_line_re[c + 1] < k_line_re[c]: #獲取到極值點 k_line_extremum.append(k_line_re[c]) k_line_extremum_inx[k_line_re[c]] = c for c in xrange(1, len(dea_re) - 1): if not isnan(dea_re[c - 1]) and not isnan(dea_re[c]) and not isnan(dea_re[c + 1]): if dea_re[c - 1] < dea_re[c] and dea_re[c + 1] < dea_re[c]: dea_extremum.append(dea_re[c]) dea_extremum_inx[dea_re[c]] = c #對極值點進行篩選判斷趨勢 sig_extremum = 0 if len(k_line_extremum) >= 2 and len(dea_extremum) >= 2: k_line_first_deviate = k_line_extremum[0] - k_line_extremum[1] dea_first_deviate = dea_extremum[0] - dea_extremum[1] k_and_dea_start_deviate = k_line_extremum_inx[k_line_extremum[0]] - dea_extremum_inx[dea_extremum[0]] k_and_dea_end_deviate = k_line_extremum_inx[k_line_extremum[1]] - dea_extremum_inx[dea_extremum[1]] #k線和dea的同時啟動差距要小于2天 if abs(k_and_dea_start_deviate) < 2: #超過3天的背離趨勢被確定為趨勢 k_deviate_dates = abs(k_line_extremum_inx[k_line_extremum[0]]) - abs(k_line_extremum_inx[k_line_extremum[1]]) dea_deviate_date = abs(dea_extremum_inx[dea_extremum[0]]) - abs(dea_extremum_inx[dea_extremum[1]]) #最好是能同時停止,如果k線和dea線持續時間差值過大,說明應該以大的線為主要趨勢 dea_k_deviate = abs(k_deviate_dates) - abs(dea_deviate_date) if abs(k_deviate_dates) >= 3 and abs(dea_deviate_date) >= 3 and dea_k_deviate < 3: if k_line_first_deviate < 0 and dea_first_deviate > 0 : #k線下降,dea上升說明是變盤上漲信號 sig_extremum = 1; elif k_line_first_deviate > 0 and dea_first_deviate < 0: #k線上升,dea下降說明是變盤下跌信號 sig_extremum = -1 else: #需要看是以那個線為準 if abs(k_deviate_dates) > abs(dea_deviate_date): if k_line_first_deviate < 0: sig_extremum = -1 else: sig_extremum = 1 else: if dea_first_deviate < 0: sig_extremum = -1 else: sig_extremum = 1 return sig_extremum def initialize(account): pass def handle_data(account): fibonacci_sequence_date1 = 144 fibonacci_sequence_date2 = 143 hist = account.get_attribute_history('closePrice', fibonacci_sequence_date1) for stock in account.universe: s_num = 0 for c in hist[stock]: if isnan(c): s_num += 1 if s_num >= len(hist[stock]) - 10: #說明數據太少直接放過 continue macd, macdsignal, macdhist = talib.MACD(hist[stock]) trade_signal = 0 for c in range(fibonacci_sequence_date2, len(macd) - 1): #1.DIFF、DEA均為正,DIFF向上突破DEA,買入信號 if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] > 0 and macdsignal[c] > 0: if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] < macdsignal[c - 1]: if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] > macdsignal[c + 1]: trade_signal += 1 #2.DIFF、DEA均為負,DIFF向下跌破DEA,賣出信號 if not isnan(macd[c]) and not isnan(macdsignal[c]) and macd[c] < 0 and macdsignal[c] < 0: if not isnan(macd[c - 1]) and not isnan(macdsignal[c - 1]) and macd[c - 1] > macdsignal[c - 1]: if not isnan(macd[c + 1]) and not isnan(macdsignal[c + 1]) and macd[c + 1] < macdsignal[c + 1]: trade_signal -= 1 #4.分析MACD柱狀線,由負變正,買入信號 if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]): if macdhist[c] > macdhist[c - 1] and macdhist[c + 1] > macdhist[c] and macdhist[c] > 0 and macdhist[c - 1] < 0: trade_signal += 1 #5.分析MACD柱狀線,由正變負,賣出信號 if not isnan(macdhist[c - 1]) and not isnan(macdhist[c]) and not isnan(macdhist[c + 1]): if macdhist[c] < macdhist[c - 1] and macdhist[c + 1] < macdhist[c] and macdhist[c] < 0 and macdhist[c - 1] > 0: trade_signal -= 1 #3.DEA線與K線發生背離,行情反轉信號 #如果返回0則代表不對信號進行疊加 trade_signal += dea_deviate_from_k_line(hist[stock], macdsignal) if trade_signal > 0 and trade_signal <= 1: order(stock, 100) elif trade_signal > 1: order(stock, 200) elif trade_signal < 0 and trade_signal >= -1: order(stock, -100) elif trade_signal < -1: order(stock, -200) ``` ![](https://box.kancloud.cn/2016-07-30_579cbb0246eab.jpg)
                  <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>

                              哎呀哎呀视频在线观看