均线策略---使用quartz实现策略

本文介绍了一个量化投资策略案例,包括设置回测区间从2008年1月1日至2015年4月23日,参考标准为上证50指数,股票池为华泰柏瑞上证50ETF,初始资金10万元。策略采用双均线交叉方法进行买卖决策,短期均线窗口长度20,长期均线窗口长度120。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

start = datetime(2008, 1, 1)                # 回测起始时间
end  = datetime(2015, 4, 23)                # 回测结束时间
benchmark = 'SH50'                          # 策略参考标准
universe = ['510050.XSHG']  # 股票池
capital_base = 100000     # 起始资金
commission = Commission(0.0,0.0)

window_short = 20
window_long = 120
longest_history = window_long
SD = 0.05

def initialize(account):                    # 初始化虚拟账户状态
    account.fund = universe[0]
    account.SD = SD
    account.window_short = window_short
    account.window_long = window_long

def handle_data(account):             # 每个交易日的买入卖出指令
    hist = account.get_history(longest_history)
    fund = account.fund
    short_mean = np.mean(hist[fund]['closePrice'][-account.window_short:]) # 计算短均线值
    long_mean = np.mean(hist[fund]['closePrice'][-account.window_long:])   #计算长均线值

    # 计算买入卖出信号
    flag = True if (short_mean - long_mean) > account.SD * long_mean else False 
    if flag:
        if account.position.secpos.get(fund, 0) == 0:
            # 空仓时全仓买入,买入股数为100的整数倍
            approximationAmount = int(account.cash / hist[fund]['closePrice'][-1]/100.0) * 100
            order(fund, approximationAmount)
    else:
        # 卖出时,全仓清空
        if account.position.secpos.get(fund, 0) >= 0:
            order_to(fund, 0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值