Python量化交易平台:QMT (日内交易策略示例)

QMT /Ptrade是一款面向个人投资者,尤其是中高净值个人投资者的专业交易系统。系统采用先进的技术框架,具有功能丰富、风控全面、管理灵活、架构精简、高效稳定等核心优势。PTrade终端不仅支持多品种普通交易、日内回转交易、量化交易等场景;还集成了期权组合交易、期权无风险套利、期权风险管理、Alpha对冲套利等多种策略交易工具;对接算法交易平台(日内算法、拆单算法等),满足投资者对交易算法的需求。

Python量化交易平台:QMT /Ptrade(日内交易策略示例)

 

import pandas as pd
import numpy as np
import datetime 

def initialize(context):
    g.cfgfile = get_research_path() + 'demo/data/yangxianstock.csv'
    g.dfconfig = pd.read_csv(g.cfgfile, header=0, index_col='stock_code')
    #股票代码
    g.security = list(g.dfconfig.index)         #取出股票池
    g.buyAmount=list(g.dfconfig.buy_amount)     #取出买入数量
    set_universe(g.security)
    if is_trade():
        log.info('—–trade——-')
    else:
        set_fixed_slippage(0.0)

    
    g.df=pd.DataFrame(0.0,index=g.security,columns =['open','close']).T
    #初始化标志
    g.isRed=[0]*len(g.security)    #是否是阳线
    g.isBuy=[1]*len(g.security)    #是否在卖出后,先遇到阴线,再遇到阳线,默认为1表示首次是阳线就买入不用管阴线

    #20170731 add wangwei 
    g.isOpenGet=[0]*len(g.security)    #是否开盘价已经获取
    
def single_stock(context, data, code,i):
    currentTime = context.blotter.current_dt + datetime.timedelta(hours=8)
    year=context.blotter.current_dt.year
    month=context.blotter.current_dt.month
    day=context.blotter.current_dt.day
    hours=currentTime.hour
    minute=currentTime.minute
    second=currentTime.second
    currentTime=datetime.datetime.combine(datetime.date(year,month,day),datetime.time(hours,minute,second))
    if(currentTime==g.targetBuyTime):
        log.info(code+'—开盘价——-'+str(g.df[code]['open']))
        log.info(code+'—收盘价——-'+str(g.df[code]['close']))
        if(g.df[code]['close']-g.df[code]['open'])>0 and (g.isRed[i]==0) and (g.isBuy[i]==1):
            log.info(code+'—遇到阳线,开仓买入')
            order_value(code, g.buyAmount[i])
            g.isRed[i]=1
            return
        
        if((g.df[code]['close']-g.df[code]['open'])<0 ) and  (g.isBuy[i]==0) :
            log.info(code+'—遇到阴线,不买入')
            g.isBuy[i]=1 #卖出之后,遇到阴线,置为1,下次再遇到阴线之间买入

        
    
    
    yesCLosePrice=g.yesclosedf[code][0]    #昨日收盘价
    highestPrice=yesCLosePrice*1.1      #涨停价
    priceLast=data[code].close          #这一分钟的价格
    log.info(code+'—当前价——-'+str(priceLast))
    log.info(code+'—涨停价——-'+str(highestPrice))
    #如果昨天已经开仓,并且小于涨停价,那么全部卖出
    if(g.isRed[i]==1) and (currentTime==g.targetSellTime) and priceLast<highestPrice:
        log.info(code+'—昨天已经买入,没有涨停,收盘不论今天价格如何都卖出')
        order_target(code,0)
        g.isRed[i]=0
        g.isBuy[i]=0  #卖出之后,需要置为0,等遇到阴线的下一个阳线再置为1
        
def before_trading_start(context, data):
    g.hisdf = get_history(1, '1d', 'low', security_list=g.security,fq='dypre')
    g.yesclosedf = get_history(1, '1d', 'close', security_list=g.security,fq='dypre')
    g.tdopendf = get_history(1, '1d', field='open', security_list=g.security, include=True)
    
def handle_data(context, data):
    
    #交易时才调用,回测需要注释掉
    currentTime = context.blotter.current_dt + datetime.timedelta(hours=8)
    year=context.blotter.current_dt.year
    month=context.blotter.current_dt.month
    day=context.blotter.current_dt.day
    hours=currentTime.hour
    minute=currentTime.minute
    second=currentTime.second
    #格式化当前时间
    currentTime=datetime.datetime.combine(datetime.date(year,month,day),datetime.time(hours,minute,second))
    
    #构造9点31分这个开盘时间
    d=context.blotter.current_dt.strftime("%Y-%m-%d")
    d = datetime.date(year,month,day)
    t = datetime.time(9,31,0) 
    g.targetOpenTime=datetime.datetime.combine(d,t)
    
    #构造14点45分这个卖出时间
    d=context.blotter.current_dt.strftime("%Y-%m-%d")
    d = datetime.date(year,month,day)
    t = datetime.time(14,45,0) 
    g.targetSellTime=datetime.datetime.combine(d,t)
    
    #构造14点50分这个买入时间
    d=context.blotter.current_dt.strftime("%Y-%m-%d")
    d = datetime.date(year,month,day)
    t = datetime.time(14,50,0) 
    g.targetBuyTime=datetime.datetime.combine(d,t)
    
    
    i=0
    #遍历代码池
    for code in g.security:
        if(currentTime<g.targetSellTime):
            yesLowPrice=g.hisdf[code]    #昨日最低价
            priceLast=data[code].close     #这一分钟的价格
            if(g.isRed[i]==1) and (priceLast<yesLowPrice[0]):  #如果最新的价格小于昨日的最低价
                log.info(code+'—昨最低价'+str(yesLowPrice[0]))
                log.info(code+'—当前分钟最新价'+str(priceLast))
                order_target(code,0)
                log.info(code+'—昨日已经买入,当前分钟最新价格小于昨最低价,止损卖出')
                g.isRed[i]=0
                g.isBuy[i]=0  #卖出之后,需要置为0,等遇到阴线的下一个阳线再置为1
              
                
        if(currentTime==g.targetOpenTime):
            g.df[code]['open']=data[code].open    #9点30分算作开盘价
            g.isOpenGet[i]=1
        #如果时间大于开盘时间,直接取开盘价
        if is_trade():
            if(currentTime>g.targetOpenTime) and g.isOpenGet[i]==0:
                log.info(g.tdopendf[code][0])
                g.df[code]['open']=g.tdopendf[code][0]
                g.isOpenGet[i]=1
                log.info(code+'—已经开盘,通过历史函数获取开盘价为'+str(g.df[code]['open']))
    
    
        if(currentTime==g.targetBuyTime):
            g.df[code]['close']=data[code].close  #14点50分算作收盘价
        
        #只有在买入和卖出时间才会进入函数下单动作
        if(currentTime==g.targetBuyTime) or (currentTime==g.targetSellTime):
            single_stock(context, data, code,i)
            
            
        #当天结束后,重置开盘价和收盘价
        if(currentTime>g.targetBuyTime):
            g.df[code]['open']=0.0    
            g.df[code]['close']=0.0
            g.isOpenGet[i]=0

        i=i+1

#:个人投资者30万门槛可申请迅投QMT、恒生PTrade量化交易系统。

来源:v15583721712

物联沃分享整理
物联沃-IOTWORD物联网 » Python量化交易平台:QMT (日内交易策略示例)

发表评论