Tuesday, May 5, 2009

Pivot levels

Here is some code to calculate the simple pivot levels for any symbols. You can choose the timeframe to decide if you want to calculate daily, weekly or monthly pivots. By default, the code only shows the pivot, S1 and R1. You can remove the hide() lines and show all the levels if you want.



input timeFrame = {default DAY, WEEK, MONTH};
def period_high = high(period = timeFrame)[1];
def period_low = low(period = timeFrame)[1];
def period_close = close(period = timeFrame)[1];

plot pivot = (period_high + period_low + period_close)/3;
pivot.SetDefaultColor(Color.YELLOW);

plot R1 = pivot * 2 - period_low;
R1.SetDefaultColor(Color.GREEN);
plot S1 = pivot * 2 - period_high;
S1.SetDefaultColor(Color.GREEN);

plot R2 = pivot + R1 - S1;
R2.SetDefaultColor(Color.LIGHT_GRAY);
R2.hide();

plot S2 = pivot - (R1-S1);
S2.SetDefaultColor(Color.LIGHT_GRAY);
S2.hide();

plot R3 = period_high + 2*(pivot-period_low);
R3.SetDefaultColor(Color.LIGHT_GREEN);
R3.hide();
plot S3 = period_low - 2*(period_high - pivot);
S3.SetDefaultColor(Color.LIGHT_GREEN);
S3.hide();