AmiBroker AFL using Switch with DayOfWeek()

Discussion in 'App Development' started by DonStar, Dec 29, 2011.

  1. DonStar

    DonStar

    AmiBroker AFL

    DayOfWeek
    Syntax: DayOfWeek()

    Returns: ARRAY

    Function: Returns the array with day of week (0-6):
    0 - Sunday
    1 - Monday
    2 - Tuesday
    3 - Wednesday
    4 - Thursday
    5 - Friday
    6 - Saturday

    I want to test DayOfWeek() and depending on the number returned, assign the corresponding day of the week to a variable.

    I am thinking the Switch statement should do this but I am having trouble with the syntax.

    switch(DayOfWeek)
    {
    case 1:
    variable == Monday;
    case 2:
    variable == Tuesday;
    case 3:
    variable == Wednesday;
    case 4:
    variable == Thursday;
    case 5:
    variable == Friday;
    }

    What would be the correct way to code this?
     
  2. DonStar

    DonStar

    Let me clairfy just a bit.

    I want to enhance the Title section of my charts by inserting the day of the week in front of the date: Friday, 12/30/2011.

    The DayOfWeek() returns a number. So I need to convert the number to text that says what day it is.

    So...

    varDayOfWeek = DayOfWeek()

    switch(varDayOfWeek)
    {
    case 1:
    varDayOnChart == Monday;
    case 2:
    varDayOnChart == Tuesday;
    case 3:
    varDayOnChart == Wednesday;
    case 4:
    varDayOnChart == Thursday;
    case 5:
    varDayOnChart == Friday;
    }
     
  3. Don,

    How did you get your data to code for 0-6 to begin with. Are there data bases that code for day of the week?

    Thanks
     
  4. DonStar

    DonStar

    Hi Quickless,

    The AmiBroker AFL reference guide gives this information:

    DAYOFWEEK

    SYNTAX dayofweek()

    RETURNS ARRAY
    FUNCTION Returns the array with day of week (0-6):
    0 - Sunday
    1 - Monday
    ...
    5 - Friday
    6- Saturday

    EXAMPLE buy = dayofweek() == 1; // buy on Monday
    sell = dayofweek() == 5; // sell on Friday
     
  5. rdg

    rdg

    Try this:

    Code:
    function DayOfWeekName()
    {
    	dayInt = DayOfWeek();
    	dayStr = 
    		WriteIf(dayInt == 0, "Sunday", 
    		WriteIf(dayInt == 1, "Monday",
    		WriteIf(dayInt == 2, "Tuesday",
    		WriteIf(dayInt == 3, "Wednesday",
    		WriteIf(dayInt == 4, "Thursday",
    		WriteIf(dayInt == 5, "Friday",
    		WriteIf(dayInt == 6, "Saturday",
    			"Unknown")))))));
    
    	return dayStr;
    }
    
     
  6. dwrowley

    dwrowley

    If you are just interested in converting a single number into a day of the week, then you can simply use:

    DayOfWeekName = StrExtract( "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday", lastvalue( DayOfWeek() ) );

    This will give you the name of the day of the last bar (day if you are using end of day data)

    Note that you may get more expertise on the yahoo group for Amibroker - Tomasz, the programmer behind AB, regularly posts.

    HTH,
    David
     
  7. DonStar

    DonStar

    Thank you rdg:

    I plugged in the code as you suggested.

    It compiles without errors and I can apply it to the chart.

    The problem is that it doesn't do anything.

    I notice you are using a Return statement.

    Sorry, I am such a rookie, but what language do I need to get the result to print out?
     
  8. DonStar

    DonStar

    Thank you dwrowley: I input the code you suggested.

    It compiles and prints to the chart. The problem is that the result is always 'Friday.' It doesn't matter what day it really is.
     
  9. rdg

    rdg

    I use return because it's returning a value from a function. You can try to restructure the code so it doesn't use a function if you're looking for ways to learn syntax. Anyway, here it is with the Price.afl Title assignment modified to include the name:
    Code:
    function DayOfWeekName()
    {
    	dayInt = DayOfWeek();
    	dayStr = 
    		WriteIf(dayInt == 0, "Sunday", 
    		WriteIf(dayInt == 1, "Monday",
    		WriteIf(dayInt == 2, "Tuesday",
    		WriteIf(dayInt == 3, "Wednesday",
    		WriteIf(dayInt == 4, "Thursday",
    		WriteIf(dayInt == 5, "Friday",
    		WriteIf(dayInt == 6, "Saturday",
    			"Unknown")))))));
    
    	return dayStr;
    }
    
    _N(Title = "{{NAME}} - {{INTERVAL}} " + DayOfWeekName() + ", {{DATE}} " + StrFormat("Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
    
     
  10. DonStar

    DonStar

    Thank you rdg!

    This works great!
     
    #10     Jan 3, 2012