Plot Pivots

This script calculates and plots pivot points twice a day.  The first pivot point (the a.m. pivot point) is calculated by obtaining the highest price and lowest price from the last two 20-minute bars from the previous day and the first 20 minute bar of the current day.  I then obtain the open and close price from the current 20-min. bar to complete the O,H,L,C average a.m. pivot point.

The p.m. pivot point is calculated around the middle of the day.  The high and low will be obtained from the current day's action (morning prices) and I only take the opening of the mid-day bar to get the O,H,L average p.m. pivot point.

procedure AMPivotPoint;
var 
  i,x,y: integer;
  nHigh,nLow,nOpen,nLast,nPivot: real;
  bPlot: boolean;
begin
  i:=BarEnd;                                      {start at current bar, work backwards}
  bPlot:=false;                                   {assume nothing to plot}
  while i>2 do begin                              {work backwards through chart bars}  
    if Bar(eDate,i)>Bar(eDate,pred(i)) then begin {found today's boundary}
      bPlot:=true;                                {will have something to plot}
      x:=IndexToX(i);                             {remember screen point where this bar is}
      nOpen:=Open(i);                             {today's 1st bar Open}
      nLast:=Last(i);                             {today's 1st bar Last}
      dec(i);                                     {point to last bar of yesterday}
      nHigh:=Highest(eHigh,i,2,i,1,0);            {highest of yesterday's last 2 bars}
      nLow :=Lowest(eLow,i,2,i,1,0);              {lowest of yesterday's last 2 bars}
      i:=1;                                       {aborts while loop, we're done}
    end; 
    dec(i);                                       {decrement bar index, move backwards}
  end;
  if bPlot then begin                             {plot AM pivot point}
    nPivot:=(nHigh+nLow+nOpen+nLast) / 4;         {compute AM pivot value}
    y:=PriceToY(nPivot);                          {screen position where to plot price}
    SetPen(clRed,1,eDot);                         {plot RED, 1 pixel width, dotted}
    MoveToLineTo(x,y,IndexToX(BarEnd),y);         {draw horizontal AM line}
  end;
end;

procedure PMPivotPoint;
var 
  i,x,y: integer;
  nHigh,nLow,nOpen,nPivot: real;
  bPlot: boolean;
begin
  i:=BarEnd;                                      {start at current bar, work backwards}
  bPlot:=false;                                   {assume nothing to plot}
  while i>2 do begin                              {work backwards through chart bars}  
    if Bar(eTime,i)=1200 then begin               {found today's mid-day bar}
      bPlot:=true;                                {will have something to plot}
      x:=IndexToX(i);                             {remember screen point where this bar is}
      nOpen:=Open(i);                             {today's mid-day Open}
      nHigh:=High(i);                             {initialize High}
      nLow :=Low(i);                              {initialize Low}
    end; 

    if Bar(eDate,i)>Bar(eDate,pred(i)) then i:=1; {found today's boundary, abort loop}
    dec(i);                                       {decrement bar index, move backwards}
 
    if bPlot and (i>0) then begin                 {check morning bars for wider range}
      nHigh:=Max(nHigh,High(i));                  {choose highest value}
      nLow:=Min(nLow,Low(i));                     {choose lowest value}
    end;
  end;
  if bPlot then begin                             {plot PM pivot point}
    nPivot:=(nHigh+nLow+nOpen) / 3;               {compute PM pivot value}
    y:=PriceToY(nPivot);                          {screen position where to plot price}
    SetPen(clBlue,1,eDot);                        {plot BLUE, 1 pixel width, dotted}
    MoveToLineTo(x,y,IndexToX(BarEnd),y);         {draw horizontal PM line}
  end;
end;

{----- MAIN PROGRAM -----}
begin
  if Who=41 then begin
    AMPivotPoint;
    PMPivotPoint;
  end;
end;

This is pretty clever code, so you should study this example.  A lot can be learned.  It illustrates the power of ESPL to build custom tools.

To use, cut the script to the clipboard by dragging across the text, and then press CTRL-C to copy.   In the ESPL editor, click the cursor in the left side panel, and press CTRL-V to paste from the clipboard.   On your 20-minute chart, the AM and PM pivot points will be plotted when you select the Color Bars button, and on the drop down panel click the #1 button.  This button has a hint of  Who=41.   Viola, the two lines will be drawn.

This tool will be dynamic, in that during the morning you may see just the AM pivot line, of course.  Then after the 1200 noon bar, the PM line will automatically start to plot as well.   You can edit the script to select your own line color, line width, and line style.   You can also edit the script to select the time for the mid-day bar.   I coded it to be 1200 (high noon), but you may want to use a different time stamp to initiate the PM pivot plot.