资源简介
海龟量化策略.py 代码
代码片段和文件信息
import numpy as np
import talib
import math
def getExtremem(arrayHighPriceResult arrayLowPriceResult):
np_arrayHighPriceResult = np.array(arrayHighPriceResult[:-1])
np_arrayLowPriceResult = np.array(arrayLowPriceResult[:-1])
maxResult = np_arrayHighPriceResult.max()
minResult = np_arrayLowPriceResult.min()
return [maxResult minResult]
def getAtrAndUnit(atrArrayResult atrLengthResult portfolioValueResult):
atr = atrArrayResult[atrLengthResult-1]
unit = math.floor(portfolioValueResult * .01 / atr)
return [atr unit]
def getStopPrice(firstOpenPriceResult units_hold_result atrResult):
stopPrice = firstOpenPriceResult - 2*atrResult + (units_hold_result-1)*0.5*atrResult
return stopPrice
def init(context):
context.tradedayNum = 0
context.unit = 0
context.atr = 0
context.tradingSignal = ‘start‘
context.preTradingSignal = ‘‘
context.units_hold_max = 4
context.units_hold = 0
context.quantity = 0
context.max_add = 0
context.firstOpenPrice = 0
context.s = ‘CSI300.INDX‘
update_universe([context.s])
context.openObserveTime = 55;
context.closeObserveTime = 20;
context.atrTime = 20;
def handle_bar(context bar_dict):
portfolioValue = context.portfolio.portfolio_value
highPrice = history(context.openObserveTime+1 ‘1d‘ ‘high‘)[context.s]
lowPriceForAtr = history(context.openObserveTime+1 ‘1d‘ ‘low‘)[context.s]
lowPriceForExtremem = history(context.closeObserveTime+1 ‘1d‘ ‘low‘)[context.s]
closePrice = history(context.openObserveTime+2 ‘1d‘ ‘close‘)[context.s]
closePriceForAtr = closePrice[:-1]
atrArray = talib.ATR(highPrice.values lowPriceForAtr.values closePriceForAtr.values timeperiod=context.atrTime)
maxx = getExtremem(highPrice.values lowPriceForExtremem.values)[0]
minn = getExtremem(highPrice.values lowPriceForExtremem.values)[1]
atr = atrArray[-2]
if (context.tradingSignal != ‘start‘):
if (context.units_hold != 0):
context.max_add += 0.5 * getAtrAndUnit(atrArray atrArray.size portfolioValue)[0]
else:
context.max_add = bar_dict[context.s].last
curPosition = context.portfolio.positions[context.s].quantity
availableCash = context.portfolio.cash
ma
- 上一篇:python3环境搭建教程.ppt
- 下一篇:django商品分页
相关资源
- python与量化投资-从基础到实战 配套资
- 100行python代码 帮你实现同花顺A股下单
- 量化投资:以Python为工具,代码和数
- 极智量化Python语言帮助文档.pdf
- 扫地僧Backtrader给力教程和源码
- 量化交易之路用Python做股票量化分析
- 《Python金融序列量化应用编程指南》
- 量化交易之路之python-源代码.rar
- Python开源量化平台框架支持A股基于
- 零起点Python大数据与量化交易高清书
- 量化投资以python为工具 数据及代码
- python量化交易教程241337
- 《Python与量化投资:从基础到实战(
- 零起点Python大数据与量化交易.pdf.zi
- 量化投资:以Python为工具 高清完整PD
- python量化.pdf
- 量化交易-python案例-简单易懂.zip
- 量化投资:以Python为工具,代码和数
- 量化投资:以Python为工具.蔡立耑(带
- 《量化投资:以python为工具》课后习
- 用Python做股票量化分析量化交易之路
- 量化投资以Python为工具高清
- 《量化投资:以Python为工具》课后习题
- python自动量化交易系统
- API — ffn 0.3.0 中文.pdf
- python做量化:一个简单的均线系统
- Python机器学习与量化投资视频教程
- python量化金融项目视频教程
- 经典动量与反转交易策略python版
- 日内回转交易策略源码
评论
共有 条评论