<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國際加速解決方案。 廣告
                # 5.16 DualTrust 策略和布林強盜策略 > 來源:https://uqer.io/community/share/564737ddf9f06c4446b48133 誰能夠幫忙實現DualTrust策略和布林強盜策略(BollingerBandit)?@薛昆Kelvin @lookis: DualTrust: ```py start = '2014-01-01' # 回測起始時間 end = '2015-01-01' # 回測結束時間 benchmark = 'HS300' # 策略參考標準 universe = set_universe("CYB") # 證券池,支持股票和基金 capital_base = 100000 # 起始資金 freq = 'm' # 策略類型,'d'表示日間策略使用日線回測,'m'表示日內策略使用分鐘線回測 refresh_rate = 1 # 調倉頻率,表示執行handle_data的時間間隔,若freq = 'd'時間間隔的單位為交易日,若freq = 'm'時間間隔為分鐘 def initialize(account): # 初始化虛擬賬戶狀態 account.k1 = 0.7 account.k2 = 0.7 account.cache = {} account.holding_max = 10 account.holding = 0 account.buy_sell_line = {} pass def handle_data(account): # 每個交易日的買入賣出指令 #準備數據 if not account.current_date.strftime('%Y%m%d') in account.cache: account.cache = {} account.cache[account.current_date.strftime('%Y%m%d')] = account.get_daily_history(1) if account.current_minute == "09:30": return #每天畫一次線 if account.current_minute == "09:31": account.buy_sell_line = {} for stock in account.cache[account.current_date.strftime('%Y%m%d')]: if stock in account.universe: close = account.cache[account.current_date.strftime('%Y%m%d')][stock]["closePrice"][0] low = account.cache[account.current_date.strftime('%Y%m%d')][stock]["lowPrice"][0] high = account.cache[account.current_date.strftime('%Y%m%d')][stock]["highPrice"][0] o = account.referencePrice[stock] r = max(high - low, close - low) account.buy_sell_line[stock] = {"buy": o + account.k1 * r, "sell": o - account.k2 * r} else: #每天剩余的時間根據畫線買賣 for stock in account.buy_sell_line: if stock in account.universe and stock in account.referencePrice and stock in account.valid_secpos: if account.referencePrice[stock] < account.buy_sell_line[stock]["sell"]: order_to(stock, 0) account.holding -= 1 for stock in account.buy_sell_line: if stock in account.universe and stock in account.referencePrice and not stock in account.valid_secpos: if account.holding < account.holding_max and account.referencePrice[stock] > account.buy_sell_line[stock]["buy"]: account.holding += 1 order_pct(stock, 1.0/account.holding_max) return ``` 回測看效果不是特別好…… LZ自己調一下參數吧 @JasonYichuan: BollingerBandit很一般,不過沒怎么調參數,看著辦吧 ```py import numpy as np import pandas as pd start = '2015-01-01' # 回測起始時間 end = '2015-11-26' # 回測結束時間 benchmark = 'HS300' # 策略參考標準 universe = set_universe('HS300') # 證券池,支持股票和基金 capital_base = 100000 # 起始資金 #commission = Commission(buycost=0.00025,sellcost=0.00025) # 傭金 freq = 'd' # 策略類型,'d'表示日間策略使用日線回測,'m'表示日內策略使用分鐘線回測 refresh_rate = 1 # 調倉頻率 # 全局參數 ## Boll線參數 N = 20 k = 2 ## ROC變動率參數 M = 20 ## 平倉參數 E = 20 def initialize(account): # 初始化虛擬賬戶狀態 # 持股代碼以及持股時間 account.duration = pd.DataFrame(np.array([0]*len(universe)), index=universe, columns=['duration']) account.amount = 400 def handle_data(account): # 每個交易日的買入賣出指令 hist = account.get_attribute_history('closePrice',50) ticker_name = [] # 符合買入要求股票代碼 for stk in account.universe: # 遍歷股票池內所有股票,選出符合要求的股票 if np.isnan(account.referencePrice[stk]) or account.referencePrice[stk] == 0: # 停牌或是還沒有上市等原因不能交易 continue # 計算股票的BOLL線上下軌 ## 計算MA MA = np.mean(hist[stk][-N:]) ## 計算標準差MD MD = np.sqrt((sum(hist[stk][-N:] - MA)**2) / N) ## 計算MB、UP、DN線 MB =np.mean(hist[stk][-(N-1):]) UP = MB + k * MD DN = MB - k * MD # 計算股票的ROC ROC = float(hist[stk][-1] - hist[stk][-M])/float(hist[stk][-M]) # 開倉條件 if (hist[stk][-1] > UP) and (ROC > 0): ticker_name.append(stk) # 若股票符合開倉條件且尚未持有,則買入 for stk in ticker_name: if stk not in account.valid_secpos: order(stk,account.amount) account.duration.loc[stk]['duration'] = 1 # 對于持有的股票,若股票不符合平倉條件,則將持倉時間加1,否則賣出,并刪除該持倉時間記錄 for stk in account.valid_secpos: T = max(E - account.duration.loc[stk]['duration'],10) if hist[stk][-1] > np.mean(hist[stk][-T:]): account.duration.loc[stk]['duration'] = account.duration.loc[stk]['duration'] + 1 else: order_to(stk,0) account.duration.loc[stk]['duration'] = 0 return ```
                  <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>

                              哎呀哎呀视频在线观看