OK.
I've managed to get a bit further along with this project. Although I am getting a script crash error in the section in bold type. Any ideas?
#include <profile.c>
#include <indicators.c>
#include <default.c>
function run()
{
vars Price = series(price());
vars Period = series();
vars Smooth = series((Price[0]*4 + Price[1]* 3 + Price[2]*2 + Price[3])/10);
vars Detrender = series((.0962 * Smooth[0] + .5769 * Smooth[2] - .5769 * Smooth[4] - .0962*Smooth[6]) * (.075 * Period[1] + .54));
// {Compute InPhase and Quadrature components.}
vars Q1 = series((.0962 * Detrender[0] + .5769 * Detrender[2] - .5769 * Detrender[4] - .0962 * Detrender[6]) * (.075 * Period[1] + .54));
vars I1 = series(Detrender[3]);
// {Advance the phase of I1 and Q1 by 90 degrees}
vars jI = series((.0962 * I1[0] + .5769 * I1[2] - .5769 * I1[4] - .0962 * I1[6]) * (.075 * Period[1] + .54));
vars JQ = series((.0962 * Q1[0] + .5769 * Q1[2] - .5769 * Q1[4] - .0962 * Q1[6]) * (.075 * Period[1] + .54));
// {Phasor addition for 3 bar averaging)}
vars I2 = series(I1[0] - JQ[0]);
vars Q2 = series(Q1[0] + jI[0]);
// {Smooth the I and Q components before applying the discriminator}
vars I2 = series(.2 * I2[0] + .8 * I2[1]);
vars Q2 = series(.2 * Q2[0] + .8 * Q2[1]);
plot("Smooth", Smooth[0], MAIN,BLUE);
plot("Detrender",Detrender[0], NEW,RED);
plot("I1", I1[0],0, BLUE);
plot("jI",jI[0], 0,ORANGE);
plot("Q1", Q1[0], 0, ORANGE);
plot("JQ",JQ[0], 0, GREEN);
plot("I2", I2[0], 0, PURPLE);
plot("Q2", Q2[0], 0, YELLOW);
// {Homodyne Discriminator}
vars Re = series(I2[0] * I2[1] + Q2[0] * Q2[1]);
vars Im = series(I2[0] * Q2[1] - Q2[0] * I2[1]);
vars Re = series(.2 * Re[0] + .8 * Re[1]);
vars Im = series(.2 * Im[0] + .8 * Im[1]);
plot("Smooth", Smooth[0], MAIN,BLUE);
plot("Detrender",Detrender[0], NEW,RED);
plot("I1", I1[0],0, BLUE);
plot("jI",jI[0], 0,ORANGE);
plot("Q1", Q1[0], 0, ORANGE);
plot("JQ",JQ[0], 0, GREEN);
plot("I2", I2[0], 0, PURPLE);
plot("Q2", Q2[0], 0, YELLOW);
plot("Re", Re[0], NEW, BLACK);
plot("Im", Im[0], 0, OLIVE);
vars SmoothPeriod = series();
if(Im != 0 and Re != 0)
vars Period = series((2*PI) / atan2(Im[0],Re[0])); // This line and below line are causing the script to crash when both are active
if(Period[0] > 1.5 * Period[1])
vars Period = series(1.5 * Period[1]);
if(Period[0] < .67 * Period[1])
vars Period = series(.67 * Period[1]);
if(Period[0] < 6)
Period = 6;
if(Period[0] > 50)
Period = 50;
vars Period = series((.2 * Period[0] + .8 * Period[1])); // This line and above
vars SmoothPeriod = series(.33 * Period[0] + .67 * SmoothPeriod[1]);
}