Automatic Trend Line Coloring

Questions about MultiCharts and user contributed studies.
User avatar
Posts: 375
Joined: 16 Jun 2008
Has thanked: 40 times
Been thanked: 38 times

Automatic Trend Line Coloring

Postby geizer » 22 Mar 2010


this code is helpful if you prefer different colors for your 'Up' and 'Down' trend lines.

At first I was going to send a new feature request, but decided to implement it in PowerLanguage.

I need some help, so the code could apply the color without having to wait for a new tick (when charting offline).


Code: Select all

// Automatic Trend Line Coloring
// File name:   TL_AutoColor
// This indicator designed to work with Multicharts and be placed
// on all your charts. The indicator detects when a new trend line
// is created or existing trend line is selected. It automatically
// applies one of three pre-defined colors to selected trend line
// depending on 'positive', 'negative', or 'neutral' slope.
// The corresponding color inputs are: 'Up', 'Down',and 'Neutral'.

inputs: Up(blue), Down(red), Neutral(cyan);

vars:   Active_TL_ID (-1),            // The ID of active TL

   condition1 = TL_GetActive <> (-1);      // trendline is active
   condition2 = TL_GetActive <> (-2);      // trendline has valid ID

if condition1 AND NOT condition2 then      // TL is active, and has Invalid ID
 // trendline has invalid ID.
 // whatever happens here

if condition1 AND condition2 then begin      // TL is active and has valid ID
   Active_TL_ID = TL_GetActive;

// Detect the slope of trendline --------------------------------------------
condition4 = TL_GetBeginTime_s(TL_GetActive) = TL_GetEndTime_s(TL_GetActive);
condition5 = TL_GetBeginDate(TL_GetActive) = TL_GetEndDate(TL_GetActive);
condition6 = TL_GetEndVal(TL_GetActive) = TL_GetBeginVal(TL_GetActive);
condition7 = (condition4 AND condition5 ) OR condition6;   // slope 'neutral'
condition8 = TL_GetEndVal(TL_GetActive) < TL_GetBeginVal(TL_GetActive);   // slope 'negative'
condition9 = TL_GetEndVal(TL_GetActive) > TL_GetBeginVal(TL_GetActive);   // slope 'positive'

   if condition9 then begin               // check for 'positive' slope
   NewTrendlineColor = Up;
   if condition8 then begin               // check for 'negative' slope
   NewTrendlineColor = Down;
   if condition7 then begin               // check for 'neutral' slope
   NewTrendlineColor = Neutral;
// slope detected  ----------------------------------------------------------

TL_SetColor(Active_TL_ID, NewTrendlineColor);      // Set trend line color

User avatar
Posts: 84
Joined: 17 Jun 2009
Location: USA
Has thanked: 55 times
Been thanked: 13 times

Postby TIKITRADER » 24 Mar 2010

This is great thank you works very nice. I wish I could help to get it to change color without a new tick but have no idea.
added simple input TL_Style to change trendline


Return to “MultiCharts”