Relative Volume - Partially Coded
I partially coded a Time Based Relative Volume Indicator It takes x last days (period) and averages the volume for a given time.
Any Help would be appreciated!
Code is at the end of this.
Short one time period example:
Day:-2, time:9:30, volume:123
Day:-1, time:9:30, volume:456 (old)
Day:0 , time:9:30, volume:789 (present day)
Relative Volume = (123 + 456)/2 = 289
PLOT(at 9:30) volume = 789
PLOT(at 9:30): relativeVolume = 289
Should look something like this (if you ignore the outliers - Bad Data)

//======================================================================
//CODE START
const predef = require("./tools/predef");
const meta = require("./tools/meta");
const SMA = require("./tools/SMA");
const { ParamType } = meta;
function number(defValue, step, min) {
return {
type: ParamType.NUMBER,
def: defValue,
restrictions: {
step: step || 1,
min: min > 0 ? min : 0
}
};
}
//==============================================================================
//I was looking to store the old volume per each time on a(period == day)
// then calculate the relative volume per time
//- Probably some errors in this code
class VDataClass {
init() {
this.vData = new Array(this.props.period);
this.rData = new Array(this.prots.period);
this.currentDay= "";
this.dayIx = 0;
this.firstTime = 1;
}
map(d) {
//Store the volume for the current period (dayIx)
var data = {"timestamp":timestamp, "volume":d.volume};
this.vData[this.dayIx].push(data);
//Handle day/period roll
const day = timestamp.getDay();
if (this.firstTime == 1)
{
this.firstTime = 0;
this.currentDay = day;
}
//Calculate relative volume by time (period == day)
//period=1, time=9:30 volume = 123
//period=2, time=9:30 volume = 456
//period=3, time=9:30 volume = 789
//Relative volume at time=9:30 = (123 + 456 + 789)/3(periods)
var rvol = 0;
var rTime;
for (var i = 0; i < this.dayIx; i++)
{
rvol = 0;
for (var j = 0; j < this.vData[i].length; j++)
{
rvol = rvol + this.vData[i].volume;
}
rvol = Math.round(rvol/this.vData[i].length);
rTime = this.vData[i].timestamp
//This should have the volume values per the dateTime.
this.rData[this.dayIx].push({"timestamp":rTime, "volume":rvol});
}
//Check to see if it's time to inc day/period
if (day != this.currentDay)
{
this.dayIx++;
}
return(this.rData);
}
}
module.exports = {
name: "rDataClass",
params: {
period: predef.paramSpecs.period(21)
}
};
//==============================================================================
class wt_RelativeVolume {
init() {
this.rvData = new VDataClass(this.props.period); //old data
console.log(" period=" + this.props.period);
}
map(d, index, history) {
const timestamp = d.timestamp();
const hour = timestamp.getHours();
const minute = timestamp.getMinutes();
var nowTime = hour + ":" + minute;
let relativeVolume = 0;
//Debug
console.log(" index=" + index);
console.log(" timestamp= " + timestamp);
console.log(" time=" + nowTime);
//Show the volume
let volume = d.volume();
console.log(" volume=" + volume);
for (var i = 0; i < this.props.period; i++)
{
const rvHour = timestamp.getHours();
const rvMinute = timestamp.getMinutes();
const rvTime = rvHour + ":" + rvMinute;
console.log(" rvTime=" + rvTime);
if (nowTime == rvTime)
{
relativeVolume = this.rvData[i].volume;
console.log(" relativeVolume=" + relativeVolume);
}
}
return {
relativeVolume,
volume
};
}
}
module.exports = {
name: "wt_RelativeVolume",
description: "wt_RelativeVolume",
calculator: wt_RelativeVolume,
params: {
period: number(21, 1, 1)
},
inputType: meta.InputType.BARS,
plots: {
relativeVolume: "Relative Volume",
volume: "Volume"
},
areaChoice: meta.AreaChoice.NEW,
tags: ['wt Tools'],
schemeStyles: {
dark: {
relativeVolume: predef.styles.plot("#b84bf2"),
volume: predef.styles.plot("#ff0000")
}
}
};
//======================================================================
//CODE END
-
Aloha Will, unfortunately this indicator will be difficult to pull off because data is only loaded in as needed, I believe for performance reasons. You'll notice that when you load a chart and scroll backwards, the additional bars and indicators are calculated.
We'd need the ability to load in say, the past 10 days of intraday data to form the buckets and figure out the average volume per period. This would be a new feature request, perhaps someone from Tradovate could weigh as this is something other platforms do offer.
-
Tiki Dave - Thanks for the reply.
I was hoping that wasn't the case. For now I'm still thinking about trying to code it and only using the indicator (like you said) for the days displayed. I think it will work on lower time periods in order to validate from consolidation to a valid trend day.
If I run into issues I will reach out.
.
Please sign in to leave a comment.
Comments
2 comments