magnetar513
New member
Someone has probably already done this, but here is my attempt: It seems to work pretty well, but if anyone finds ways to improve it, please let me know.
# Multi Day Moving Average that remains accurate for different chart
# aggregation periods. i.e. Study length is automatically adjusted
# according to aggregation period of the chart.
# I tried adding multiple MAs to a single study, but plotting was
# unreliable, particularly on aggregation periods less than two hours.
# Selecting several single studies with different day lengths on the same chart seems to sidestep these problems.
# Of course, it won't work with Ticks.
# by: Magnetar513
declare upper;
input price1= close;
input avgType1= AverageType.WILDERS;
input numDays= 10;
input priceA= FundamentalType.CLOSE;
def aggPeriod= GetAggregationPeriod();
def length= if aggPeriod== aggregationPeriod.MIN then (1440* numDays)
else if aggPeriod== aggregationPeriod.TWO_MIN then (720* numDays)
else if aggPeriod== aggregationPeriod.THREE_MIN then (480* numDays)
else if aggPeriod== aggregationPeriod.FOUR_MIN then (360* numDays)
else if aggPeriod== aggregationPeriod.FIVE_MIN then (288* numDays)
else if aggPeriod== aggregationPeriod.TEN_MIN then (144* numDays)
else if aggPeriod== aggregationPeriod.FIFTEEN_MIN then (96* numDays)
else if aggPeriod== aggregationPeriod.TWENTY_MIN then (72* numDays)
else if aggPeriod== aggregationPeriod.THIRTY_MIN then (48* numDays)
else if aggPeriod== aggregationPeriod.HOUR then (24* numDays)
else if aggPeriod== aggregationPeriod.TWO_HOURS then (12* numDays)
else if aggPeriod== aggregationPeriod.FOUR_HOURS then (3* numDays)
else if aggPeriod== aggregationPeriod.DAY then (numDays)
else if aggPeriod== aggregationPeriod.TWO_DAYS then (numDays/2)
else if aggPeriod== aggregationPeriod.THREE_DAYS then (numDays/3)
else if aggPeriod== aggregationPeriod.FOUR_DAYS then (numDays/4)
else if aggPeriod== aggregationPeriod.WEEK then (numDays/5)
else if aggPeriod== aggregationPeriod.MONTH then (numDays/20)
else if aggPeriod== aggregationPeriod.QUARTER then (numDays/60)
else if aggPeriod== aggregationPeriod.YEAR then (numDays/180)
else double.nan;
plot mvgAvg= MovingAverage(avgType1, price1, length);
mvgAvg.setDefaultColor(color.green);
# Multi Day Moving Average that remains accurate for different chart
# aggregation periods. i.e. Study length is automatically adjusted
# according to aggregation period of the chart.
# I tried adding multiple MAs to a single study, but plotting was
# unreliable, particularly on aggregation periods less than two hours.
# Selecting several single studies with different day lengths on the same chart seems to sidestep these problems.
# Of course, it won't work with Ticks.
# by: Magnetar513
declare upper;
input price1= close;
input avgType1= AverageType.WILDERS;
input numDays= 10;
input priceA= FundamentalType.CLOSE;
def aggPeriod= GetAggregationPeriod();
def length= if aggPeriod== aggregationPeriod.MIN then (1440* numDays)
else if aggPeriod== aggregationPeriod.TWO_MIN then (720* numDays)
else if aggPeriod== aggregationPeriod.THREE_MIN then (480* numDays)
else if aggPeriod== aggregationPeriod.FOUR_MIN then (360* numDays)
else if aggPeriod== aggregationPeriod.FIVE_MIN then (288* numDays)
else if aggPeriod== aggregationPeriod.TEN_MIN then (144* numDays)
else if aggPeriod== aggregationPeriod.FIFTEEN_MIN then (96* numDays)
else if aggPeriod== aggregationPeriod.TWENTY_MIN then (72* numDays)
else if aggPeriod== aggregationPeriod.THIRTY_MIN then (48* numDays)
else if aggPeriod== aggregationPeriod.HOUR then (24* numDays)
else if aggPeriod== aggregationPeriod.TWO_HOURS then (12* numDays)
else if aggPeriod== aggregationPeriod.FOUR_HOURS then (3* numDays)
else if aggPeriod== aggregationPeriod.DAY then (numDays)
else if aggPeriod== aggregationPeriod.TWO_DAYS then (numDays/2)
else if aggPeriod== aggregationPeriod.THREE_DAYS then (numDays/3)
else if aggPeriod== aggregationPeriod.FOUR_DAYS then (numDays/4)
else if aggPeriod== aggregationPeriod.WEEK then (numDays/5)
else if aggPeriod== aggregationPeriod.MONTH then (numDays/20)
else if aggPeriod== aggregationPeriod.QUARTER then (numDays/60)
else if aggPeriod== aggregationPeriod.YEAR then (numDays/180)
else double.nan;
plot mvgAvg= MovingAverage(avgType1, price1, length);
mvgAvg.setDefaultColor(color.green);
Last edited: