Not saving data into a file for last hour from analysis with auto repeat

I use purchased AmiBroker software. I wrote an afl program to save basic information of a bar like O, C, H, L, MACD, RSI with 5 min period into a file. I created a new analysis opened this afl program and set 5min periodiocity and 5min auto repeat. I open file using append ("a" in fopen) mode. The data gets saved properly but not saving the last hour data from 2:30 to 3:30 PM. But the data is getting updated in the chart views. Am I missing something? Any help will be greatly appreciated.

@sdonthu if you would like to get specific answers to solve this problem, I suggest you post your code.

Otherwise, if no other user already had similar experiences, the possibility of receiving some useful feedback seems very limited.

Hi Beppe,

Thanks for your reply. Below is the code I used. (Sorry, Indentation missing in preview. All tabs and spaces removed.)

r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 200, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
MCD = MACD(r1, r2);

TimeArray = TimeNum();
periodsR = Param( "PeriodsR", 31, 1, 200, 1 );
periodsS = Param( "PeriodsS", 15, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
Dsmooth = Param( "%D avg", 3, 1, 200, 1 );
RSI1 = RSI(periodsR);
STC = StochD(periodsS , Ksmooth, DSmooth);

India = False;
TimeNow =  Now(4);

YearInd = Now(8);
MonthInd =  Now(7);
DayInd =  Now(6);
IndHour = int(TimeNow/10000);

if (!India)
{
	HourNow = Int(TimeNow/10000);
	Remain = TimeNow%10000;
	MinNow = Int(Remain/100);
	secNow = Remain%100;
	TotMinSince12AM = HourNow*60+MinNow;
	TotMinForInd = TotMinSince12AM + 570; // 9.5*60; EST to India time diff in Min
	if (TotMinForInd > 1440)  // 24*60
	{
		TotMinForInd -= 1440;
		DayInd++;
		if (MonthInd==2)
		{
			if (DayInd>28 AND YearInd%4 != 0)
			{
				DayInd=1;
				MonthInd++;
			}
		}
		else
		{
			if (MonthInd==1 OR MonthInd==3 OR MonthInd==5 OR
				MonthInd==7 OR MonthInd==8 OR MonthInd==10 OR 
				MonthInd==12)
			{
				if (DayInd>31)
				{
					DayInd=1;
					MonthInd++;
				}
			}
			else
			{
				if (DayInd>30)
				{
					DayInd=1;
					MonthInd++;
				}
			}
		}
		if (MonthInd>12)
		{
			MonthInd=1;
			YearInd++;
		}
	}
	IndHour = int(TotMinForInd/60);
	IndMin = TotMinForInd%60;
	TimeNow = IndHour*10000 + IndMin*100 + secNow;
}
TimeNowStr = "" + TimeNow;
if (TimeNow < 100000)
	TimeNowStr = "0" + TimeNowStr;
BarTime = LastValue( TimeArray );
BarTimeStr = "" + BarTime;
if (BarTime < 100000)
	BarTimeStr = "0" + BarTimeStr;
BarHour = int(BarTime/10000);

FileName = "C:\\StockData\\RT_Data\\5Min_Data\\" + YearInd + "\\";
Mnth = MonthInd;
if (Mnth < 10)
	FileName += "0";
FileName += "" + Mnth + "\\";
Day1 = DayInd;
if (Day1 < 10)
	FileName += "0";

FileName += "" + Day1 + "\\" + Name() + ".csv";

fo = fopen(FileName, "a");
if (fo)
{
	//fputs("TIME,SIGNAL_TIME,OPEN,CLOSE,HIGH,LOW,VOLUME,MACD,RSI,STC,End\n", fo);
	j = BarCount-1;
	fputs(TimeNowStr + "," + BarTimeStr + "," + O[j] + "," + C[j] + "," + H[j] +
			"," + L[j] + "," + V[j] + "," + MCD[j] + "," + RSI1[j] + "," + STC[j] +
			",End\n", fo);
	fclose(fo);
}

Moderator comment: Added code tags

@sdonthu when you post a piece of code you should always try to it in the right way.1

Please, employ the code tags as explained clearly in the Enter the AFL code properly section, but ABOVE ALL you should do your best effort to post a piece of code that will execute with no syntax errors (if a forum user will copy your formula to the code editor it should be immediately testable).

Unfortunately, your code contains multiple errors like:

  • Variables used without being assigned
  • Incorrect use of backslashes in filenames

So, please, fix the syntax mistakes, and next time paste it here using the code tags.

Then, kindly, add a short explanation about the purpose of the calculation you are doing in the

if (!India)
{
     ...
}

conditional block. This is not clear to me since I live in Italy and I'm not familiar with India timezones!
As far as I understand it, after a specific hour (14:40) you add a day to you time variables (and when needed you also increment the month resetting the day to 1).

If the calculations are the correct ones (i did NOT test your code) then the data for "the last hour" is expected to be written to a different file (the fopen() append mode "a" will create a new file if it does not already exist).

I see that in your logic you use a folder structure where each new file is stored in its own folder (build using Year\Month\Day variables).

Does the folder for the "next" (tomorrow) day already exists when you run tour formula "today"?

If the directory does not exist the fopen() will fail :cry:
So, to help to debug the issue, I suggest also adding some code that will check/report for file creation errors (at least use a _TRACE() and examine the log)

For example:

filename = "C:\\non_existing_folder\\test.txt";
fh = fopen( Filename, "a" );

if( fh )
{
    fputs( "Testing " + Filename, fh );
    fclose( fh );
}
else  // REPORT errors to the log
{
    _TRACE( Now( 0 ) + " - Error writing file [" + filename + "]" );
}


In any case, here below, are some additional observations to fix your syntax issues and simplify the code.

When assigning filenames you should use 2 backslashes sequences:

fh = fopen("C:\windows\test.txt", "w"); //  Error 54 - incorrect espace sequence WRONG - a single backslash should be encoded as \\ 
fh = fopen("C:\\windows\\test.txt", "w" ); // CORRECT 

Moreover I see that you build the filename in multiple steps.
I suggest learning how to use the StrFormat() to simplify the code (it will take care to add the "0" to single d/m digits as needed).
For example::

Version (6.20); // Starting from version 6.20, printf/StrFormat support now "%s" (string specifier) 

// Fake values to test the StrFormat function - change them to see how it works
YearInd = 2018;
MonthInd = 8; // 11 
DayInd = 2;

Filename = StrFormat("C:\\StockData\\RT_Data\\5Min_Data\\%04.f\\%02.0f\\%02.0f\\%s.csv", 
				YearInd, MonthInd, DayInd, Name());
_TRACE(Filename);



1) Ideally, reindent your code before posting in the forum; remove all leading spaces and tabs and then use the AmiBroker code editor "Edit -> Prettify Selection" menu item.

4 Likes

@sdonthu, I noted that your code (after the moderator added the code tags) now is free of syntax errors.

This is one of the reasons why it is imperative always to use them. Their absence, in combination with particular characters sequences (commonly found in a piece of source code), could result in a "rendered" text that looks different from what was initially typed!

(Unfortunately, I did not remember about this already known problem when I provided my previous answer. I apologize).

So, please, disregard the comments about the errors to fix, but verify if adding a day after 14:40 may be the cause of the issue you are facing; I suspect that the formula fails to write the "last hour" data since the folder where to write a new file still does not exists.

In any case, add the error reporting section to the section where you write data to the .csv file.

1 Like