Warning: file_exists(): open_basedir restriction in effect. File(/www/wwwroot/com.xiximiao.oa/com.cmstop/public/www/wp-content/db.php) is not within the allowed path(s): (/www/wwwroot/com.xiximiao.oa/com.cmstop/public/www/:/tmp/:/proc/:/var/log/nginx/:/www/wwwroot/com.xiximiao.oa/com.cmstop/public/:/www/wwwroot/com.xiximiao.oa/com.cmstop/vendor/:/www/wwwroot/com.xiximiao.oa/com.cmstop/ppk/) in /www/wwwroot/com.xiximiao.oa/com.cmstop/public/www/wp-includes/load.php on line 707
2057 股指期货套利,这段时间都没有交易分享给大家.py » 轻知量化 QMT、PTrade、聚宽策略分享交流平台

2057 股指期货套利,这段时间都没有交易分享给大家.py


# 标题:股指期货套利,这段时间都没有交易分享给大家
# 作者:节点量化
# 回测金额要选择 100万元。

# 导入函数库
from jqdata import *
import datetime as dt
import numpy as np
import datetime

## 初始化函数,设定基准等等
def initialize(context):
    # 设定沪深300作为基准
    set_benchmark('000300.XSHG')
    #交易量设置  开仓量不超过当前25%
    set_option('order_volume_ratio', 0.25)
    #开启动态复权模式(真实价格)
    set_option('use_real_price', True)
    # 过滤掉order系列API产生的比error级别低的log
    log.set_level('order', 'error')
    ### 期货相关设定 ###
    # 设定账户为金融账户
    set_subportfolios([SubPortfolioConfig(cash=context.portfolio.starting_cash, type='index_futures')])
    # 开盘前运行
    run_daily( before_market_open, time='before_open', reference_security='000300.XSHG')
    # 开盘时运行
    run_daily( market_open, time='every_bar')
'''
================================================================================
每天开盘前
================================================================================
'''
#每天开盘前要做的事情
def before_market_open(context):
    #设置股票交易手续费
    set_slip_fee(context)

    #获得当前时间
    date = datetime.date.today()
    now = datetime.datetime.now()
    ye = context.current_dt.date().year
    mo = context.current_dt.date().month
    da = context.current_dt.date().day
    if mo==1:
        ye1=ye
        ye2=ye
        mo1=3
        mo2=6
        mon1 = str(mo1)
        mon2 = str(mo2)
        yea1=str(ye1)[2:4]
        yea2=str(ye2)[2:4]
        g.code1="IF"+yea1+"0"+mon1+".CCFX"
        g.code2="IF"+yea2+"0"+mon2+".CCFX"
    elif mo<=4:
        ye1=ye
        ye2=ye
        mo1=6
        mo2=9
        mon1 = str(mo1)
        mon2 = str(mo2)
        yea1=str(ye1)[2:4]
        yea2=str(ye2)[2:4]
        g.code1="IF"+yea1+"0"+mon1+".CCFX"
        g.code2="IF"+yea2+"0"+mon2+".CCFX"
    elif mo<=7:
        ye1=ye
        ye2=ye
        mo1=9
        mo2=12
        mon1 = str(mo1)
        mon2 = str(mo2)
        yea1=str(ye1)[2:4]
        yea2=str(ye2)[2:4]
        g.code1="IF"+yea1+"0"+mon1+".CCFX"
        g.code2="IF"+yea2+mon2+".CCFX"
    elif mo<=10:
        ye1=ye
        ye2=ye+1
        mo1=12
        mo2=3
        mon1 = str(mo1)
        mon2 = str(mo2)
        yea1=str(ye1)[2:4]
        yea2=str(ye2)[2:4]
        g.code1="IF"+yea1+mon1+".CCFX"
        g.code2="IF"+yea2+"0"+mon2+".CCFX"
    elif mo<=12:
        ye1=ye+1
        ye2=ye+1
        mo1=3
        mo2=6
        mon1 = str(mo1)
        mon2 = str(mo2)
        yea1=str(ye1)[2:4]
        yea2=str(ye2)[2:4]
        g.code1="IF"+yea1+"0"+mon1+".CCFX"
        g.code2="IF"+yea2+"0"+mon2+".CCFX"

#根据不同的时间段设置滑点与手续费
def set_slip_fee(context):
    # 设置期货交易的滑点
    # set_slippage(StepRelatedSlippage(0))
    set_slippage(StepRelatedSlippage(2),type='futures',ref = 'IF') 
    # 根据不同的时间段设置手续费
    today=context.current_dt
    # 设置期货合约保证金和手续费
    #2017-2-17调整平今仓位万分之9.2,2017-9-18起,调整为万分之6.9,2018-12-3起,万分之4.6,2019-4-22起,万分之3.45.买入时万分之0.23,卖出时万分之0.23,
    if today=9.55:
        #计算开仓条件并开仓
        if context.portfolio.positions_value==0:
            if gap.quantile(0.9)>=0 and gap_now>gap.quantile(0.9)*1.1:
                order_target(g.code1, s, side='short',close_today = False)
                order_target(g.code2, s, side='long',close_today = False)
            elif gap.quantile(0.9) < 0 and gap_now>gap.quantile(0.9)*0.9:
                order_target(g.code1, s, side='short',close_today = False)
                order_target(g.code2, s, side='long',close_today = False)
        #计算平仓条件并平仓
        elif gap_now

      
                    2025-02-20                            
⚠️
本站资源大多来自网络,仅供网友学习交流,未经作者或上传书面授权,请勿作他用。
站长 vx: xiangyin615 或者 留言反馈 ,我们将尽快处理。
Notice: When you of the legal rights be violate, please stir to vx: xiangyin615
个人中心
购物车
优惠劵
搜索