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.
//http://www.amibroker.com/members/library/detail.php?id=461//
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);
Daysinmonthfinal=IIf(Month()==2,Daysinmonthfeb,Daysinmonth);
returnvalue=IIf(Daysinmonthfinal-Day()<7 && DayOfWeek()==4,1,0);
return returnvalue;
}
ThirdThursd = DayOfWeek()==4 && (Day()>16 && Day()<24); //https://forum.amibroker.com/t/calculating-date-of-expiry/6534/2
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);
Filter=1;
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");
@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.
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.
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.