量化合约(合约量化)系统开发详细及说明丨合约量化(量化合约)开发源码成熟技术

161 阅读3分钟

Quantitative trading refers to the use of modern statistical and mathematical methods, through computer technology to trade investment methods. Quantitative trading selects multiple "high probability" events from massive historical data that can achieve excess returns to formulate strategies, and uses quantitative models to validate and solidify these laws and strategies.

According to the mathematical model, the first step of buying or selling (a computer) at an appropriate time point is completed by a person, and the second step of quantitative contract development is completed by a computer. Usually, the term "quantitative trading robot" refers to the second step: the computer determines the appropriate time to buy or sell based on a pre written program. So we can simply think of computer programs as robots

  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):关于区块链项目技术开发唯:MrsFu123,代币发行、dapp智能合约开发、链游开发、多链钱包开发

  交易所开发、量化合约开发、互助游戏开发、Nft数字藏品开发、众筹互助开发、元宇宙开发、swap开发、

  链上合约开发、ido开发、商城开发等,开发过各种各样的系统模式,更有多种模式、制度、案例、后台等,成熟技术团队,欢迎实体参考。   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):开发智能合约I59系统2OO7开发3O69

  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