Advanced - Straight Lines and Text in Paintbars

A paintbar can draw straight lines on the chart and annotate them with text. It is done using the SetLine function in Advanced mode and RemoveLine if at a later point paintbar needs to remove it. RemoveAllLines would remove every line from the chart.

 

public void SetLine(double ID, Color color, double x1, double y1, double x2, double y2, int width = 1, string text = null, LineTextPosition textposition = LineTextPosition.RightAbove, double fontMult = 1)

public void SetLine(double ID, Color color, DateTime T1, double y1, DateTime T2, double y2, int width = 1, string text = null, LineTextPosition textposition = LineTextPosition.RightAbove, double fontMult = 1)

 

 

ID

a unique identifier for the line. This would vary depending on how you define the lines. For example, if you know that your lines will never start at the same X position on the left, you can just use the line's left anchor's X coordinate as an ID.

color

the line's color. Can be Color, SysColor or an hex representation of the color - same as with the SetColor function

x1 or T1

the X or Timestamp coordinate of the first anchor of the line.
 
NOTE: the X coordinates of the line are in candle numbers' units. Each candle on the chart is numbered, so if you want the line to start at the current candle, you would use the CandleNumber property. If you want it to start 3 candles before the current one, set it to CandleNumber-3.
 
The Timestamp coordinates are just the UTC (note: UTC!) date/timestamps of the line anchors
 

y1

the price Y coordinate of the first anchor of the line

x2,y2

the coordinates for the second anchor

width

width of the line, in pixels.

text

the text with which the line is annotated. If no text, specify null

textposition

where on the line the text goes.

fontMult

font size multiplier. If set to 1, the text will be same size as other text on the chart. 0.5 would make it half as big, etc.

 

 

 

Example - draw High/Low lines every X minutes

 

double PeriodHigh;

double PeriodLow;

double PrevPeriod;

int PeriodStart;

 

public void MainCalculation()

{

  if (CandleNumber == 0) // just starting the paintbar

       PrevPeriod = -1;

 

  var MinFromSessionStart = (Timestamp - TradingDay.SessionStart).Ticks / TimeSpan.TicksPerMinute; // minutes from start of session

  if (MinFromSessionStart < 0) return; // only do regular trading hours

 

   DefinePaintbarParameter("Period", "Minutes", true, 5, 1000, 1, 30);

  var min = Convert.ToInt32(GetPaintbarParameter("Period"));

 

  var Period = MinFromSessionStart / min; // current period number

  if (Period != PrevPeriod) // if period changed, reset the period values

   {

       PeriodHigh = High;

       PeriodLow = Low;

       PeriodStart = CandleNumber;

       PrevPeriod = Period;

   }

 

   PeriodHigh = Math.Max(PeriodHigh, High); // update the period's high and low

   PeriodLow = Math.Min(PeriodLow, Low);

 

   Color HColor = GetNamedColor("High", SysColor.MainIndicator1);

  // set the high line

   SetLine(TradingDay.DayNumber * 1000 + Period, HColor, PeriodStart, PeriodHigh, CandleNumber + 1, PeriodHigh, 1, "H", LineTextPosition.RightAbove, 0.8);

   Color LColor = GetNamedColor("Low", SysColor.MainIndicator2);

  // set the low line

   SetLine(TradingDay.DayNumber * 2000 + Period, LColor, PeriodStart, PeriodLow, CandleNumber + 1, PeriodLow, 1, "L", LineTextPosition.RightBelow, 0.8);

}

 

Example - draw Pivot Support/Resistance every X minutes

 

double PrevHigh;

double PrevLow;

double PrevClose;

double PrevPeriod;

int PeriodStart;

 

public void MainCalculation()

{

  if (CandleNumber == 0) // just starting

   {

       PrevPeriod = -1; PrevHigh = 0;

   }

  var MinFromSessionStart = (Timestamp - TradingDay.SessionStart).Ticks / TimeSpan.TicksPerMinute;

  if (MinFromSessionStart < 0) return;

   DefinePaintbarParameter("Period", "Minutes", true, 5, 1000, 1, 30);

  var min = Convert.ToInt32(GetPaintbarParameter("Period"));

  var Period = MinFromSessionStart / min;

  if (Period != PrevPeriod)

   {

       PrevHigh = High;

       PrevLow = Low;

       PrevClose = Close;

       PeriodStart = CandleNumber;

       PrevPeriod = Period;

   }

  if (Period != 0)

   {

      var Pivot = (PrevHigh + PrevLow + PrevClose) / 3;

       Color R1Color = GetNamedColor("R1", SysColor.MainIndicator1);

       SetLine(TradingDay.DayNumber * 1000 + Period, R1Color,

               PeriodStart, 2 * Pivot - PrevLow,

               CandleNumber + 1, 2 * Pivot - PrevLow,

               1, "R1", LineTextPosition.RightAbove, 0.8);

       Color S1Color = GetNamedColor("S1", SysColor.MainIndicator2);

       SetLine(TradingDay.DayNumber * 2000 + Period, S1Color,

               PeriodStart, 2 * Pivot - PrevHigh,

               CandleNumber + 1, 2 * Pivot - PrevHigh,

               1, "S1", LineTextPosition.RightBelow, 0.8);

   }

   PrevHigh = Math.Max(PrevHigh, High);

   PrevLow = Math.Min(PrevLow, Low);

   PrevClose = Close;

 

}