Function for an AddColumn to show up in an exploration

I have a custom analysis that has several hundred indicators. I want to run an exploration that brings up the tickers in my watch list that has the conditions met and the conditions themselves. As an example, one of my indicators is

Filter = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top10 = WithinRange AND OpenPositionPercentage >= 90 AND IsCloseBelowSMA10 = IIf(Close < sma10, 1, 0);

If I was to name this in AddColumn, it would probably be something like "ATR 1 < ATR 5, Open In Top 10%, < SMA 10"

The exploration currently shows the symbol and then the indicators themselves with the column heading as the name of the exploration and if it's valid or not valid for that particular period of time as a 1 (true) or 0 (not true). I know that I cannot have hundreds of columns showing up in an exploration, hence, I only want to see the indicators that are true for that particular time. I imagine I'll have to run individual explorations for each ticker. How could I accomplish this? Thanks.

@edwardlevy, if I understand your post correctly, when running an exploration for a single instrument you can use something like the following example:

ma5 = EMA(C, 5);
ma50 = EMA(C, 50);
ma200 = EMA(C, 200);

condition1 = C > ma5;
condition2 = C > ma50;
condition3 = C > ma200;

bir = Status("barinrange");

c1cum = lastValue(cum(iif( bir, condition1, 0)));
c2cum = lastValue(cum(iif( bir, condition2, 0)));
c3cum = lastValue(cum(iif( bir, condition3, 0)));

Filter = 1;
if (c1cum)
	AddColumn(condition1, "C > MA(C, 5)", 1);
if (c2cum)
	AddColumn(condition2, "C > MA(C, 50)", 1);
if (c3cum)
	AddColumn(condition3, "C > MA(C, 200)", 1);
SetSortColumns(-2);

To test it use a single instrument: for instance, I used "AA" in the USA market.
In general, in the Analysis apply the formula to "Current" and try different ranges (like 10, 20 or 30 recent bars).

There may be more efficient methods, but at least you have something to work with.

Hi beppe,

This is what I was looking for. Thank you!

Ed

Why not use AFL Function Reference - ADDMULTITEXTCOLUMN ?

Maybe you are right, but the above line made me think of the posted approach.

Hey Beppe,

I'm trying to use your template to bring up only the indicators that are valid on any given day. My code isn't bringing up any results. I do get an error on line 72: Info 1001. The function LastValue() used here may be looking into the future. However, I'm getting this error with the code you put above, and your code yields results on the same symbols I'm running my code against. I messed something up, and it's probably minor. Do you see anything?

// Define Individual Indicators

// ATR calculations
atr1 = ATR(1);
atr5 = ATR(5);
atr25 = ATR(25);

// SMA calculations
sma200 = MA(Close, 200);
sma50 = MA(Close, 50);

IsSMA50AboveSMA200 = IIf(sma50 > sma200, 1, 0);
IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);

// Access ATR using daily timeframe
ATRValue = ATR(10);  // 10-day Average True Range

// Previous day values
prevClose = Ref(Close, -1);
prevHigh = Ref(High, -1);
prevLow = Ref(Low, -1);

// Conditions
closeAbovePrevHigh = Close > prevHigh;
closeBelowPrevLow = Close < prevLow;

// Calculate the previous day's range and adjustments for open positions
PrevRange = PrevHigh - PrevLow;
PrevRangeAdjusted = IIf(PrevRange == 0, 1, PrevRange);  // Adjust for zero range to avoid division by zero
OpenPositionPercentage = ((Open - PrevLow) / PrevRangeAdjusted) * 100;

// Define opening position classifications within the range
WithinRange = Open >= PrevLow AND Open <= PrevHigh;
Top10 = WithinRange AND OpenPositionPercentage >= 90;
Top25 = WithinRange AND OpenPositionPercentage >= 75 AND OpenPositionPercentage < 90;
TopHalf = WithinRange AND OpenPositionPercentage >= 50 AND OpenPositionPercentage < 75;
BottomHalf = WithinRange AND OpenPositionPercentage < 50 AND OpenPositionPercentage >= 25;
BottomQuarter = WithinRange AND OpenPositionPercentage < 25 AND OpenPositionPercentage >= 10;
Bottom10 = WithinRange AND OpenPositionPercentage < 10;

// Define classifications for opening above previous day's range
AboveRange = Open > PrevHigh;
ATRAboveRange = (Open - PrevHigh) / ATRValue;
OpenATR0to25 = AboveRange AND ATRAboveRange <= 0.25;
OpenATR25to50 = AboveRange AND ATRAboveRange > 0.25 AND ATRAboveRange <= 0.5;
OpenATR50to75 = AboveRange AND ATRAboveRange > 0.5 AND ATRAboveRange <= 0.75;
OpenATR75to1 = AboveRange AND ATRAboveRange > 0.75 AND ATRAboveRange <= 1;
OpenATRGreater1 = AboveRange AND ATRAboveRange > 1;

// Define classifications for opening below previous day's range
BelowRange = Open < PrevLow;
ATRBelowRange = (PrevLow - Open) / ATRValue;
OpenBelowATR0to25 = BelowRange AND ATRBelowRange <= 0.25;
OpenBelowATR25to50 = BelowRange AND ATRBelowRange > 0.25 AND ATRBelowRange <= 0.5;
OpenBelowATR50to75 = BelowRange AND ATRBelowRange > 0.5 AND ATRBelowRange <= 0.75;
OpenBelowATR75to1 = BelowRange AND ATRBelowRange > 0.75 AND ATRBelowRange <= 1;
OpenBelowATRGreater1 = BelowRange AND ATRBelowRange > 1;

// Conditions
condition1 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND WithinRange = Open >= PrevLow AND Open <= PrevHigh AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition2 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top10 = WithinRange AND OpenPositionPercentage >= 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition3 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top25 = WithinRange AND OpenPositionPercentage >= 75 AND OpenPositionPercentage < 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition4 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND TopHalf = WithinRange AND OpenPositionPercentage >= 50 AND OpenPositionPercentage < 75 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition5 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomHalf = WithinRange AND OpenPositionPercentage < 50 AND OpenPositionPercentage >= 25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition6 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomQuarter = WithinRange AND OpenPositionPercentage < 25 AND OpenPositionPercentage >= 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition7 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Bottom10 = WithinRange AND OpenPositionPercentage < 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition8 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR0to25 = AboveRange AND ATRAboveRange <= 0.25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);
condition9 = Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR25to50 = AboveRange AND ATRAboveRange > 0.25 AND ATRAboveRange <= 0.5 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);

bir = Status("barinrange");

c1cum = lastValue(cum(IIf( bir, condition1, 0)));
c2cum = lastValue(cum(IIf( bir, condition2, 0)));
c3cum = lastValue(cum(IIf( bir, condition3, 0)));
c4cum = lastValue(cum(IIf( bir, condition4, 0)));
c5cum = lastValue(cum(IIf( bir, condition5, 0)));
c6cum = lastValue(cum(IIf( bir, condition6, 0)));
c7cum = lastValue(cum(IIf( bir, condition7, 0)));
c8cum = lastValue(cum(IIf( bir, condition8, 0)));
c9cum = lastValue(cum(IIf( bir, condition9, 0)));

Filter = 1;

if (c1cum) AddColumn(condition1, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND WithinRange = Open >= PrevLow AND Open <= PrevHigh AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0)", 1);
if (c2cum) AddColumn(condition2, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top10 = WithinRange AND OpenPositionPercentage >= 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c3cum) AddColumn(condition3, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top25 = WithinRange AND OpenPositionPercentage >= 75 AND OpenPositionPercentage < 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c4cum) AddColumn(condition4, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND TopHalf = WithinRange AND OpenPositionPercentage >= 50 AND OpenPositionPercentage < 75 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c5cum) AddColumn(condition5, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomHalf = WithinRange AND OpenPositionPercentage < 50 AND OpenPositionPercentage >= 25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c6cum) AddColumn(condition6, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomQuarter = WithinRange AND OpenPositionPercentage < 25 AND OpenPositionPercentage >= 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c7cum) AddColumn(condition7, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Bottom10 = WithinRange AND OpenPositionPercentage < 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c8cum) AddColumn(condition8, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR0to25 = AboveRange AND ATRAboveRange <= 0.25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c9cum) AddColumn(condition9, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR25to50 = AboveRange AND ATRAboveRange > 0.25 AND ATRAboveRange <= 0.5 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);

bir = Status("barinrange");

c1cum = lastValue(cum(iif( bir, condition1, 0)));
c2cum = lastValue(cum(iif( bir, condition2, 0)));
c3cum = lastValue(cum(iif( bir, condition3, 0)));
c4cum = lastValue(cum(iif( bir, condition4, 0)));
c5cum = lastValue(cum(iif( bir, condition5, 0)));
c6cum = lastValue(cum(iif( bir, condition6, 0)));
c7cum = lastValue(cum(iif( bir, condition7, 0)));
c8cum = lastValue(cum(iif( bir, condition8, 0)));
c9cum = lastValue(cum(iif( bir, condition9, 0)));

Filter = 1;

if (c1cum) AddColumn(condition1, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND WithinRange = Open >= PrevLow AND Open <= PrevHigh AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0)", 1);
if (c2cum) AddColumn(condition2, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top10 = WithinRange AND OpenPositionPercentage >= 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c3cum) AddColumn(condition3, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Top25 = WithinRange AND OpenPositionPercentage >= 75 AND OpenPositionPercentage < 90 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c4cum) AddColumn(condition4, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND TopHalf = WithinRange AND OpenPositionPercentage >= 50 AND OpenPositionPercentage < 75 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c5cum) AddColumn(condition5, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomHalf = WithinRange AND OpenPositionPercentage < 50 AND OpenPositionPercentage >= 25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c6cum) AddColumn(condition6, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND BottomQuarter = WithinRange AND OpenPositionPercentage < 25 AND OpenPositionPercentage >= 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c7cum) AddColumn(condition7, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND Bottom10 = WithinRange AND OpenPositionPercentage < 10 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c8cum) AddColumn(condition8, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR0to25 = AboveRange AND ATRAboveRange <= 0.25 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);
if (c9cum) AddColumn(condition9, "Is1ATRLessThan5ATR = IIf(atr1 < atr5, 1, 0) AND OpenATR25to50 = AboveRange AND ATRAboveRange > 0.25 AND ATRAboveRange <= 0.5 AND IsSMA50BelowSMA200 = IIf(sma50 < sma200, 1, 0);", 1);

SetSortColumns(-2);

@edwardlevy your main issue is the section where you define the condition variables 1/9

I changed the first 3 to show you how to fix them:

//Create any additional variables that are used later in the condition 1/9
IsSMA50BelowSMA200 = sma50 < sma200;
Is1ATRLessThan5ATR = atr1 < atr5;

// Conditions displayed in the final exploration
condition1 = Is1ATRLessThan5ATR AND WithinRange AND IsSMA50BelowSMA200;
condition2 = Is1ATRLessThan5ATR AND Top10 AND IsSMA50BelowSMA200;
condition3 = Is1ATRLessThan5ATR AND Top25 AND IsSMA50BelowSMA200;
condition4 = ......

I haven't checked the rest of the code, so there may be other problems.

The message you get on line 72 is not an Error: it is a Warning: please review this thread.

In any case, to debug your formula, please, review carefully this other thread and in particular post 11 (from @KeithMcC) about "Divide and Conquer": if you start to write a very complex formula, when it does not what you expect, sometimes it is difficult to understand what is wrong.
Start small, test/verify (ideally using the exploration) all variables, and then add new code when everything previously completed works.

1 Like

Yep, that fixed it. Thanks!