Not understanding what is going wrong with this if

I am pretty sure that IF I would understand everything, I could find the answer in the manual, but honestly, I just do not understand why the following is not working.

 If(DaysToThirdWednesday[0] > 0)
     
    {
		UntilNext = 3;
		SincePrev = 4;
    }
    
    else
    {
    	
    	SincePrev = 2;
    	untilnext = 1;
    }
  

My guess is that it has to do with the ; after the 3. This will therefore become the "if" option and the 4 will now become the "if not" option.

I hoped the { } would help with this...

But it does not... the script never gets to the "else". It will use the first 2 all the time.

It might also be something completely different.... that I am just using this completely wrong....

If someone could explain to me what I am not understanding, you are very welcome.

tia !

You are just outputting values at single element of array.
Use IIf() array function.

x = DaysToThirdWednesday;// array
is_greater_zero = x > 0;
UntilNext = IIf(is_greater_zero, 3, 1);
SincePrev = IIf(is_greater_zero, 4, 2);

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

Next you should check whether x ever becomes equal/smaller zero.
Use Explorer for example.

Thanks Fxshrat. I simplified the code hoping that I would understand it, and after still not understanding it, I posted my question. I understand your solution if my quest would be for the numbers but it is more complex. I am constantly working with the explorer, that is how I found that my code did not get to the else part....

  if(DaysToThirdWednesday[0] > 0)
    {
		untilNext = DaysToThirdWednesday;
		MonthNumber = Month();
		MonthNumber = MonthNumber -1;
		sincePrev = abs(DaysToThirdWednesday - 31 + (31 - DaysInMonth(MonthNumber,Year())));
    }
    
    if(DaysToThirdWednesday[0] <= 0)
    {
    	sincePrev = abs(DaysToThirdWednesday);
		untilNext = DaysToThirdWednesday + 31 - (31 - DaysInMonth(Month(),Year()));
    }

This is the original code. (however, there are some other errors in there). I want to use it to create a wieght to another data series. (It is for 2 futures, the front and next month, wieght is decided based on how far the expiration is)

So where I have put the numbers "1, 2, 3 and 4" I will use some calculations.

I just did not understand why it does not work.... (still don't by the way :slight_smile: for me if I translate it to normale language, "if this is true, do this, else do that" sounds like it should work :slight_smile:

If I try to use your example to the above code I would get this:

x = DaysToThirdWednesday;// array
	is_greater_zero = x > 0;

	MonthNumber = Month();
	MonthNumber = MonthNumber -1;

	UntilNext = IIf(is_greater_zero, DaysToThirdWednesday, DaysToThirdWednesday + 31 - (31 - DaysInMonth(Month(),Year())));
	
	SincePrev = IIf(is_greater_zero, abs(DaysToThirdWednesday), abs(DaysToThirdWednesday - 31 + (31 - DaysInMonth(MonthNumber,Year()))));

Totaly not sure if the month lines can be at that spot.... totaly not sure if all the rest will work as desired (not talking about the calculation because I know that has some errors) but atleast it verifies and runs and gives results in the explorer.

I will go through the explorer to see if this is indeed working as desired and try to solve my calculation errors... Hopefully that is now easier to spot since the code appears to run correctly.

Thanks for giving the solution !

Knipsel

You talked about "single element". Is this the single you mean ?

Trying to understand.... but it's just not my thing :slight_smile: and it is FAR from easy for me :slight_smile:

Funny thing is.... the help references Excel... in Excel this is all a lot easier for me :slight_smile:

Yes.
An array consists of n-number of elements (or think of Excel -> a row of elements).
Arrays in AFL are numbered arrays so each one consist of "row of numbers".

-> That one is not an array but a single number of array.
So if you add it to if-else statement then only single true/false case is returned.
It would be like picking result of single cell of spread sheet.

1 Like

Knipsel

Thanks for the explanation.

But this is what makes it so tough for me (and probably others that don't fully understand it all yet)

In the screenshot I see the row representing "DaysToThird Wednesday"

For me it looks like an array... It looks like "a row of elements" a "row of numbers"

I am confinced that at some point, I will understand it all... the when, how, when not, why's of AFL.

For now, I will just keep reading the help file and try to edit the code so it will do what I want it to do... hopefully :slight_smile:

In this case I want a single true/false for every bar, because it is used to calculate the remaining day's until the next expiration and the expired day's since the previous expiration so I guess I am on the right track :slight_smile:

Thanks again ! Appreciated !

Really please DO use the debugger: How do I debug my formula?

Add variables to "WATCH" window and then realize that pretty much everything in AmiBroker is an ARRAY (data series): Understanding how AFL works

As to Excel analogy, you need to understand that single variable in AFL is ENTIRE COLUMN (an array, not one cell) in Excel.

At some point, I am pretty sure I will understand.

I am using the explorer a lot, that is how I know things are not working and that I am doing something wrong. But knowing you are doing something wrong.... and understanding what the error is I am making, and knowing why it is wrong, and then finding the right solution.... that is something completely different :slight_smile:

As said, it will come, and thank you for the help getting there. Believe me, I, and probably many other users, are not unwilling, it's just very very very tough. Some get it in 5 minutes, others, well... this piece of code you are seeing there.... think I looked and tried to understand it, ran it in the explorer, went through every step... for atleast 3 hours.... The rest of the script, several hours more.

DaysToThirdWednesday = IIf (3-wd<0, (10-wd) % 7, (3 - wd) % 7);
		
	// thursday to saturday will be <0   in the 3-wd calculation.
	// sunday monday tuesday will be >0
	// wednesday will be equal
	// result for thursday =  (10 - 4) = 6 % 7 = 6
	//result for monday =  (3 - 1) = 2 % 7 = 2
	
	//lets run this for monday 18th oktober 2021 to see what the calculation does.
	// in oktober 2021 the 20th was the 3rd wednesday
	
	ThirdWednesday = ((d + DaysToThirdWednesday) % 7)+14;

	// monday before expiration is day 18, results for DTTW is 2 which would result in:
	//  ((18 + 2) %7)
	//  20 % 7 = 6 + 14 = 20
	
	// 20th is the 3rd wednesday.....
	
	// Running it for tuesday the 5th:
	
	// DTTW =  3-2 = 1  = >0  =  (3 - 2) = 1%7 = 1
	// TWD = 5 + 1 =6%7 = 6 + 14 = 20
	// 
	// THE CALCULATION WILL RETURN THE DATE FOR 3RD WEDNESDAY !!!!!!
	
		

    ThirdWednesday = IIf(ThirdWednesday==14, 19, ThirdWednesday); //corrects problem if the first of the month is a Saturday
    
    // Lets forget about this correction until we understand the rest of the code.
        
    
    DaysToThirdWednesday = (ThirdWednesday - d);
    
    // For monday the 18th this will be:   20 - 18 = 2
    // This will calculate how many days there are untill 3rd wednesday.
    
    // for tuesday the calculation will return 1. This is correct for the way we want to use the weighted contango

    // for wednesday the calculation will return 0 and the new month will need to be used.
    
    // thursday 21st oktober the calculation will be:
    // DaysToThirdWednesday = IIf (3-wd<0, (10-wd) % 7, (3 - wd) % 7);
    // wd = 4    3-4 = -1 =<0  (10-4) = 6 % 7 = 6
    // ThirdWednesday = ((d + DaysToThirdWednesday) % 7)+14;
    // (21 + 6) = 29 % 7 = 1 + 14 = 15
    // DaysToThirdWednesday = ThirdWednesday - d;
    // 15 - 21 = -7
    
    // thursday 16-9 the calculation will be:
    // DTTW 10-4 = 6 % 7 = 6
    // TW  16 + 6 = 22 % 7 = 1 + 14 = 15
    // DTTW 15 - 16 = -1
    
    
    
    
    x = DaysToThirdWednesday;// array
	is_greater_zero = x > 0;

	MonthNumber = Month();
	MonthNumber = MonthNumber -1;

	UntilNext = IIf(is_greater_zero, DaysToThirdWednesday, DaysToThirdWednesday + 31 - (31 - DaysInMonth(Month(),Year())));
	
	SincePrev = IIf(is_greater_zero, abs(DaysToThirdWednesday), abs(DaysToThirdWednesday - 31 + (31 - DaysInMonth(MonthNumber,Year()))));
    
    
   
    
    
    /*
      If(DaysToThirdWednesday[0] > 0)
     
    {
		UntilNext = 3;
		SincePrev = 4;
		//untilNext = DaysToThirdWednesday;  //correct
		MonthNumber = Month();
		MonthNumber = MonthNumber -1;
		//sincePrev = abs(DaysToThirdWednesday - 31 + (31 - DaysInMonth(MonthNumber,Year())));
    }
    
    else
    {
    	//sincePrev = abs(DaysToThirdWednesday + 31 - (31 - DaysInMonth(Month(),Year())));
    	SincePrev = 2;
    	untilnext = 1;
    }
  */
  /*
    
    if(DaysToThirdWednesday[0] > 0)
    {
		untilNext = DaysToThirdWednesday; 	// correct. For oct 18th this will be 2
		
		
		MonthNumber = Month();
		MonthNumber = MonthNumber -1;
		sincePrev = abs(DaysToThirdWednesday - 31 + (31 - DaysInMonth(MonthNumber,Year())));
		
		// addcolumn shows that daysinmonth is calculated correctly
		
		// sinceprev = (2 - 31) = -19 + (31 - 31
    }
    
  //  
    
    if(DaysToThirdWednesday[0] <= 0)
    {
    //	SincePrev = abs(DaysToThirdWednesday + 31 - (31 - DaysInMonth(Month(),Year())));
    	
    //	UntilNext = abs(DaysToThirdWednesday);
    	
    //	untilNext = (DaysInMonth(MonthNumber,Year()) - 	d) +	DaysInMonth(MonthNumber,Year());
    	sincePrev = abs(DaysToThirdWednesday);
    	
    	
	}
    
   

    */

See all the notes.... that is my way trying to troubleshoot, recalculating every step....

It's not unwillingness, it's just a user, and I am pretty sure I am not alone with this, that is having a lot of trouble understanding.

ps: Do understand very clearly that this is not a problem of AB or a problem of AFL, it's a problem of all users that are just not good at this but still would love to use all the AB abilities. Takes a lot more time for them than for many other users :wink:

I will continue my journey, thanks again for the help.

1 Like

Hi @henri - the array vs scalar issue is critical for you to fully understand before you continue. This thread might also help:

Play around with the various options in Tomasz's debugging thread. Limit your testing one or two variables to make sure that they contain the values that you are expecting.

You will get this. Keep at it. It is worth the effort.

1 Like

Thank you vmonkey, will read it.

I hope it is worth the effort... since sometimes I have the feeling I will get it by the time I am 65 years old... and do not need to invest anymore :slight_smile:

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