如何使用Thinkscript获得总收入?

时间:2017-09-26 11:38:12

标签: thinkscript

我想计算图表上的总收入。如果这是1年的日线图,我应该得到4个收益。没有错误消息,但图表上没有显示标签。

def earningCount = if IsNaN(earningCount) then 0 else if hasEarnings() then earningCount + 1 else earningCount;
AddLabel(yes, "There are total " + earningCount + " earnings"); 

2 个答案:

答案 0 :(得分:0)

您需要做的是从第一天开始,并在前一天重复询问hasEarnings()。不幸的是,在thinkcript中没有任何for / while循环功能,这将是非常乏味的:

def earningCount;

#get latest date
def today = getYYYYMmDd();

#get first date in chart
def firstDay = first(today);

#get number of days to iterate through:
def numOfDays = CountTradingDays(firstDay,today);

#Ask for each day one at a time: if hasEarnings() then earningCount + 1 else Double.NaN;

#today
today

#day before
today[1]

#day before that... etc..
today[2]

#... until first day in chart
today[numOfDays]

不是您想要的最佳解决方案。或者,您可以询问图表中有多少年,也可以询问4乘以4,因为您知道通常每年有4个收入......

答案 1 :(得分:0)

def earningCount = if IsNaN(earningCount[1]) then 0 else if hasEarnings() then earningCount[1] + 1 else earningCount[1];
AddLabel(yes, "There are total " + earningCount + " earnings");