利用新闻情感数据炒股 python程序,新闻python,from CAL.PyC
文章由Byrx.net分享于2019-03-23 05:03:36
利用新闻情感数据炒股 python程序,新闻python,from CAL.PyC
from CAL.PyCAL import Datestart = '2010-01-01' # 回测起始时间end = '2015-05-05' # 回测结束时间benchmark = 'HS300' # 策略参考标准universe = set_universe('HS300') # 证券池,支持股票和基金capital_base = 1000000 # 起始资金longest_history = 0 # handle_data 函数中可以使用的历史数据最长窗口长度refresh_rate = 1 # 调仓频率,即每 refresh_rate 个交易日执行一次 handle_data() 函数longest_history = 1def initialize(account): # 初始化虚拟账户状态 account.isBuyPeriod = False account.dayCount = 0def handle_data(account): # 每个交易日的买入卖出指令 account.dayCount += 1 if account.isBuyPeriod: # 每60个工作日(3个月)调仓 hist = account.get_history(longest_history) endDate = Date.fromDateTime(account.current_date) startDate = endDate - 30 res = DataAPI.NewsSentimentIndexGet(secID=account.universe, field=['secID', 'newsPublishDate', 'sentimentIndex'], beginDate=startDate.strftime('%Y%m%d'),endDate=endDate.strftime('%Y%m%d')) res = res.groupby('secID') # top 10% top10 = res.mean().sort('sentimentIndex', ascending=False).head(int(0.1*len(res))) buyList = list(top10.index) print u"%s 买入 : %s" % (endDate, buyList) # 等权重买入 if len(buyList) != 0: singleCash = account.cash / len(buyList) for stock in buyList: approximationAmount = int(singleCash / hist[stock]['closePrice'][-1]/100.0) * 100 order(stock, approximationAmount) account.isBuyPeriod = False account.dayCount = 0 elif account.dayCount == 59: # 调仓日前一日清空当前仓位 for stock in account.valid_secpos: order_to(stock,0) account.isBuyPeriod = True
相关内容
- python读写dbf文件,,读取遍历dBASE或Xb
- 文件下载与进度条,文件下载进度条,演示从指定url下载
- Python删除指定目录下的过期文件,python过期,import osim
- 批量下载豆瓣妹子的图片,豆瓣妹子图片,#!/usr/bin/e
- 利用xlrd模块实现Python读取Excel文档,xlrdpython,# -*- codi
- python3 取计算机名末尾数字+10 拼接成IP地址,python3ip,
- 基础汇编->机器语言翻译器,基础汇编,#! -*-
- Python获取局域网中IP及对应的MAC地址,pythonmac,#!/usr/bi
- Python数据库-链接mongodb带用户验证,pythonmongodb,from pym
- python实现ftp传输,pythonftp传输,import ftpli
评论关闭