合肥生活安徽新聞合肥交通合肥房產生活服務合肥教育合肥招聘合肥旅游文化藝術合肥美食合肥地圖合肥社保合肥醫院企業服務合肥法律

        代寫聚寬量化策略 聚寬代碼代寫

        時間:2024-03-28  來源:合肥網hfw.cc  作者:hfw.cc 我要糾錯


        '''
        1、首先計算:
        (1)N日High的最高價HH, N日Close的最低價LC;
        (2)N日Close的最高價HC,N日Low的最低價LL;
        (3)Range = Max(HH-LC,HC-LL)
        (4)BuyLine = Open + K1*Range
        (5)SellLine = Open + K2*Range

        2.構造系統
        (1)當價格向上突破上軌時,如果當時持有空倉,則先平倉,再開多倉;如果沒有持倉,則直接開多倉;
        (2)當價格向下突破下軌時,如果當時持有多倉,則先平倉,再開空倉;如果沒有持倉,則直接開空倉;
        '''

        def initialize(context):
            # 設定滬深300作為基準
            set_benchmark('000300.XSHG')
            # True為開啟動態復權模式,使用真實價格交易
            set_option('use_real_price', True) 
            # 設定成交量比例
            set_option('order_volume_ratio', 1)
            # 關閉訂單提醒
            # log.set_level('order', 'error')
            # 設定期貨保證金比例
            set_option('futures_margin_rate', 0.3)
            # 設定操作金融期貨
            set_subportfolios([SubPortfolioConfig(cash=context.portfolio.cash, type='index_futures')])
            # 金融期貨close_today_commission可不用設定,平今倉默認0.0023
            set_order_cost(OrderCost(open_commission=0.000023, close_commission=0.000023, close_today_commission=0.0023), type='index_futures')
            #運行函數
            run_daily(set_info, time='before_open', reference_security='IF1512.CCFX')
            run_daily(trade, time='every_bar', reference_security='IF1512.CCFX')

        def set_info(context):
            # 分鐘計數
            g.minute_count = 0

        def trade(context):
            # 開盤第一分鐘
            if g.minute_count == 0:
                # 獲取當月可交易的 HS300 股指期貨合約
                g.security = get_stock_index_futrue_code(context,symbol='IF',month='current_month')
                # 獲取 BuyLine, SellLine
                g.BuyLine, g.SellLine = dual_thrust(g.security,n=10,K1=0.5,K2=0.5)
                # 分鐘計數
                g.minute_count += 1
            # 開盤第一分鐘之后
            else:
                # 獲取標的可平多倉
                long_closeable_amount = context.portfolio.long_positions[g.security].closeable_amount
                # 獲取標的可平空倉
                short_closeable_amount = context.portfolio.short_positions[g.security].closeable_amount
                # 獲取標的的最新價
                current_price = attribute_history(g.security, 1, '1m', ['close'], df=False)['close'][0]

                # 當價格向上突破上軌時
                if current_price > g.BuyLine:
                    # 如果當時持有空倉,則先平倉,再開多倉;
                    if(short_closeable_amount>0):
                        # 平空倉
                        order_target(g.security, 0 , side='short')
                        # 開1手多倉
                        order(g.security, 1, side='long')
                        log.info('持有空倉,先平倉,再開多倉')
                    # 如果沒有持倉,則直接開多倉;
                    elif (short_closeable_amount == 0) and (long_closeable_amount == 0):
                        # 開1手多倉
                        order(g.security, 1, side='long')
                        log.info('沒有持倉,開多倉')
                # 當價格向下突破下軌時
                elif current_price < g.SellLine:
                    # 如果當時持有多倉,則先平倉,再開空倉;
                    if (long_closeable_amount>0):
                        # 平多倉
                        order_target(g.security, 0 , side='long')
                        # 開1手空倉
                        order(g.security, 1, side='short')
                        log.info('持有多倉,先平倉,再開空倉')
                    # 如果沒有持倉,則直接開空倉;
                    elif (short_closeable_amount == 0) and (long_closeable_amount == 0):
                        # 開1手空倉
                        order(g.security, 1, side='short')
                        log.info('沒有持倉,則直接開空倉')

                # 分鐘計數
                g.minute_count += 1

        ## 獲取 BuyLine 和 SellLine
        def dual_thrust(security,n,K1,K2):
            hist = attribute_history(security, n, '1d', ['high','low','close','open'], df=False)
            HH = max(hist['high'])
            LC = min(hist['close'])
            HC = max(hist['close'])
            LL = min(hist['low'])
            Open = get_current_data()[security].day_open
            # 獲取 Range
            Range = max((HH-LC),(HC-LL))
            # 計算BuyLine 和 SellLine
            
            BuyLine = Open + K1 * Range
            SellLine = Open - K2 * Range
            # 返回結果
            return BuyLine, SellLine

        ## 獲取當天時間正在交易的股指期貨合約
        def get_stock_index_futrue_code(context,symbol,month='current_month'):
            '''
            獲取當天時間正在交易的股指期貨合約。其中:
            symbol:
                    'IF' #滬深300指數期貨
                    'IC' #中證500股指期貨
                    'IH' #上證50股指期貨
            month:
                    'current_month' #當月
                    'next_month'    #隔月
                    'next_quarter'  #下季
                    'skip_quarter'  #隔季
            '''
            display_name_dict = {'IF':'滬深300指數期貨','IC':'中證500股指期貨','IH':'上證50股指期貨'}
            month_dict = {'current_month':0, 'next_month':1, 'next_quarter':2, 'skip_quarter':3}

            display_name = display_name_dict[symbol]
            n = month_dict[month]
            dt = context.current_dt.date()
            a = get_all_securities(types=['futures'], date=dt)
            try:
                df = a[(a.display_name == display_name) & (a.start_date <= dt) & (a.end_date >= dt)]
                if (len(df)>4) and (month in ('next_quarter','skip_quarter')):
                    return df.index[n+1]
                else:
                    return df.index[n]
            except:
                return 'WARRING: 無此合約'

        # 獲取金融期貨合約到期日
        def get_CCFX_end_date(fature_code):
            return get_security_info(fature_code).end_date



        如有需要,請加QQ:88652583 或微信: 88652583

        掃一掃在手機打開當前頁
      1. 上一篇:代寫CPSC 217、代做python編程設計
      2. 下一篇:菲律賓機場小黑屋 &#160;為什么會被關進菲律賓小黑屋
      3. 無相關信息
        合肥生活資訊

        合肥圖文信息
        急尋熱仿真分析?代做熱仿真服務+熱設計優化
        急尋熱仿真分析?代做熱仿真服務+熱設計優化
        出評 開團工具
        出評 開團工具
        挖掘機濾芯提升發動機性能
        挖掘機濾芯提升發動機性能
        海信羅馬假日洗衣機亮相AWE  復古美學與現代科技完美結合
        海信羅馬假日洗衣機亮相AWE 復古美學與現代
        合肥機場巴士4號線
        合肥機場巴士4號線
        合肥機場巴士3號線
        合肥機場巴士3號線
        合肥機場巴士2號線
        合肥機場巴士2號線
        合肥機場巴士1號線
        合肥機場巴士1號線
      4. 短信驗證碼 酒店vi設計 NBA直播 幣安下載

        關于我們 | 打賞支持 | 廣告服務 | 聯系我們 | 網站地圖 | 免責聲明 | 幫助中心 | 友情鏈接 |

        Copyright © 2025 hfw.cc Inc. All Rights Reserved. 合肥網 版權所有
        ICP備06013414號-3 公安備 42010502001045

        国内精品videofree720| 99久久精品免费精品国产| 日韩精品久久久久久| 国产精品99久久99久久久动漫| 久久精品久久久久观看99水蜜桃| 亚洲av午夜成人片精品网站| 国产亚洲精品AA片在线观看不加载| 无码人妻精品一区二| 亚洲日韩精品无码AV海量| 国产91精品不卡在线| 日韩精品久久不卡中文字幕| 无码国产精品久久一区免费| 97精品人妻系列无码人妻| 91精品国产高清久久久久久91| 99久久国产综合精品1尤物| 久久亚洲精品中文字幕无码| 久久精品国产亚洲网站| 亚洲中文字幕久久精品无码喷水| 精品综合久久久久久88小说| 精品午夜福利1000在线观看| 久久国产精品视频| 久久精品国产黑森林| 精品久久久久中文字幕一区| 国产成人精品一区二三区在线观看| 日韩免费无码一区二区视频| 精品国产日韩一区三区| 亚洲AV日韩综合一区尤物| 亚洲av日韩精品久久久久久a | 国产精品麻豆入口| 一区二区不卡久久精品| d动漫精品专区久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲AV永久无码精品成人| 久久99精品综合国产首页| 青青草国产精品久久| 久久国产精品免费观看| 日韩人妻精品一区二区三区视频| 99久久国产综合精品1尤物| 亚洲欧洲国产精品久久| 国产精品一级香蕉一区| 婷婷射精av这里只有精品|