价值投资与量化交易
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
量化投资多因子选股策略:
import numpy as np #导入python 计算模块
start = '2014-08-01'
end = '2016-08-01'
benchmark = 'HS300'
capital_base = 1000000
freq = 'd'
refresh_rate = 1
universe = StockScreener( Factor.LFLO.nsmall(30)) #用选股器选择流通市值最小的30只股票作为股票池
def initialize(account):
pass
def handle_data(account):
#生成买入列表
last_date = account.previous_date.strftime("%Y-%m-%d") #获取上一个交易日
last_screener = universe.preview(last_date) #获取上一个交易日市值最小的30只股票
buylist = [sec for sec in last_screener if sec in account.universe]
v = account.referencePortfolioValue
d = len(buylist)
#卖出不在买入列表中的股票,估计持仓价值
for stock in account.valid_secpos:
if stock not in buylist:
if stock in account.universe:
order_to(stock,0)
else:
v -= account.valid_secpos[stock] * account.referencePrice[stock] #获得调仓数量
change = {}
for stock in buylist:
p = account.referencePrice[stock]
if p and not np.isnan(p):
change[stock] = int(v / d / p) - account.valid_secpos.get(stock,0)
#按先卖后买的顺序发出指令
for stock in sorted(change,key=change.get):
if change[stock] <= -100 or change[stock] >= 100:
order(stock,change[stock])
量化投资:期货日内策略
from datetime import datetime
import numpy as np
###策略初始化函数
universe = ['RBM0'] #策略交易的期货合约
start = "2015-11-01"
end = "2016-12-28"
capital_base = 1e6
refresh_rate = 5
freq = 'm'
comission = {'RB':(0.000025, 'perValue')}
slippage = Slippage(0, 'perValue')
amount =20
accounts ={'fantasy_account': AccountConfig(account_type='futures', capital_base=100000,commission=commission,slippage=slippage)}
def initialize(context):
context.high = np.NAN
context.low = np.NAN
context.close = np.NAN
context.count1 = 0
context.count2 = 0
context.keys = [u'openPrice', u'highPrice', u'lowPrice', u'closePrice', u'tradeDate', u'turnoverVol']
def handle_data(context):
futures_account = context.get_account('fantasy_account')
symbol = context.get_symbol(universe[0])
current_position = futures_account.get_position(symbol)
if current_position:
long_position = current_position.long_amount
short_position = current_position.short_amount
else:
long_position = 0
short_position = 0
current_time = context.now.strftime('%H:%M:%S')
#记录当日开盘的高开低收数据
if current_time =='09:30:00':
yester_data = DataAPI.MktFutdGet(tradeDate=context.previous_date, ticker=symbol, field=[u'closePrice',u'highestPrice',u'lowestPrice'],pandas= "1")
context.high = yester_data['highestPrice'].iat[0]
context.low = yester_data['lowestPrice'].iat[0]
context.close = yester_data['closePrice'].iat[0]
#盘中逻辑