Sebastian and bistrader,

That is a very clean, fast, and simple solution, Sebastian. I actually
prefer your method most of the time.

However, I kind of got carried away and wrote it the hard way  reading a
csv file directly with the string functions. This code could be used to
plot user-defined periods of any sort whose start and end dates are
stored in two columns in a csv file.

Please read the comments at the top of my  code so you know how the
begin and end recession dates should be  formatted in the csv file. I
wrote the dates for the recessions from  then nber.org website from 1900
to now, and just guessed that the  current recession would end at the
end of this year. You can find them in a comment below the main code.

  I fully debugged  this and it works. It basically parses the dates out
of a csv file  and then loops through to make a "isRecession" boolean
array, which is then plotted on the chart in gray area, as bistrader 
originally requested.

I hope you like this solution!

-Paul


CODE:


/*Coded By: Paul D. ( notanaiqgen...@yahoo.com )
Last Modified: 07/05/2010 using Amibroker 5.3
for Yahoo amibroker group message #150705 for bistrader

NOTE #1: CSV file must be in following format:

MM/DD/YYYY,MM/DD/YYYY
MM/DD/YYYY,MM/DD/YYYY
MM/DD/YYYY,MM/DD/YYYY

where the first column is the START Of the recession OR period AND
the Second column is the END Of the recession OR period

NOTE #2: be sure to feed the full folder AND file path with \\ slashs 
for folder/file
separators; it should look like this path string:
"C:\\abTestData\\recessions.csv"
You will need to make a folder called abTestData and put the
recessions.csv file
in there if you want my code to work "as-is"

NOTE #3: the Plot should be inserted as an OVERLAY.

NOTE #4: dates before 1900 won't work i don't think because i used "Easy
Language" style dates
which are in the format of YYYMMDD like 1101231. 01/01/1900 would be 
0000101, so can't go before
01/01/1900. Sorry didn't think about this beforehand. */

function GetRecessionDates(returnBeginOrEnd, fullPathAndFileName)
{    //if you want the BEGIN dates array to be returned, set 
returnBeginOrEnd to 0
     //if you want the END dates array to be returned, set 
returnBeginOrEnd to 1
     local recDateStr;
     local fh;
     recDateStr = "";
     fh = fopen(fullPathAndFileName,"r");
     if (fh)
     {
         while(!feof(fh)) //get all text/string from csv file
         {
             recDateStr += fgets(fh);
             recDateStr += ",";
         }
         fclose(fh);

         //_TRACE("the string: " + recDateStr);

         //parse string and put it into an numeric array that this 
function will return
         rDatesBEGINarr = 0; rDatesENDarr = 0;
         DateCntr = 0; rDatesBEGINcntr = 0; rDatesENDcntr = 0;
         slashCounter = 0;
         MonthNum = 0; DayNum = 0; YearNum = 0; tempDateNum = 0;
         i = 0;
         fp = 0;    //fp = first position
         while(i<StrLen(recDateStr))
         {
             if(StrMid(recDateStr,i,1)=="/")
             {
                 switch(slashCounter)
                 {
                     case 0: MonthNum = 
StrToNum(StrMid(recDateStr,fp,i-fp));break; //save month
                     case 1: DayNum = 
StrToNum(StrMid(recDateStr,fp,i-fp));break; //save day
                 }
                 slashCounter++;
                 fp = i + 1;
             }
             //save year + entire datenum
             if(StrMid(recDateStr,i,1)=="," &  StrMid(recDateStr,i-1,1)
!= ",")
             {
                 if(StrMid(recDateStr,i-1,1)=="\n")        //if previous 
char was newline char
                     YearNum = StrToNum(StrMid(recDateStr,fp,i-fp-1));
                 else                                        //else last 
char not newline char
                     YearNum = StrToNum(StrMid(recDateStr,fp,i-fp));
                 tempDateNum = 10000*(YearNum-1900) + 100*MonthNum + 
DayNum;
                 if(DateCntr % 2 == 0)
                 {
                     rDatesBEGINarr[rDatesBEGINcntr] = tempDateNum;
                     rDatesBEGINcntr++;
                 }
                 else
                 {
                     rDatesENDarr[rDatesENDcntr] = tempDateNum;
                     rDatesENDcntr++;
                 }
                 DateCntr++;
                 fp = i + 1;
                 slashCounter = 0;
             }
             i++;
         }
     }
     if(returnBeginOrEnd==0)
         returnArr = rDatesBEGINarr;
     else
         returnArr = rDatesENDarr;
     return returnArr;
}

myFolderFilePath = "C:\\abTestData\\recessions.csv";    //remember "\" 
is written as "\\"
rBegin = GetRecessionDates(0, myFolderFilePath);
rEnd = GetRecessionDates(1, myFolderFilePath);

/*tempStr = "";
for(i=0;i<BarCount;i++)
{
     tempStr = StrFormat("   rBegin[i]: %f " + "   rEnd[i]: %f", 
rBegin[i],rEnd[i]);
     _TRACE("i: " + i + tempStr);
}*/

/*Now make "isRecession" amibroker array that will have
"1" for recessions and "0" for no recession on that date*/
dn = DateNum();
isRecession = 0;
j = 0;
for(i=0;i<BarCount;i++)
{
     while(j < BarCount-1 & dn[i] > rEnd[j])
         j++;
     if(dn[i] >= rBegin[j] & dn[i] <= rEnd[j])
         isRecession[i] = 1;
     //tempstr = StrFormat("i: %f   datenum[i]: %f   isRecession[i]: 
%f",i,dn[i],isRecession[i]);
     //_TRACE(tempstr);
}

recBarColor = Param("RecessionColorNum",47,16,55,1);
Plot(isRecession, "RecessionOverlay", recBarColor, 
styleOwnScale|styleArea|styleNoLabel,0,1);

//PUT THESE DATES IN A CSV FILE CALLED "recession.csv"
/*
09/01/1902,08/31/1904
05/01/1907,06/30/1908
01/01/1910,01/31/1912
01/01/1913,12/31/1914
08/01/1918,03/31/1919
01/01/1920,07/31/1921
05/01/1923,07/31/1924
10/01/1926,11/30/1927
08/01/1929,03/31/1933
05/01/1937,06/30/1938
02/01/1945,10/31/1945
11/01/1948,10/31/1949
07/01/1953,05/31/1954
08/01/1957,04/30/1958
04/01/1960,02/28/1961
12/01/1969,11/30/1970
11/01/1973,03/31/1975
01/01/1980,07/31/1980
07/01/1981,11/30/1982
07/01/1990,03/31/1991
03/01/2001,11/30/2001
12/01/2007,12/31/2010
*/




--- In amibroker@yahoogroups.com, "sebastiandanconia"
<sebastiandanco...@...> wrote:
>
> Using a csv file was the way I solved this problem.  The AB code is
short and simple that way.
>
> I took the recession/expansion dates from:
>
> http://www.nber.org/cycles.html
>
> ...then created a csv file" named "^Recession Ribbon" (with the
data,open,high,low,close,volume,open interest format) with "1"s and "0"s
to denote expansions and recessions.
>
> This AB code will give you a yellow ribbon from the top to the bottom
of a price chart during recessions.
>
> REC=Foreign("^Recession Ribbon","C")==0;
> Plot(REC, "Recession",
colorYellow,styleOwnScale|styleArea|styleNoLabel , 0,1);
>
>
> Sebastian
>
> --- In amibroker@yahoogroups.com, "bistrader" bistrader@ wrote:
> >
> > Paul,
> >
> > Thanks.
> >
> > Two items.  First, I would like the overlay to extend from the top
to the bottom of the chart.  Is there a way to do this?  I remember
seeing a chart someplace that did this but can not find it.  Second, I
would like to place the state date and end date for each recession in a
csv, but is there a way to read the start and end dates (from column A
and B, respectively) into the arrays that you have?
> >
> > Thanks
> >
> > Bert
> >
> > --- In amibroker@yahoogroups.com, "notanaiqgenius" <notanaiqgenius@>
wrote:
> > >
> > > Hi, I made this indicator for you. Try applying as an overlay. Let
me
> > > know if that works for you.
> > >
> > > Paul
> > > -------------
> > >
> > > //RD stands for recession date
> > > StartRD1 = 1050101;
> > > EndRD1 = 1050631;
> > >
> > > StartRD2 = 1000401;
> > > EndRD2 = 1030101;
> > >
> > > StartRD3 = 1070701;
> > > EndRD3 = 1090301;
> > >
> > > isRecession = (DateNum()>=StartRD1 AND DateNum()<=EndRD1) OR
> > >                  (DateNum()>=StartRD2 AND DateNum()<=EndRD2) OR
> > >                  (DateNum()>=StartRD3 AND DateNum()<=EndRD3);
> > >
> > > myATR = ATR(50);
> > > recBarColor = Param("RecessionColorNum", 25,16,55,1);
> > >
> > > rOpen = IIf(isRecession,Open,Open);
> > > rHigh = IIf(isRecession,High+5*myATR,High);
> > > rLow = IIf(isRecession,Low-5*myATR,Low);
> > > rClose = IIf(isRecession,Close,Close);
> > >
> > > PlotOHLC( rOpen, rHigh, rLow, rClose, "RecessionOverlay",
> > >              recBarColor, styleCandle);
> > >
> > > --- In amibroker@yahoogroups.com, "bistrader" <bistrader@> wrote:
> > > >
> > > > I would like to create an overlay afl that has recession periods
in a
> > > color that can be selected via parameter with default of light
gray.  I
> > > can come up with the beginning and ending dates for each period,
but am
> > > not how to proceed.  Getting lost on how to best define (i.e.,
maybe 1
> > > if in recession and 0 otherwise) and how to best plot.  Help
> > > appreciated.
> > > >
> > >
> >
>

Reply via email to