Anh,
Here's an example that does analysis at market close and then trades, based on the analysis, at market open. If I'm understanding correctly, you should be able to devise a 'learn_period' with schedule_function. Instead of doing the analysis every day, as I do, you could do it every 60 days, for example. So long as your computation takes no more than 50 seconds, it should work.
However, I would wonder if 'under the hood' history is getting updated every bar anyway? The clever Q engineers may have written code that 'sees' the history call within the function upon algo build, and then sets up to accumulate bar data in the background, as it arrives (in essence, history is always kept 'warm'). So, when you call history, the data are already in RAM; they aren't retrieved from disk. It isn't clear to me that there is any "running" of history; I suspect that the data are there automatically, whether it is called or not.
Grant