合肥生活安徽新聞合肥交通合肥房產(chǎn)生活服務(wù)合肥教育合肥招聘合肥旅游文化藝術(shù)合肥美食合肥地圖合肥社保合肥醫(yī)院企業(yè)服務(wù)合肥法律

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

        時(shí)間:2024-03-28  來源:合肥網(wǎng)hfw.cc  作者:hfw.cc 我要糾錯(cuò)


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

        2.構(gòu)造系統(tǒng)
        (1)當(dāng)價(jià)格向上突破上軌時(shí),如果當(dāng)時(shí)持有空倉,則先平倉,再開多倉;如果沒有持倉,則直接開多倉;
        (2)當(dāng)價(jià)格向下突破下軌時(shí),如果當(dāng)時(shí)持有多倉,則先平倉,再開空倉;如果沒有持倉,則直接開空倉;
        '''

        def initialize(context):
            # 設(shè)定滬深300作為基準(zhǔn)
            set_benchmark('000300.XSHG')
            # True為開啟動(dòng)態(tài)復(fù)權(quán)模式,使用真實(shí)價(jià)格交易
            set_option('use_real_price', True) 
            # 設(shè)定成交量比例
            set_option('order_volume_ratio', 1)
            # 關(guān)閉訂單提醒
            # log.set_level('order', 'error')
            # 設(shè)定期貨保證金比例
            set_option('futures_margin_rate', 0.3)
            # 設(shè)定操作金融期貨
            set_subportfolios([SubPortfolioConfig(cash=context.portfolio.cash, type='index_futures')])
            # 金融期貨close_today_commission可不用設(shè)定,平今倉默認(rèn)0.0023
            set_order_cost(OrderCost(open_commission=0.000023, close_commission=0.000023, close_today_commission=0.0023), type='index_futures')
            #運(yùn)行函數(shù)
            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):
            # 分鐘計(jì)數(shù)
            g.minute_count = 0

        def trade(context):
            # 開盤第一分鐘
            if g.minute_count == 0:
                # 獲取當(dāng)月可交易的 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)
                # 分鐘計(jì)數(shù)
                g.minute_count += 1
            # 開盤第一分鐘之后
            else:
                # 獲取標(biāo)的可平多倉
                long_closeable_amount = context.portfolio.long_positions[g.security].closeable_amount
                # 獲取標(biāo)的可平空倉
                short_closeable_amount = context.portfolio.short_positions[g.security].closeable_amount
                # 獲取標(biāo)的的最新價(jià)
                current_price = attribute_history(g.security, 1, '1m', ['close'], df=False)['close'][0]

                # 當(dāng)價(jià)格向上突破上軌時(shí)
                if current_price > g.BuyLine:
                    # 如果當(dāng)時(shí)持有空倉,則先平倉,再開多倉;
                    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('沒有持倉,開多倉')
                # 當(dāng)價(jià)格向下突破下軌時(shí)
                elif current_price < g.SellLine:
                    # 如果當(dāng)時(shí)持有多倉,則先平倉,再開空倉;
                    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('沒有持倉,則直接開空倉')

                # 分鐘計(jì)數(shù)
                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))
            # 計(jì)算BuyLine 和 SellLine
            
            BuyLine = Open + K1 * Range
            SellLine = Open - K2 * Range
            # 返回結(jié)果
            return BuyLine, SellLine

        ## 獲取當(dāng)天時(shí)間正在交易的股指期貨合約
        def get_stock_index_futrue_code(context,symbol,month='current_month'):
            '''
            獲取當(dāng)天時(shí)間正在交易的股指期貨合約。其中:
            symbol:
                    'IF' #滬深300指數(shù)期貨
                    'IC' #中證500股指期貨
                    'IH' #上證50股指期貨
            month:
                    'current_month' #當(dāng)月
                    'next_month'    #隔月
                    'next_quarter'  #下季
                    'skip_quarter'  #隔季
            '''
            display_name_dict = {'IF':'滬深300指數(shù)期貨','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

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

        合肥圖文信息
        急尋熱仿真分析?代做熱仿真服務(wù)+熱設(shè)計(jì)優(yōu)化
        急尋熱仿真分析?代做熱仿真服務(wù)+熱設(shè)計(jì)優(yōu)化
        出評 開團(tuán)工具
        出評 開團(tuán)工具
        挖掘機(jī)濾芯提升發(fā)動(dòng)機(jī)性能
        挖掘機(jī)濾芯提升發(fā)動(dòng)機(jī)性能
        海信羅馬假日洗衣機(jī)亮相AWE  復(fù)古美學(xué)與現(xiàn)代科技完美結(jié)合
        海信羅馬假日洗衣機(jī)亮相AWE 復(fù)古美學(xué)與現(xiàn)代
        合肥機(jī)場巴士4號線
        合肥機(jī)場巴士4號線
        合肥機(jī)場巴士3號線
        合肥機(jī)場巴士3號線
        合肥機(jī)場巴士2號線
        合肥機(jī)場巴士2號線
        合肥機(jī)場巴士1號線
        合肥機(jī)場巴士1號線
      4. 短信驗(yàn)證碼 酒店vi設(shè)計(jì) deepseek 幣安下載 AI生圖 AI寫作 aippt AI生成PPT 阿里商辦

        關(guān)于我們 | 打賞支持 | 廣告服務(wù) | 聯(lián)系我們 | 網(wǎng)站地圖 | 免責(zé)聲明 | 幫助中心 | 友情鏈接 |

        Copyright © 2025 hfw.cc Inc. All Rights Reserved. 合肥網(wǎng) 版權(quán)所有
        ICP備06013414號-3 公安備 42010502001045

        99久在线精品99re6视频| 国产成人亚洲精品影院| 2020天堂在线亚洲精品专区| 亚洲AV第一页国产精品| 人与狗精品AA毛片| 国产精品福利片免费看| 国产精品麻豆欧美日韩WW| 国产精品va在线观看无| 92国产精品午夜福利| 亚洲mv国产精品mv日本mv| 久久ww精品w免费人成| 亚洲一区精品中文字幕| 久章草在线精品视频免费观看| 国产AV午夜精品一区二区三| 中文字幕一区精品| 精品日本一区二区三区在线观看 | 日韩欧美亚洲中文乱码| 国产伦精品一区二区三区视频小说 | 日韩国产精品视频| 国产精品自拍一区| 99久久人妻无码精品系列蜜桃 | 99视频在线精品免费观看6| 亚洲精品天堂成人片AV在线播放| 亚洲精品第一综合99久久| 99re热久久这里只有精品首页 | 亚洲精品岛国片在线观看| 亚洲av午夜精品一区二区三区| 日韩免费观看的一级毛片| 日韩中文字幕不卡| 精品特级一级毛片免费观看| 四虎精品成人免费视频| 成人国产精品一区二区网站| 亚洲精品乱码久久久久久蜜桃| 亚洲性日韩精品一区二区三区 | 天美传媒精品1区2区3区| 最新国产在线精品观看| 亚洲精品国产成人片| 国产精品186在线观看在线播放| 亚洲第一极品精品无码久久| 久久久亚洲精品视频| 久久99国产综合精品女同|