How to Run batch for exploration and writing csv file 15 minutes

Hi

Is it possible to run batch every 15 minutes, as I saw it just support 1 hour?

My actual requirement is that I want to do the following every 15m.
1.run exploration.
2.export any exploration result to CSV file.

The below show
1

2

How to deal with this problem?

The other app will consume this file to do something.

Thank you
Ponthorn

See Batch manual where it is mentioned that you can also use ShellExecute to run batch file.

So you could apply below code to chart pane to run batch every n-minutes of hour.

batch_file = "path_to_batch_file.abb";// batch file location

//RequestTimedRefresh(1);// if local DB

x = (Now(4)/100)%100;
_TRACEF("Now (minutes):%g", x);

run_every = 15;// run every n-minutes of hour

if ( x % run_every == 0 ) {
	_TRACE("Batch run");
	ShellExecute("runbatch", batch_file, "" ); 
}

Or instead of n-minutes of hour you could also apply code after each new (real-time) bar creation:
https://www.amibroker.com/kb/2015/11/29/how-to-execute-part-of-the-formula-only-when-new-bar-is-added/

Or use Explorer to apply ShellExecute() every 15-minutes (see Auto-repeat setting therein Analysis window). But it may be 0... 15... 30.. 45 or 1... 16 ... 31... 46 or 2... 17... 32... 47 etc minute of hour depending on when you start initial exploration run.

8 Likes

3

It is ok.

Hi fxshrat,

That's a very nice way to automatically run batch. I found another posts that you replied, in which you said to create multiple 1 hour repeat tasks with 5 minutes interval to be very useful.

Now, I created an AFL with your code above, hit scan/exploration and I didn't see anything happen. Am I missing something there? My guess is that I have to change the name of the batch file? So I did change this line:

batch_file = "path_to_batch_file.abb";// batch file location

into this line with the name of my .abb file. However, the abb file is still not running.

batch_file = "Daily Exploration.abb";// batch file location

It would be great if you give me some clue on what part I can customize of your code to make it work for my system. Thanks again for your great work.

Yes, of course you have to change to your existing batch file.


The code is supposed to be run in chart pane!
It is clearly written in upper post:

I am sorry for I am still not familiar with the chart pane. I did saw that in your earlier post.
I tried to right click on the chart and only find this:
2020-11-25

How do you apply the code to the chart pane?
Otherwise, If you can give me a link to read more on it that would be nice.

AmBroker help has search function!

14

1 Like

I got your code running! It works when I double click the formula and it appears on my chart. That was silly of me :slight_smile:

However, I got another problem coming.
current run

I figured out that the reason I got this message was because I already set up 12 batch schedules for every 5 minute of the same .abb file.
My question is: do you have a way to limit the batch run time (every 1 minute) during a certain time period, for example from 9AM to 4PM only and only repeat on weekday?

Thanks again!

@fxshrat
Something strange happened. The problem I mentioned in my previous comment just keep popping up:
"There is other batch currently running or paused. Please waited until other batch finishes before starting new one"

I have already delete all of the previously set schedule. I wonder if it has anything to do with the code?

error

I have faced wtih the problem in the last year.
that is my way.
In the script below, you have to adjust something in the code especially

the comment
//=====Start and Close Market depending on what market you are trainding===//

//#pragma nocache


RequestTimedRefresh( 56, False ); 

robotName = "tfex_features_ls";   


//============================path file==============================
batchPath ="D:\\AB_DB";

log_file = batchPath+"\\log_" + robotName + ".log";  
batch_file = batchPath+"\\X_Batch_XXX.abb";


//paramExport2CSV= ParamTrigger("Export data to file", "Click here to Export");


fhLogFile = fopen(log_file, "a" ); // a = append    
function writeLog(message) {        
	if (fhLogFile) {        
		fputs( message + "\n" , fhLogFile );         
	}        
} 

// =================================Set Trade Time======================================================

modelTF = 15;       
amiTimeFrame = IIf(modelTF == 5, in5Minute, in15Minute);

// If you want to use other timeframe, please set the following 3 lines        
TimeFrameSet(amiTimeFrame);          
SetOption("StaticVarAutoSave", amiTimeFrame);

localTimeZone = 0;        


//barHour = IIf(localTimeZone == 0, Hour(), Hour() + 7);         
barHour = Hour();         
barMinute = Minute(); 

// =================================Start and Close Market depending on what market you are trainding======================================================       
// For Thailand  09:45-16:50  
// Check first bar        
isFirstBar = IIf( modelTF == 60 AND ( ( barHour >= 10 AND barHour < 11 ) OR ( barHour == 14 AND barHour < 15 )) ,1, 
           IIf( modelTF == 15 AND ( ( barHour < 10 ) OR   ( barHour == 14 AND barMinute < 30 )) , 1,
           IIf( modelTF == 5 AND( ( barHour == 9 AND barMinute < 50 ) OR( barHour == 14 AND barMinute < 20 ) ), 1, 0 ) ));
			
// Check last bar        
isLastBar =  IIf( modelTF == 60 AND( ( barHour == 12 ) OR( barHour == 16 ) ), 1,
             IIf( modelTF == 15 AND( ( barHour == 12 AND barMinute >= 15 ) OR( barHour == 16 AND barMinute >= 45 ) ), 1,
             IIf( modelTF == 5 AND( ( barHour == 12 AND barMinute >= 25 ) OR( barHour == 16 AND barMinute >= 50 ) ), 1, 0 ) )); 
 //===========================================================================================================================


lastBarTimeForTrade = Status("lastbarend");
recordTimestamp = Nz(StaticVarGet(Name() + robotName + "_lastBarTimeForTrade"));

// =================================Feed feature data to csv for ml prediction======================================================

if ( lastBarTimeForTrade != recordTimestamp AND LastValue(isFirstBar) == 0) {

timestamp = DateTimeToStr( Now(5) );
logLine = StrFormat("%s  Start to Explore Features",timestamp);
writeLog(logLine);


ShellExecute("runbatch", batch_file, "" ); 

StaticVarSet(Name() + robotName + "_lastBarTimeForTrade", lastBarTimeForTrade);

timestamp = DateTimeToStr( Now(5) );
logLine = StrFormat("%s Completed to Explore Features", timestamp);
writeLog(logLine);


}


// Close log file
if (fhLogFile) {
	fclose( fhLogFile );  
}
 
 
  _N(Title = StrFormat("Feed Features  ML V2{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); 
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 



















1 Like

Hi @technqvi
Thanks so much for sharing your code! I am trying to modify your code to make it work on my system, and so far, I have found a problem when I check the code, specifically the problem lies within the log_file that you call out at the beginning of your code.
What is that log file? Do you have to create that additional log file somehow manually?

Hi, guy

You have to change batchPath variable first.
batchPath variable ={which drive suh as C , D etc. }:\{your folder name}";

Keep in mind that
your file .abb that is value batch_file variable must be existing in batchPath.

1 Like

Thank you so much! It works just great.
The log feature is pretty cool! I can see exactly the time it runs.

1 Like

Hi @technqvi,

I tried to create a simple version of your code. I probably don't need the time limit for the code to run (I tried, I guess it doesn't work on my EOD data).

By combining your code with @fxshrat code that run every minute, I got the following code:

RequestTimedRefresh(5); 

robotName = "HaSepa";   

//============================path file==============================
//batchPath ="D:\\AB_DB"; C:\\Program Files (x86)\\AmiBroker\\Formulas\\Custom\\HA SEPA BATCH FORMULA\\Daily explorer.abb
batchPath ="C:\\Program Files (x86)\\AmiBroker\\Formulas\\Custom\\HA SEPA BATCH FORMULA";
log_file = batchPath+"\\log_" + robotName + ".log";  
batch_file = batchPath+"\\Daily explorer.abb";

fhLogFile = fopen(log_file, "a" ); // a = append    
function writeLog(message) {        
	if (fhLogFile) {        
		fputs( message + "\n" , fhLogFile );         
	}        
} 


x = (Now(4)/100)%100;
_TRACEF("Now (minutes):%g", x);

// run every n-minutes of hour
run_every = 1; 

/*/if ( x % run_every == 0 ) 
{
	_TRACE("Batch run");
	ShellExecute("runbatch", batch_file, "" ); 
}*/

if ( x % run_every == 0 ) 
{

timestamp = DateTimeToStr( Now(5) );
logLine = StrFormat("%s  Start to Explore Features",timestamp);
writeLog(logLine);

_TRACE("Batch run");
ShellExecute("runbatch", batch_file, "" ); 

StaticVarSet(Name() + robotName + "_lastBarTimeForTrade", lastBarTimeForTrade);

timestamp = DateTimeToStr( Now(5) );
logLine = StrFormat("%s Completed Explore Features", timestamp);
writeLog(logLine);

}

// Close log file
if (fhLogFile) 
{
	fclose( fhLogFile );  
}
 
 
_N(Title = StrFormat("Autorun batch v3.0 {{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) )); 
Plot( C, "Close", ParamColor("Color", colorblue ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

The modified code still work, but when I minimize amibroker, it seems to not automatically running. And then for a while, AB pop up that error, saying that there are more than 1 batch file running. I don't get why it shows that error since there should be only 1 batch file currently running.

2 batch window

Can someone please help :frowning:

My sample code is proper for Intraday

1 Like