Thank you.. I looked at TV and saw how that Pivots Back was limiting how many pivots where being plotted.Hello,
I recently took a Pivot Point class from Jeff York and was very excited to cobble together the following code - inspirations are from this website (unattributed when I found it) and elsewhere, and I inserted the variety of Pivot formulas that I learnt in Jeff York's class. I am unable to figure out how to attach a screenshot of the Pivot formulas for reference to this thread, but I am quite sure that the code is simple enough to understand each formula.
The number of Rs and Ss that are to be plotted are different for the different types of Pivots. I am unable to run this code without keeping the number of Rs and Ss exactly the same. Can someone please help? I have annotated the code at the lines where I am facing issues. Please ask me questions that would help in assisting me with this problem. I appreciate your time, and thanks in advance.
Update: I will also request help with stretching the Pivots to a few periods in the past (akin to what can be accomplished on Trading View).
Update2: Code updated to reflect Quarter aggregation
There is no built-in function in TOS to do that, but it can be coded. In the following code, to accomplish that, all your original plot statement were changed to def (eg: def R4; etc, ....) statements. All the plots where renamed by adding a P to the def statements (eg: plot R4P, etc ....).
In the picture below, Pivots Back was set to 3 Traditional Pivots set to DAY on a 5m chart.
Ruby:
input timeFrame = {DAY, default WEEK, MONTH, QUARTER, YEAR};
input pivotType = {Camarilla, Demarks, Fibonacci, default Traditional, Woodies};
input showOnlyToday = no;
#COMMON
def H = high(period = timeFrame)[1];
def L = low(period = timeFrame)[1];
def C = close(period = timeFrame)[1];
#DEMARKS
def O = open(period = timeFrame)[1];
def X = if C < O then (H + (2 * L) + C) else if C > O then ((2 * H) + L + C) else (H + L + (2 * C));
#WOODIES
def T = open(period = "DAY");
def R4;
def R3;
def R2;
def R1;
def PP;
def S1;
def S2;
def S3;
def S4;
if (showOnlyToday and !IsNaN(close(period = timeFrame)[-1])) or
(GetAggregationPeriod() > if timeFrame == timeFrame.DAY
then AggregationPeriod.DAY
else if timeFrame == timeFrame.WEEK
then AggregationPeriod.WEEK
else if timeFrame == timeFrame.MONTH
then AggregationPeriod.MONTH
else if timeFrame == timeFrame.QUARTER
then AggregationPeriod.QUARTER
else AggregationPeriod.YEAR)
then {
R1 = Double.NaN;
R2 = Double.NaN;
R3 = Double.NaN;
R4 = Double.NaN;
PP = Double.NaN;
S1 = Double.NaN;
S2 = Double.NaN;
S3 = Double.NaN;
S4 = Double.NaN;
}
else {
switch (pivotType) {
case Camarilla:
PP = (H + L + C) / 3;
R1 = (0.0916 * (H - L)) + C;
R2 = (0.1830 * (H - L)) + C;
R3 = (0.2750 * (H - L)) + C;
R4 = (0.5500 * (H - L)) + C;
S1 = C - (0.0916 * (H - L));
S2 = C - (0.1830 * (H - L));
S3 = C - (0.2750 * (H - L));
S4 = C - (0.5500 * (H - L));
case Fibonacci:
PP = (H + L + C) / 3;
R1 = PP + (0.382 * (H - L));
R2 = PP + (0.618 * (H - L));
R3 = PP + (1.000 * (H - L));
R4 = Double.NaN;
S1 = PP - (0.382 * (H - L));
S2 = PP - (0.618 * (H - L));
S3 = PP - (1.000 * (H - L));
S4 = Double.NaN;
case Demarks:
PP = X / 4;
R1 = (X / 2) - L;
R2 = Double.NaN;
R3 = Double.NaN;
R4 = Double.NaN;
S1 = (X / 2) - H;
S2 = Double.NaN;
S3 = Double.NaN;
S4 = Double.NaN;
case Traditional:
PP = (H + L + C) / 3;
R1 = (2 * PP) - L;
R2 = PP + H - L;
R3 = H + 2 * (PP - L);
R4 = Double.NaN;
S1 = (2 * PP) - H;
S2 = PP - H + L;
S3 = L - 2 * (H - PP);
S4 = Double.NaN;
case Woodies:
PP = (H + L + (T * 2)) / 4;
R1 = (2 * PP) - L;
R2 = PP + (H - L);
R3 = H + (2 * (PP - L));
R4 = R3 + (H - L);
S1 = (2 * PP) - H;
S2 = PP - (H - L);
S3 = L - (2 * (PP - L));
S4 = S3 - (H - L);
} #/switch
} #/ifte
def capture = !IsNaN(close) and PP != PP[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def pivot = (HighestAll(dayCount) - dayCount) + 1 ;
input pivotsback = 3;
plot ppp = if pivot > pivotsback then Double.NaN else PP;
plot R4P = if pivot > pivotsback then Double.NaN else R4;
plot R3P = if pivot > pivotsback then Double.NaN else R3;
plot R2P = if pivot > pivotsback then Double.NaN else R2;
plot R1P = if pivot > pivotsback then Double.NaN else R1;
plot S1P = if pivot > pivotsback then Double.NaN else S1;
plot S2P = if pivot > pivotsback then Double.NaN else S2;
plot S3P = if pivot > pivotsback then Double.NaN else S3;
plot S4P = if pivot > pivotsback then Double.NaN else S4;
R1P.SetDefaultColor(Color.RED);
R2P.SetDefaultColor(Color.RED);
R3P.SetDefaultColor(Color.RED);
R4P.SetDefaultColor(Color.RED);
S1P.SetDefaultColor(Color.GREEN);
S2P.SetDefaultColor(Color.GREEN);
S3P.SetDefaultColor(Color.GREEN);
S4P.SetDefaultColor(Color.GREEN);
R1P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R2P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R3P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
R4P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S1P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S2P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S3P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
S4P.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ppp.DefineColor("Color", CreateColor(0, 102, 204));
ppp.AssignValueColor(ppp.Color("Color"));
ppp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Last edited by a moderator: