持久化在量化策略中的重要性与应用

大观量化
大观量化 以大见小,以小入微

0 People liked this article · 1145 views

策略在运行过程中产生的一些数据信息,比如股票池、账户信息、订单信息,这些数据是存储在内存中的,如果遇到服务器异常或者策略优化更新之后重启策略,这些数据都会丢失,导致策略运行失真或报错。持久化即是将这些数据存储到硬盘中,这样数据就不会丢失,量化策略要稳定运行必须进行持久化处理。

 

以下举例使用pickle进行持久化的示例代码,需要注意的是不能被序列化的数据是无法使用pickle进行持久化的。

 

def before_trading_start(context, data):

    import pickle

    notebook_path = get_research_path()

    try:
        with open(notebook_path+'days.pkl','rb') as f:
            days = pickle.load(f)
    except:
        days=0

    days+=1

    with open(notebook_path+'days.pkl','wb') as f:
        pickle.dump(days,f)

    log.info(days)   

 

附件包括一个统计持仓天数的函数,是一个持久化应用更具体的例子,可按需下载。

关于这个函数的详细说明可参阅http://www.100apple.net/article/detail.html?id=733

注意:下载文件建议使用电脑浏览器下载,勿用手机下载,否则可能会出现下载不了的情况。

List of attachments

get_days.txt

1KB

9.80

线上付款

Published on 2025-09-24 03:39

Disclaimers:

This document is written by 大观量化 Original published on 百果量化交流平台 ,The copyright belongs to the author。

Log in,More exciting content waiting for you to find,Contribute wonderful answers,Participate in comment interaction

go Sign in! No accountgoregister