WriteIf Conditional error

_SECTION_BEGIN("3X5X34 Basic");

EMA3=EMA(C,3);
EMA5=EMA(C,5);
EMA34=EMA(C,34);
PerChg=(C-Ref(C,-1))/Ref(C,-1)*100;
ATR10=ATR(10);

no=Param( "Swing", 3, 1, 55 );
toc = Param( "TO Check", 5, 25, 750);
tocrore = toc *10000000;

TOcheck=(MA((OI*Aux1),50))>tocrore;


res=HHV(H,no);
sup=LLV(L,no);
tsl=IIf(ValueWhen(IIf((C>Ref(res,-1) AND EMA3>EMA5),1,IIf((C<Ref(sup,-1) AND EMA3<EMA5),-1,0))!=0,IIf((C>Ref(res,-1) AND EMA3>EMA5),1,IIf((C<Ref(sup,-1) AND EMA3<EMA5),-1,0)),1)==1,sup,res);
e=Ref(tsl,-1);
gd=(Ref(EMA3,-1)*(0.5-1)-Ref(EMA5,-1)*(0.333333 - 1))/(0.5 - 0.333333);
zd=(EMA(Close,3) * (2 / 4 - 1) - EMA(Close,5) * (2 / 6 - 1)) / (2 / 4 -2 /6);
Cond0=EMA(C,3)>EMA(C,5);
NR7 = (Ref(H,-1) - Ref(L,-1)) < Ref(LLV(H-L,7),-1);

Up=Max(zd,tsl);
Up1=Ref(Up,-1);
Down=Min(zd,tsl);
Down1=Ref(Down,-1);

_SECTION_END();


_SECTION_BEGIN("Conditions");

Buy = (Cross(C,Up) AND EMA3>EMA5) OR (Cross(C,Up) AND Ref(EMA3,-1)>Ref(EMA5,-1)) OR (Cross(C,Up) AND Ref(EMA3,-2)>Ref(EMA5,-2));
Sell = (Cross(Down,C) AND EMA3<EMA5) OR (Cross(Down,C) AND Ref(EMA3,-1)<Ref(EMA5,-1)) OR (Cross(Down,C) AND Ref(EMA3,-2)<Ref(EMA5,-2));


Short = Sell;
Cover = Buy;

Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );

BuyWatch=Cross(EMA3,EMA5);
SellWatch=Cross(EMA5,EMA3);

BuytoSell=Sell;
SelltoBuy=Buy;


BuyPrice=ValueWhen(SelltoBuy,Up1,1);
SellPrice=ValueWhen(BuytoSell,Down1,1);



rev=IIf(Buy OR Long,Down,Up);

CondSTBull =C>Ref(rev,-1);
CondSTBear =C<Ref(rev,-1);
STT= WriteIf(CondSTBull,"BULL","BEAR");


BuyPrice=ValueWhen(SelltoBuy,Up1,1);
SellPrice=ValueWhen(BuytoSell,Down1,1);

STT= WriteIf(CondSTBull,"BULL","BEAR");

EMA34prev = Ref(EMA(C,34),-1);
Cond34Buy = Ref(C,-1)<EMA34prev AND C>EMA34;
Cond34Sell = Ref(C,-1)>EMA34prev AND C<EMA34;

CondLTBull =C>EMA(C,34);
CondLTBear =C<EMA(C,34);
LTT= WriteIf(CondLTBull,"BULL","BEAR");

DOUBLEBULL = CondSTBull AND CondLTBull;
DOUBLEBEAR = CondSTBear AND CondLTBear;

NETT = WriteIf(DOUBLEBULL,"BULL",WriteIf(DOUBLEBEAR,"BEAR","SIDEWAYS"));


Filter=1;


AddTextColumn(FullName(),"F-Name");
AddColumn(C,"PClose",1.2);
AddColumn(PerChg,"Per",1.2);
AddColumn(rev,"STRevPrice",1.2);
AddTextColumn(STT,"STT");
AddColumn(EMA34,"LTRevPrice",1.2);
AddTextColumn(LTT,"LTT");
AddTextColumn(NETT,"NETT");
AddColumn( ATR10 , "ATR-10", 1.2 );
AddColumn(ADX(14),"ADX",1.2);
AddColumn(HHV(H,5),"5H",1.2);
AddColumn(LLV(L,5),"5L",1.2);

SetSortColumns( 1 ); 

_SECTION_END();

An afl for dynamic checking of trend based on 3X5 EMA and 34 EMA. While there are no syntax errors, the WriteIf conditions are failing:

Double Bull

Please, review the documentation for WriteIf()

https://www.amibroker.com/guide/afl/writeif.html

Please note that WriteIf returns just single string representing current SelectedValue of the EXPRESSION

This KB article will further explain the issue you are facing:
http://www.amibroker.com/kb/2014/12/22/text-output-in-explorations/

Therefore, such approach as above can only be used in situations where we run the exploration applied e.g. to 1-Recent bar, because it’s the last bar from the range which determines the text displayed in the column in such situation

Your conditions are returning arrays so the best way to do it in recent versions of AmiBroker is to use the AddMultiTextColumn() function

https://www.amibroker.com/guide/afl/addmultitextcolumn.html

It allows you to add a text column to the exploration where text displayed is choosen based on array value.
For boolean conditions arrays you have to pass to it a string built with the 2 desired values, for instance:

// AddTextColumn(STT,"STT");
AddMultiTextColumn(CondSTBull,"BULL\nBEAR", "STT");``

Thanks beppe, it almost resolved the issue; though a bit stuck up with the following line where mutliple possibilities are put up

NETT = WriteIf(DOUBLEBULL,“BULL”,WriteIf(DOUBLEBEAR,“BEAR”,“SIDEWAYS”));

The issue still persists:

_SECTION_BEGIN("3X5X34 Basic");

EMA3=EMA(C,3);
EMA5=EMA(C,5);
EMA34=EMA(C,34);
PerChg=(C-Ref(C,-1))/Ref(C,-1)*100;
ATR10=ATR(10);

no=Param( "Swing", 3, 1, 55 );
toc = Param( "TO Check", 5, 25, 750);
tocrore = toc *10000000;

TOcheck=(MA((OI*Aux1),50))>tocrore;


res=HHV(H,no);
sup=LLV(L,no);
tsl=IIf(ValueWhen(IIf((C>Ref(res,-1) AND EMA3>EMA5),1,IIf((C<Ref(sup,-1) AND EMA3<EMA5),-1,0))!=0,IIf((C>Ref(res,-1) AND EMA3>EMA5),1,IIf((C<Ref(sup,-1) AND EMA3<EMA5),-1,0)),1)==1,sup,res);
e=Ref(tsl,-1);
gd=(Ref(EMA3,-1)*(0.5-1)-Ref(EMA5,-1)*(0.333333 - 1))/(0.5 - 0.333333);
zd=(EMA(Close,3) * (2 / 4 - 1) - EMA(Close,5) * (2 / 6 - 1)) / (2 / 4 -2 /6);
Cond0=EMA(C,3)>EMA(C,5);
NR7 = (Ref(H,-1) - Ref(L,-1)) < Ref(LLV(H-L,7),-1);

Up=Max(zd,tsl);
Up1=Ref(Up,-1);
Down=Min(zd,tsl);
Down1=Ref(Down,-1);

_SECTION_END();


_SECTION_BEGIN("Conditions");

Buy = (Cross(C,Up) AND EMA3>EMA5) OR (Cross(C,Up) AND Ref(EMA3,-1)>Ref(EMA5,-1)) OR (Cross(C,Up) AND Ref(EMA3,-2)>Ref(EMA5,-2));
Sell = (Cross(Down,C) AND EMA3<EMA5) OR (Cross(Down,C) AND Ref(EMA3,-1)<Ref(EMA5,-1)) OR (Cross(Down,C) AND Ref(EMA3,-2)<Ref(EMA5,-2));


Short = Sell;
Cover = Buy;

Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );

BuyWatch=Cross(EMA3,EMA5);
SellWatch=Cross(EMA5,EMA3);

BuytoSell=Sell;
SelltoBuy=Buy;


BuyPrice=ValueWhen(SelltoBuy,Up1,1);
SellPrice=ValueWhen(BuytoSell,Down1,1);



rev=IIf(Buy OR Long,Down,Up);

CondSTBull =C>rev;
CondSTBear =C<rev;
dynamicSTcolor = IIf( CondSTBull, colorGreen, colorRed ); 


BuyPrice=ValueWhen(SelltoBuy,Up1,1);
SellPrice=ValueWhen(BuytoSell,Down1,1);

EMA34prev = Ref(EMA(C,34),-1);
Cond34Buy = Ref(C,-1)<EMA34prev AND C>EMA34;
Cond34Sell = Ref(C,-1)>EMA34prev AND C<EMA34;

CondLTBull =C>EMA(C,34);
CondLTBear =C<EMA(C,34);
dynamicLTcolor = IIf( CondLTBull, colorGreen, colorRed ); 

DOUBLEBULL = CondSTBull AND CondLTBull;
DOUBLEBEAR = CondSTBear AND CondLTBear;

SIDEWAYS = (CondSTBull AND CondLTBear) OR (CondSTBear AND CondLTBull); 


Blank = "$";

Filter=1;


AddTextColumn(FullName(),"F-Name");
AddColumn(C,"PClose",1.2);
AddColumn(PerChg,"Per",1.2);
AddColumn(rev,"STRevPrice",1.2);
AddMultiTextColumn(CondSTBull,"BULL\nBEAR", "STT");
AddColumn(EMA34,"LTRevPrice",1.2);
AddMultiTextColumn(CondLTBull,"BULL\nBEAR", "LTT");
//AddMultiTextColumn(SIDEWAYS,"SIDEWAYS \nBULL\nBEAR", "NTT");
AddColumn( ATR10 , "ATR-10", 1.2 );
AddColumn(ADX(14),"ADX",1.2);

_SECTION_END();

What is BULL is BEAR and vice-versaDouble Bull

Read How to add a single column that says oversold, overbought and neutral based on the RSI?

It seems to me that you have the some wrong code here:

AddMultiTextColumn(CondSTBull,"BULL\nBEAR", "STT");
...
AddMultiTextColumn(CondLTBull,"BULL\nBEAR", "LTT");

Your CondSTBull/CondLTBull arrays have a value of 1 for each bar when the condition is true and 0 when the condition is false.

From the doc of the AddMultiTextColumn function:

TextList - newline-separated list of texts to be displayed depending on ARRAY value. Note: newline is a “\n” symbol
This work so, when ARRAY value is zero, then first item from TextList is choosen and displayed, when ARRAY value is 1 then second item is choosen and so on.

So, in your case the string should be: “BEAR\nBULL”.

Yes indeed a logical shortfall. Did work now. Thanks Beppe.
While on the matter, as part of troubleshooting, is there a way to go through the code & use the columns “step by step”, something similar to BROWSE function in Foxpro or C? This would really help people like me who are not habitual coders…

You can use more extensively the explorations adding columns that will show you the intermediate values.

For instance you could do something like this to your code:

(partial snippet)

DOUBLEBULL = CondSTBull AND CondLTBull;
DOUBLEBEAR = CondSTBear AND CondLTBear;
SIDEWAYS = ((CondSTBull OR CondLTBull) AND (CondLTBear OR CondSTBear));
BitMask1 = Nz(DoubleBull) * 1; // Double bull 
BitMask2 = Nz(DoubleBear * 2); // Double bear
BitMask3 = Nz(Sideways * 3); // Sideway
SidewaySelector = Max(Max(BitMask1, BitMask2), bitMask3);

Blank = "$";

Filter=1;

AddTextColumn(FullName(),"F-Name");
AddColumn(C,"PClose",1.2);
AddColumn(PerChg,"Per",1.2);
AddColumn(rev,"STRevPrice",1.2);
// "Bear" is used for zero values - "Bull" for 1)
AddMultiTextColumn(CondSTBull,"BEAR\nBULL", "STT");
AddMultiTextColumn(CondLTBull,"BEAR\nBULL", "LTT");
// Start of code section to comment out in production 
AddColumn(CondSTBull, "STBull",1.0); 
AddColumn(CondLTBull, "LTBull",1.0); 
AddColumn(CondSTBear, "STBear",1.0);         
AddColumn(CondLTBear, "LTBear",1.0);         
AddColumn(DoubleBull, "D.Bull",1.0);         
AddColumn(DoubleBear, "D.Bear",1.0);         
AddColumn(Sideways, "Sideway",1.0);         
AddMultiTextColumn(SidewaySelector, stext, "NTT");
// End of code section to comment out in in production
stext = "N/A\n 1) DBL. BULL\n 2) DBL. BEAR\n 3) SIDEWAY\n";
AddColumn(sideWaySelector, "SwS", 1.0); // 
AddColumn( ATR10 , "ATR-10", 1.2 );
AddColumn(ADX(14),"ADX",1.2);

In this way, you can check all the values and figure out what is going on.
Another way is to use the debugger, but probably for you at present is not the easy way.

https://www.amibroker.com/guide/h_debugger.html

2 Likes