Alligator Williams

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

// Define the moving average periods for the Alligator lines

const jawPeriod = 13;


const teethPeriod = 8;
const lipsPeriod = 5;

// Define the shift amount for the Alligator lines


const shift = 8;

// Define the smoothing method for the moving averages


const smoothingMethod = 'SMA';

// Define the input data for the Alligator lines


const inputData = [ { open: 100, close: 105 }, { open: 105, close: 110 },
{ open: 110, close: 115 }, { open: 115, close: 120 }, { open: 120, close: 125 },
{ open: 125, close: 130 }, { open: 130, close: 135 }, { open: 135, close: 140 },
{ open: 140, close: 145 }, { open: 145, close: 150 }, { open: 150, close: 155 },
{ open: 155, close: 160 }, { open: 160, close: 165 }];

// Calculate the Alligator lines


const jaw = movingAverage(inputData, jawPeriod, smoothingMethod);
const teeth = movingAverage(inputData, teethPeriod, smoothingMethod);
const lips = movingAverage(inputData, lipsPeriod, smoothingMethod);

// Shift the Alligator lines by the defined amount


const shiftedJaw = shiftArray(jaw, shift);
const shiftedTeeth = shiftArray(teeth, shift);
const shiftedLips = shiftArray(lips, shift);

// Plot the Alligator lines on a chart


plotLine(shiftedJaw, 'Jaw');
plotLine(shiftedTeeth, 'Teeth');
plotLine(shiftedLips, 'Lips');

// Function to calculate a moving average


function movingAverage(data, period, method) {
// Initialize the array of moving averages
const movingAverages = [];

// Loop through the data and calculate the moving average for each period
for (let i = 0; i < data.length; i++) {
// Calculate the moving average based on the specified method
let sum = 0;
if (method === 'SMA') {
// Simple moving average
for (let j = 0; j < period; j++) {
sum += data[i - j].close;
}
movingAverages.push(sum / period);
} else if (method === 'EMA') {
// Exponential moving average
const previousEMA = movingAverages[i - 1] || data[0].close;
movingAverages.push((data[i].close - previousEMA) * (2 / (period + 1)) +
previousEMA);
}
}

// Return the array of moving averages


return movingAverages;
}
// Function to shift an array by a certain number of elements
function shiftArray(arr, shiftAmount) {
// Initialize the shifted array
const shiftedArr = [];

You might also like