I have revisted my Synthetic Moving Average effort.
his file will draw a red dot at a price equal to a
simple moving average of a HIGHER time frame.
If you have a 1 min chart, the default draws a 5 min simple MA
with a period of 50 based on the close.
If you have a 5 minute chart, you CAN NOT plot a 13 minute MA
because the closing values are not available to calculate the
moving average.
The higher time frame MUST be an even interval of the base chart.
An error message is given if the interval you are using
will not work for the higher interval you desire.
Charts with intervals of seconds (any amount) or minutes (gt 100)
will not compute either.
Not perfect for every application but it's a start.
Double check the output by creating a separate chart with the higher interval ma on it.
Be sure the Time Template is the same on both charts when you compare results.
The results seem to be off by less than 0.10 points on the ES - a very small percentage. Maybe some one can figure out why.
This only works for SIMPLE MAs.
var maSum=0;
var maSumAdj;
var BarMin;
var BarHr;
var BarHrMinDec;
var Price;
function preMain(){
setPriceStudy(true);
setDefaultBarStyle(PS_DOT, 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(3, 0);
setPlotType(PLOTTYPE_DOT, 0);
}
function main(maLen, maMin, Price){ //edit these with a right click on the chart,
//Edit Studies, select maTimea
maSum=0;
if(maLen==null)
{
var maLen=50;
}
if(maMin==null)
{
var maMin=5;
}
if(Price==null)
{
var Price="close";
}
var curInt=getInterval();
var IntRaw=curInt;
var curIntLen=curInt.length;
if(curIntLen>3) // 100+minutes is too long
{
debugPrintln("Interval Length too long");
return;
}
if(curIntLen==3) //i.e. 30S, no good
{
var IntRaw = curInt.substring(0, 2);
debugPrintln("IntRaw= "+IntRaw); //strip off 1st two characters
var timeframe = curInt.substring(2, 3); //check for S econds
debugPrintln("timeframe= "+timeframe);
intOK=((maLen*60/IntRaw)%1); //number of bars in a complete maLen
debugPrintln("intOK= "+intOK);
if(intOK!=0||timeframe!="S")
{
debugPrintln(curInt+"S is a Bad Interval, no seconds allowed");
return;
}
}
if(curIntLen==2) //i.e. 5S, no good
{
var IntRaw = curInt.substring(0, 1);
debugPrintln("IntRaw= "+IntRaw);
var timeframe = curInt.substring(1, 2); //check for S econds
debugPrintln("timeframe= "+timeframe);
intOK=((maLen*60/IntRaw)%1);
debugPrintln("intOK= "+intOK);
if(intOK!=0||timeframe!="S")
{
debugPrintln(curInt+"S is a Bad Interval, no seconds allowed");
return;
}
}
bars=maMin/curInt;
if(bars%1!=0) //if bars is not an integer, the current bars do not have the closing price data needed
{
debugPrintln(curInt+" is a BAD Interval");
}
nNum=maMin/IntRaw*maLen; //number of bars needed
//debugPrintln("nNum= "+nNum+" bars "+maMin+" "+IntRaw+" "+maLen);
for(i=0; i<maLen; i++)
{
getBarTime();
vTime=(BarHrMinDec/maMin)%1;
//debugPrintln((BarHrMinDec/maMin)%1);
if( (BarHrMinDec/maMin)%1==0)
{
testc=getValue(Price,-i*bars,1)*1;
maSum=maSum+testc;
//debugPrintln(-i*bars+" testc= "+testc+" "+maSum/50);
}
}
if(maSum!=0)
return maSum/50;
if(maSum==0)
return;
}
function getBarTime(){
BarHr = getHour()*60;
BarMin = getValue("Minute");
BarHrMinDec=BarHr*1+BarMin*1-570;
//debugPrintln(BarHrMinDec);
}
his file will draw a red dot at a price equal to a
simple moving average of a HIGHER time frame.
If you have a 1 min chart, the default draws a 5 min simple MA
with a period of 50 based on the close.
If you have a 5 minute chart, you CAN NOT plot a 13 minute MA
because the closing values are not available to calculate the
moving average.
The higher time frame MUST be an even interval of the base chart.
An error message is given if the interval you are using
will not work for the higher interval you desire.
Charts with intervals of seconds (any amount) or minutes (gt 100)
will not compute either.
Not perfect for every application but it's a start.
Double check the output by creating a separate chart with the higher interval ma on it.
Be sure the Time Template is the same on both charts when you compare results.
The results seem to be off by less than 0.10 points on the ES - a very small percentage. Maybe some one can figure out why.
This only works for SIMPLE MAs.
var maSum=0;
var maSumAdj;
var BarMin;
var BarHr;
var BarHrMinDec;
var Price;
function preMain(){
setPriceStudy(true);
setDefaultBarStyle(PS_DOT, 0);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarThickness(3, 0);
setPlotType(PLOTTYPE_DOT, 0);
}
function main(maLen, maMin, Price){ //edit these with a right click on the chart,
//Edit Studies, select maTimea
maSum=0;
if(maLen==null)
{
var maLen=50;
}
if(maMin==null)
{
var maMin=5;
}
if(Price==null)
{
var Price="close";
}
var curInt=getInterval();
var IntRaw=curInt;
var curIntLen=curInt.length;
if(curIntLen>3) // 100+minutes is too long
{
debugPrintln("Interval Length too long");
return;
}
if(curIntLen==3) //i.e. 30S, no good
{
var IntRaw = curInt.substring(0, 2);
debugPrintln("IntRaw= "+IntRaw); //strip off 1st two characters
var timeframe = curInt.substring(2, 3); //check for S econds
debugPrintln("timeframe= "+timeframe);
intOK=((maLen*60/IntRaw)%1); //number of bars in a complete maLen
debugPrintln("intOK= "+intOK);
if(intOK!=0||timeframe!="S")
{
debugPrintln(curInt+"S is a Bad Interval, no seconds allowed");
return;
}
}
if(curIntLen==2) //i.e. 5S, no good
{
var IntRaw = curInt.substring(0, 1);
debugPrintln("IntRaw= "+IntRaw);
var timeframe = curInt.substring(1, 2); //check for S econds
debugPrintln("timeframe= "+timeframe);
intOK=((maLen*60/IntRaw)%1);
debugPrintln("intOK= "+intOK);
if(intOK!=0||timeframe!="S")
{
debugPrintln(curInt+"S is a Bad Interval, no seconds allowed");
return;
}
}
bars=maMin/curInt;
if(bars%1!=0) //if bars is not an integer, the current bars do not have the closing price data needed
{
debugPrintln(curInt+" is a BAD Interval");
}
nNum=maMin/IntRaw*maLen; //number of bars needed
//debugPrintln("nNum= "+nNum+" bars "+maMin+" "+IntRaw+" "+maLen);
for(i=0; i<maLen; i++)
{
getBarTime();
vTime=(BarHrMinDec/maMin)%1;
//debugPrintln((BarHrMinDec/maMin)%1);
if( (BarHrMinDec/maMin)%1==0)
{
testc=getValue(Price,-i*bars,1)*1;
maSum=maSum+testc;
//debugPrintln(-i*bars+" testc= "+testc+" "+maSum/50);
}
}
if(maSum!=0)
return maSum/50;
if(maSum==0)
return;
}
function getBarTime(){
BarHr = getHour()*60;
BarMin = getValue("Minute");
BarHrMinDec=BarHr*1+BarMin*1-570;
//debugPrintln(BarHrMinDec);
}
Comment