Issue with WriteIf

Dear All,

I am facing a weird issue in one of the below snippets when trying to convert it on string based on the condition, While debugging the results are corrects however, when the same logic is applied to writeif I am not getting the correct result, maybe I missing something which I am not been able to figure out.

function Lastthursday()
 Daysinmonth=IIf(Month()==1 | Month()==3 | Month()==5 | Month()==7 | Month()==8 | Month()==10 | Month()==12,31,30);
 Daysinmonthfeb=IIf(Year()%4 == 0 && Year()%100!=0,29,28);
 returnvalue=IIf(Daysinmonthfinal-Day()<7 && DayOfWeek()==4,1,0);
 return returnvalue;
ThirdThursd = DayOfWeek()==4 && (Day()>16 && Day()<24); //

BarfrmThridThurs = BarsSince(ThirdThursd);
condtrue = IIf(Ref(BarfrmThridThurs,-1)<5 && !Ref(Lastthursday(),-1),1,0); // keep the condtion true for the period

Ext = WriteIf(condtrue,"","WK"); // if the condition is true replace the symbol with "" else replace with "WK" 
Symbol = StrReplace(Name(),"-FUT",Ext);

string = "NoSignal\nNIFTY\nNIFTYWK";
Selectstring = 1*(condTrue==1) + 2*(condTrue==0);
AddColumn(thirdThursd, "thirdThursd", 1.0, colorDefault, IIf(thirdThursd, colorGreen, colorDefault));
AddColumn(Lastthursday(), "Lastthursday", 1.0,colorDefault, IIf(Lastthursday(), colorGreen, colorDefault));
AddColumn(condtrue, "returnTrue", 1.0);
AddMultiTextColumn(Selectstring, string, "Which-Symbol");
AddtextColumn(Symbol, "Symbol");


There is not any issue with WriteIf function!

You just have to read documentation carefully.

Tomasz Janeczko

2005-08-10 06:37:55 Please note that WriteIf returns just single string representing current SelectedValue of the EXPRESSION

There is not array of strings.

Also AddtextColumn does not return array of strings.

@fxshrat thanks for the revert, I have gone through Writeif function & in fact used it on several occasion, as you rightly said there isn't any issue with Writeif function as on other occasion i have succesfully used it, may be i have not been able elaborate my issue clearly.

based on below condition

I am facing an issue while trying to replace the string so that automatically changes my symbol based on condtrue.

However, when i debug the same logic through AddMultiTextColumn I am getting the proper result.

Hence I am not been able to figure out where am I going wrong.



Apparently you don't understand.

I am repeating once again:

... in AFL.

In an array a value is a single element of array!
An array is a vector consisting of n-number of elements.
If expression is array then WriteIf does not return results per each element of array but per selected one (single element). In analysis selected value is last bar of range.

Try to understand difference between value/element and array

So what's the problem. You have to use AddMultiTextColumn but NOT Writeif() and NOT AddTextColumn if you want to output (changing) string result per each element of array.

AddMultiTextColumn has been implemented for what you want to do!

The logic behind converting and replacing the string-based on "condtrue" on the symbol is because I need to further use it to customized the symbol for the Foreign function so that I can fetch the data of the Foreign symbol for calculation and not for exploration.

Currently, I am doing this manually every time on the third Thursday I have to change the symbol from weekly to monthly to fetch the correct symbol data and roll back from monthly to weekly on the last Thursday of the month.

//Symbol = StrReplace(Name(),"-FUT","");
Symbol = StrReplace(Name(),"-FUT","WK");
newDay = Day()!= Ref( Day(), -1 );
// Call Option Price for Calculation
iCEC = Foreign(Symbol+iCE+"CE","C"); // In the Money Price
iCEV = Foreign(Symbol+iCE+"CE","V"); // In the Money Volume
iCEV1 = SumSince(newday,iCEV) + ValueWhen(newday,iCEV);

Hence AddMultiTextColomn may not be solving my purpose, the reason I used it only for debugging to see if the logic work.

Any workaround to achieve the above goal other than Writeif.



Use IIf() function.

price = IIf(condTrue, Foreign(symbol1,"C"), Foreign(symbol2,"C"));


This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.