// MurreyMath.c
// Murrey Math Channel for Zorro Lite-C
// --- Determine fractal size ---
function double DetermineFractal(double value)
{
if(value <= 250000 && value > 25000) return 100000;
if(value <= 25000 && value > 2500) return 10000;
if(value <= 2500 && value > 250) return 1000;
if(value <= 250 && value > 25) return 100;
if(value <= 25 && value > 12.5) return 12.5;
if(value <= 12.5 && value > 6.25) return 12.5;
if(value <= 6.25 && value > 3.125) return 6.25;
if(value <= 3.125 && value > 1.5625) return 3.125;
if(value <= 1.5625 && value > 0.390625) return 1.5625;
if(value <= 0.390625 && value > 0) return 0.1953125;
return 0;
}
// --- Murrey Math calculation ---
// Fills "levels[13]" with values from [+2/8] to [-2/8]
function MurreyMath(vars PriceHigh, vars PriceLow, int Period, var* levels)
{
if(Bar < Period + 1) return; // Not enough bars yet
var min = MinVal(PriceLow, Period);
var max = MaxVal(PriceHigh, Period);
var fractal = DetermineFractal(max);
var range = max - min;
var sum = floor(log(fractal / range) / log(2));
var octave = fractal * pow(0.5, sum);
var mn = floor(min / octave) * octave;
var mx = mn + (2 * octave);
if((mn + octave) >= max)
mx = mn + octave;
// Resistance determination
var x1 = 0, x2 = 0, x3 = 0, x4 = 0, x5 = 0, x6 = 0;
if ((min >= (3*(mx-mn)/16 + mn)) && (max <= (9*(mx-mn)/16 + mn))) x2 = mn + (mx - mn)/2;
if ((min >= (mn - (mx - mn)/8)) && (max <= (5*(mx - mn)/8 + mn)) && (x2 == 0)) x1 = mn + (mx - mn)/2;
if ((min >= (mn + 7*(mx - mn)/16)) && (max <= (13*(mx - mn)/16 + mn))) x4 = mn + 3*(mx - mn)/4;
if ((min >= (mn + 3*(mx - mn)/8)) && (max <= (9*(mx - mn)/8 + mn)) && (x4 == 0)) x5 = mx;
if ((min >= (mn + (mx - mn)/8)) && (max <= (7*(mx - mn)/8 + mn)) && (x1 == 0) && (x2 == 0) && (x4 == 0) && (x5 == 0))
x3 = mn + 3*(mx - mn)/4;
if ((x1 + x2 + x3 + x4 + x5) == 0) x6 = mx;
var resistance = x1 + x2 + x3 + x4 + x5 + x6;
// Support determination
var y1 = 0, y2 = 0, y3 = 0, y4 = 0, y5 = 0, y6 = 0;
if (x1 > 0) y1 = mn;
if (x2 > 0) y2 = mn + (mx - mn)/4;
if (x3 > 0) y3 = mn + (mx - mn)/4;
if (x4 > 0) y4 = mn + (mx - mn)/2;
if (x5 > 0) y5 = mn + (mx - mn)/2;
if ((resistance > 0) && ((y1 + y2 + y3 + y4 + y5) == 0)) y6 = mn;
var support = y1 + y2 + y3 + y4 + y5 + y6;
var divide = (resistance - support) / 8;
levels[12] = support - 2*divide; // [-2/8]
levels[11] = support - divide; // [-1/8]
levels[10] = support; // [0/8]
levels[9] = support + divide; // [1/8]
levels[8] = support + 2*divide; // [2/8]
levels[7] = support + 3*divide; // [3/8]
levels[6] = support + 4*divide; // [4/8]
levels[5] = support + 5*divide; // [5/8]
levels[4] = support + 6*divide; // [6/8]
levels[3] = support + 7*divide; // [7/8]
levels[2] = resistance; // [8/8]
levels[1] = resistance + divide; // [+1/8]
levels[0] = resistance + 2*divide;// [+2/8]
}
// --- Main script ---
function run()
{
set(PLOTNOW);
BarPeriod = 1440; // Daily bars
LookBack = 200; // Required lookback
// Use correct series() syntax: returns `vars` (pointer array)
vars PriceHigh = series(priceHigh(), LookBack);
vars PriceLow = series(priceLow(), LookBack);
static var mm[13]; // Buffer for Murrey levels
MurreyMath(PriceHigh, PriceLow, 64, mm);
// Plotting the Murrey channels
plot("MM +2/8", mm[0], LINE, BLUE);
plot("MM +1/8", mm[1], LINE, BLUE);
plot("MM 8/8", mm[2], LINE, BLACK);
plot("MM 7/8", mm[3], LINE, RED);
plot("MM 6/8", mm[4], LINE, RED);
plot("MM 5/8", mm[5], LINE, GREEN);
plot("MM 4/8", mm[6], LINE, BLACK);
plot("MM 3/8", mm[7], LINE, GREEN);
plot("MM 2/8", mm[8], LINE, RED);
plot("MM 1/8", mm[9], LINE, RED);
plot("MM 0/8", mm[10], LINE, BLUE);
plot("MM -1/8", mm[11], LINE, BLUE);
plot("MM -2/8", mm[12], LINE, BLUE);
}