Average number of bars in swing while in up trends

Hi all,

I’m trying to work out the average number of bars in up swings while the (Gann) trend is up.

What I’ve got so far is

gs1 = GANNSWING();
SWINGSTAT(gs1, DEFAULT=AvgBars)

That seems to give me the average number of bars in up swings, but ignores the direction of the trend.

SWINGTRENDUP(gs1)

gives me a way to work out the direction of the trend, but I can’t work out how to use it to filter the results of SWINGSTAT() in the above script.

Any thoughts?

Thanks in advance

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)