PTrade工具:分享一个统计持仓天数的易用函数

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

0 People liked this article · 1221 views

get_days函数可以统计持仓标的持有天数。


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

 

首先需要将附件get_days.txt中的函数代码复制到策略末尾。然后在before_trading_start、after_trading_end或者run_daily中调用。


函数包含持久化,重启策略不影响已经统计的数据。

 

使用示例如下:

def before_trading_start(context, data):

    if len(context.portfolio.positions)>0:

        log.info(get_days(context))

 

输出结果:

List of attachments

get_days.txt

1KB

9.80

线上付款

Published on 2025-09-04 15:18

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