Conditional textColumns Rowwise

Dear Team,

I am working on the following exploration wherein I wish to go row wise in calculations and then based on condition create a TextColumn

_SECTION_BEGIN("Sigma Screener - MultiBar FINAL");

LookbackPeriod = Param("Lookback for Stats", 252, 10, 500, 1);
MAPeriod = Param("MA Period", 200, 10, 500, 1);

// 1. Calculations
DEMA200 = EMA(C, MAPeriod);
VarPer = ((C - DEMA200) / DEMA200) * 100;
MeanVar = MA(VarPer, LookbackPeriod);
SDev = StDev(VarPer, LookbackPeriod);

V3P = MeanVar + (3 * SDev);
V2P = MeanVar + (2 * SDev);
V2M = MeanVar - (2 * SDev);
V3M = MeanVar - (3 * SDev);

// 2. Numerical State - The "Single Source of Truth"
// 1=+3SD, 2=+2SD, 3=-3SD, 4=-2SD
State = IIf(VarPer > V3P, 1, 
        IIf(VarPer < V3M, 3, 
        IIf(VarPer > V2P, 2, 
        IIf(VarPer < V2M, 4, 0))));

// 3. Exploration Setup
Filter = State > 0;
SetSortColumns(1, -2); // Ticker (A-Z), then Date (Newest first)

AddColumn(C, "Close", 1.2);
AddColumn(VarPer, "% Var/MA", 1.2, colorDefault, IIf(VarPer > 0, colorPaleGreen, colorRose));
AddColumn(State, "State Num", 1.0); 

// 4. Color Logic - Strictly tied to State
S_Color = IIf(State == 1, colorGreen, 
          IIf(State == 2, colorBlue, 
          IIf(State == 3, colorRed, 
          IIf(State == 4, colorOrange, colorDefault))));

// 5. THE FIX: Direct Text Mapping
// We put the logic INSIDE AddTextColumn to force row-by-row evaluation
AddTextColumn(
    WriteIf(State == 1, "EXTREME OVERBOUGHT (+3SD)",
    WriteIf(State == 2, "OVERBOUGHT (+2SD)",
    WriteIf(State == 3, "EXTREME OVERSOLD (-3SD)",
    WriteIf(State == 4, "OVERSOLD (-2SD)", "Normal")))),
    "Sigma Flag", 1.0, colorWhite, S_Color, 200);

// Boundary values for verification
AddColumn(V3P, "Value +3SD", 1.2, colorGreen);
AddColumn(V2P, "Value +2SD", 1.2, colorBlue);
AddColumn(V2M, "Value -2SD", 1.2, colorOrange);
AddColumn(V3M, "Value -3SD", 1.2, colorRed);

_SECTION_END();

The code is running right till the State, but falters once it outputs the TextColumn.

It marks the latest value. Astonishingly the Color is being populated rightly

Am I missing out on something?

@pushkan I recommend studying the documentation for the AddMultiTextColumn()
function and searching this forum for examples of its use.

2 Likes

@beppe is correct. You cannot use AddTextColumn to output different text in different rows for the same symbol.

As always @beppe helps out. Kudos & Thanks a ton.
Here is the part code for reference

_SECTION_BEGIN("Sigma Screener - AddMultiText Fix");

LookbackPeriod = Param("Lookback for Stats", 252, 10, 500, 1);
MAPeriod = Param("MA Period", 200, 10, 500, 1);

// 1. Calculations
DEMA200 = EMA(C, MAPeriod);
VarPer = ((C - DEMA200) / DEMA200) * 100;
MeanVar = MA(VarPer, LookbackPeriod);
SDev = StDev(VarPer, LookbackPeriod);

V3P = MeanVar + (3 * SDev);
V2P = MeanVar + (2 * SDev);
V2M = MeanVar - (2 * SDev);
V3M = MeanVar - (3 * SDev);

// 2. Numerical State (Starting at 0 for the List)
// 0=Normal, 1=+3SD, 2=+2SD, 3=-3SD, 4=-2SD
State = IIf(VarPer > V3P, 1, 
        IIf(VarPer < V3M, 3, 
        IIf(VarPer > V2P, 2, 
        IIf(VarPer < V2M, 4, 0))));

// 3. Exploration Setup
Filter = State > 0;
SetSortColumns(1, -2); 

AddColumn(C, "Close", 1.2);
AddColumn(VarPer, "% Var/MA", 1.2);

// 4. THE FORUM FIX: AddMultiTextColumn
// We define the list with \n as the separator
TextList = "Normal\nEXTREME OVERBOUGHT (+3SD)\nOVERBOUGHT (+2SD)\nEXTREME OVERSOLD (-3SD)\nOVERSOLD (-2SD)";

// Color Logic stays the same
S_Color = IIf(State == 1, colorGreen, 
          IIf(State == 2, colorBlue, 
          IIf(State == 3, colorRed, 
          IIf(State == 4, colorOrange, colorDefault))));

// ARRAY (State) decides on a bar-by-bar basis which item is chosen
AddMultiTextColumn(State, TextList, "Sigma Flag", 1.0, colorWhite, S_Color);

_SECTION_END();

Request do correct if there is some logically glitch or error

1 Like