List of Dates from CSV into Array to avoid for BUY

Hello!

I would like Amibroker to open a CSV file and import/read a list of dates from one of its columns. I believe that I would need to create an array of these dates? I figured out how to read the column with fopen(), but not sure how to create the list/array here. How can I do that?

Once I have the list of dates loaded, I would like for the BUY signal to check if a bar is in this list/array and if so to avoid buying on all of the dates in the list. Is this possible?

Open the CSV file and for each date you look for the barindex() of the date, if found you check an array which will be used as a filter for your buy

bi = barindex();
OkToBuy = true;
InputDate = "2011-04-05"; 
foundDate = Lookup( bi, _DT( InputDate ), 0 ); 
if( not IsNull(foundDate) )
{
     OkToBuy[foundDate] = false;
}
2 Likes

Thank you very much for this! It's exactly what I was looking for.

Do you know why I am getting an error for the following:

row = "2,ALL_STOCKS,14,6,2017,2017-03-16";

bi = barindex();
OkToBuy = true;
InputDate = StrToDateTime(StrExtract( row, 5 ));
foundDate = Lookup( bi, _DT( InputDate ), 0 ); 
if( not IsNull(foundDate) )
{
     OkToBuy[foundDate] = false;
}

-> foundDate function expected a STRING, but found a NUMBER

InputDate = StrToDateTime(StrExtract( row, 5 ));
foundDate = Lookup( bi, InputDate, 0 ); 
// OR
InputDate = StrExtract( row, 5 );
foundDate = Lookup( bi, _DT( InputDate ), 0 ); 

3 Likes

Thank you! This works perfectly for daily bars, but how could I avoid 15 minute bars for example when providing only a date like "2017-03-16"?

Look for Lookup function in help. Read carefully the parameter mode it can help you find one bar of the day, then is just a matter of marking the rest of the day as not ok to buy

Do you think you could give me an example? Tried this in all possible ways, but no success...

Especially this: marking the rest of the day as not ok to buy

The example I gave you use mode 0 for lookup function which means "find exact match, otherwise return Null" if you use mode -1 it will return exact match, OR the nearest predecesor (if datetime is past last bar it will return last bar value)
So use lookup with mode -1, this will return index to the last intra bar of that day. Than just mark all the rest of the day

This works, but how do you mark the rest of the day?

There are many ways to mark the rest of the day, one way would be

if( not IsNull(foundDate) )
{
	dd = DateNum();
	DoNotBuyWhen = dd == dd[foundDate];
       OkToBuy = OkToBuy AND NOT DoNotBuyWhen;
}

2 Likes

Amazing! Thank you very much!

Follow up question to this:

How could I multiply the Open with a ChangeValue as below for the rest of the found date for Intraday? The code below is a start, but I'm not sure how to isolate all the bars to multiply.

(I would like to apply this to a CSV file with thousands of rows like this one).

row = "2,ALL_STOCKS,2017-03-16,50";

bi = barindex();
InputDate = StrToDateTime( StrExtract(row, 2 ));
ChangeValue = StrExtract( row, 3 );
foundDate = Lookup( bi, InputDate , -1 ); 
if( not IsNull(foundDate) )
{
	O[foundDate] = O[foundDate] * ChangeValue;
}

For what it is worth: your AFL formulas run with private copy of OHLC data, so if you modify them, you will only see those modification within the scope of that formula upto execution end. You won't be modifying actual database.

Yes, that is intentional, but thank you for the note.

row = "2,ALL_STOCKS,2018-03-05,50";

InputDate = StrToDateTime( StrExtract(row, 2 ));
// convert InputDate to DateNum
dnInputDate = DateTimeConvert(0, InputDate); 
// DateNum
dn = DateTimeConvert(0, DateTime());
CSVDate = dn == dnInputDate;
//Plot(CSVDate,"CSVDate", colorRed, styleHistogram | styleOwnScale);
Open = IIf(CSVDate, Open * ChangeValue, Open);

2 Likes