I managed to figure out a solution in Python. This code works on the last 100 days’ data, which is fine for my purposes:
import Tool
from statistics import mean
#Init function gets called once when tool is applied/loaded
def Init():
Tool.Props.Name = "AvgDaysUp"
Tool.Props.Hint = "Average number of days spent in up moves while trend is up"
#Process function gets called when applied/loaded or when new data is received
#start, end are the start and end+1 indexes of the source data list.
def Process(start, end):
TrendIsUp = Tool.RunScript('gs1=GANNSWING(SWINGCOUNT=1);SWINGTRENDUP(gs1) and CLOSE()>0', Tool.Source)
HigherLow = Tool.RunScript('LOW(OFFSET=1)<=LOW()', Tool.Source)
DayRunCount = 0
RunCounts = []
for i in range(end-101, end):
if TrendIsUp.Row(i).Close != 0 and HigherLow.Row(i).close != 0:
DayRunCount += 1
else:
if DayRunCount > 0:
RunCounts.append(DayRunCount)
DayRunCount = 0
print(mean(RunCounts))
for i in range(end-101, end):
Tool.DataOut.Row(i).Close = mean(RunCounts)