Financial document platform

来自cslt Wiki
跳转至: 导航搜索

虚拟交易平台使用手册

  1. 创建平台
    先到目录下/nfs/disk/work/users/tanghui/platform/call_back,把main.py文件拷贝到自己的文件夹下。至此,我们的回测系统已经创建好了。
  2. 使用平台
    1. 总体介绍
      • 首先导入了几个包,它主要是python库和使用这个平台所依赖的包。
      • 然后有三个函数,分别是 'main','simulation','handle_data':
      1. main包含需要使用的回测的基本信息。有起始日期(start),截止日期(end),所用的股票(universe),起始资金(captial_base),一天交易次数(freq不可改),交易方式(refresh_rate不可改)。
      2. simulation主要是根据输入的信息,初始化account虚拟账户类和生成对应的json信息,并在网页上显示。
      3. handle_data是开发者需要编写程序的地方。这个里面有很有用的东西,我们接下来会慢慢介绍它。
    2. mian函数使用
      1. 在main函数中,开发者可以选择起始日期(start),截止日期(end)。日期的格式为‘%Y-%m-%d’,比如:‘2014-01-01’,但不要出现错误日期如:'2014-02-30'。
      2. 所选的股票(universe),可以选择自己所选的股票经行回测,比如 universe: = ['000001.XSHE'],现在只能自己选股。
      3. 起始资金(captial_base)是你的启动资金
      4. 交易方式freq = 'd',表示现在进行的是日间回测,这个结果展示不能改;refresh_rate = 1,表示一天之内handle_data的调用次数。
    3. simulation函数使用
      • 这个主要是初始化相关的类,每天调用handle_data类,计算出结果并将结果写到json中。特别的,如果是实盘测试,先初始化信息,然后再读取历史json信息,之后再执行调用handle_data等步骤。
      1. 首先进行初始化信息。这个开发者不需要去改动。
      2. 如果是实盘测试,这里有个地方需要开发者注意:读取json信息的位置。比如: read_json.get_data(account,result,'data2.json')。这个函数有三个参数,开发者只需关心第三个参数,这个是读取的json的位置,在这个例子中读取的本地文件夹下的‘data2.json’。
      3. 如果不是实盘测试,则不需要上面的步骤。接着就是每个有效日(一年大概250个有效日)去调用一下handle_data函数。这个地方不需要改动。
      4. 然后将生成的信息写入到json文件中。 cj.convert(account,result,"data2.json")。这个就是将对应的信息写入到json中,开发者只需注意第三个参数。这个参数是指明生成的信息存储的位置。
    4. 得到分类列表
      get_classify_stock([str_num1,str_num2])
      list_num = ['101001002009'] #注意:可以有多个值输入
      account.get_classify_stock(list_num)
    5. handle_date函数使用
      • 这个里面主要使用的是account类,这个类中有很多信息供开发者去使用,接下来将会给大家来介绍如何去编写程序回测
      1. account.current_day:
        该变量是模拟测试中今天的日期,string类型的。
      2. account.get_attribute_history(attribute, range) :
        这个函数是获取最近几天的信息,比如:
        his = account.get_attribute_history('closePrice',2)
        结果: his = array([11,12])
        这个是获得最近2天的收盘价,返回的his是一个np.array数组,其中12表示是昨天的收盘价。注意:今天得到最新的信息是昨天的。
        当然,还有其他的使用属性可以使用,比如:
        his = account.get_attribute_history('openPrice',2)
        具体的信息还有'highPrice'(最高价),'lowPrice'(最低价),'volume'(成交量),'value'(成交额)。
        注意:成交量和成交额返回的是list,其他的属性返回的是numpy.ndarray
      3. account.order(stock_code,num)
        这个函数是指定某只股票的买,卖。
        account.order('000001.XSHE',300) 表示000001.XSHE股票买300只
        account.order('000001.XSHE',-300) 表示000001.XSHE股票卖300只
      4. account.order_to(stock_code,num)
        这个函数是指定某只股票的买,卖到num(num >=0)支。
        account.order_to('000001.XSHE',0), 表示000001.XSHE股票卖到0,就是卖空
        account.order_to('000001.XSHE',300), 表示000001.XSHE股票买/卖到300,不管之前有多少只

程序交易规则:

  1. 程序的买和卖都是都是有手续费的。对于买手续费是千分之一,卖是千分之二
  2. 程序的买和卖使用的都是昨天的信息,其中买用的是昨天的开盘价,卖用的是昨天的收盘价
  3. 程序的交易没有使用滑点
  4. 程序中,如果指定买500只股票,但是余额不足的时候,则剩下的钱能买多少就买多少
  5. 如果一只股票要卖500只,但是现在没有这么多股票的时候,则把现在剩下的该只股票卖空

修正的bug:

  1. 修正了收益指数中的年华收益率,年华基准收益率,信息比率,最大回撤的运算规则。
  2. 修正了数据更新的错误

增加的功能:

  1. 增加了开盘涨跌停,不能交易的功能
  2. 增加了买卖量上限的功能,买卖的上限是当天的成交量
  3. 增加了实盘模拟的功能
  4. 增加了得到股票列表的功能

待修正的bug:

  1. 收益指数中波动率,Alpha,Beta三个指标的计算需要改进

潜在需要提高的地方:

  1. 在account类中,增加一个用户可以自己可以的属性

测试Demo

import datetime,time

import numpy as np

import os,sys

sys.path.append("/nfs/disk/work/users/tanghui/platform/call_back")

from Account import Account

from InitData import InitData

from Result import Result

from getData import GetData

from ConvertJson import ConvertJson

from readJson import ReadJson

from set_universe import SetUniverse


def handle_data(account):

   window = 2
   his = account.get_attribute_history('closePrice',window)
   all = account.get_symbol_history(window)
   if not his :
       return
   for stock_code in account.universe:
       all_data = all[stock_code]
       cP = all_data['closePrice']
       oP = all_data['openPrice']
       hP = all_data['highPrice']
       lP = all_data['lowPrice']
       my_data = np.array([cP,oP,hP,lP])
       print account.current_day, my_data,account.universe
       if his.has_key(stock_code)==False:
           continue
       data =  his[stock_code][window-1]
       avr = np.mean(his[stock_code])
       if avr >  his[stock_code][window-1]:
           account.order(stock_code,300)
       else:
           account.order(stock_code,-300)
   return

def simulation(initData):

   account = Account()
   start = time.clock()
   account.init(initData)
   result = Result()
   cj = ConvertJson()
   read_json = ReadJson()
   start_date = datetime.datetime.strptime(initData.start,'%Y-%m-%d')
   end_date = datetime.datetime.strptime(initData.end,'%Y-%m-%d')
   temp_date = start_date
   list_num = ['101001002009']
   print 'classify:',account.get_classify_stock(list_num)
   while temp_date <= end_date:
       start = time.clock()
       str_temp_date = temp_date.strftime('%Y-%m-%d')
       account.current_day = str_temp_date
       temp_date = temp_date + datetime.timedelta(days=1)
       if account.idxmap['datetime'][account.bench_code].has_key(account.current_day) == False
           continue
       handle_data(account)
       if account.record.has_key(account.current_day) == True:
           account.update_daily_account()
   result.calculate(account)
if __name__ == '__main__':
   start = '2010-05-01'
   end = '2012-01-01'
   universe = ['000001.ZICN']
   #universe = SetUniverse.set_universe("HS300")
   #print 'main_universe:',universe
   benchmark = 'HS300'
   captial_base = 100000
   freq = 'd'
   refresh_rate = 1
   initData = InitData(start,end,universe,benchmark,captial_base,freq,refresh_rate) 
   simulation(initData)

版本:1.3