I am performing periodic task (for example, every 5 minutes) using below method. Is there any better alternative than below solution
# Start the periodic update self.update_interval = 5 # Update interval in minutes self.start_periperodic_update() # Schedule the end-of-day routine
def start_periodic_update(self):
"""
Start a periodic update of portfolio metrics using a timer-based approach.
"""
self.update_metrics() # Initial update call
self.schedule_next_update()
def schedule_next_update(self):
"""
Schedule the next update after the specified interval.
"""
self.timer = threading.Timer(self.update_interval * 60, self.update_metrics)
self.timer.start()
def update_metrics(self):
"""
Update portfolio metrics, called by the periodic timer.
"""
self.portfolioDrawDown.update_max_market_values_and_drawdowns()
self.objectStorageManagement.persist_to_disk()
# Schedule the next update after this one finishes
self.schedule_next_update()
def cancel_periodic_update(self):
"""
Cancel the periodic updates if needed (e.g., on shutdown).
"""
if hasattr(self, 'timer'):
self.timer.cancel()
Mia Alissi
Yes, there is an alternative way to perform periodic tasks in QuantConnect. You can use the
Schedule
function in the algorithm framework. This function allows you to schedule events to happen at specific times or intervals.Here is an example of how you can schedule a function to be called every 5 minutes:
In this example,
update_metrics
is the function that you want to call every 5 minutes. This function should contain the logic for updating your portfolio metrics.This approach is more suitable for QuantConnect's environment as it doesn't rely on threading, which might not be fully supported in the cloud-based trading engine.
You can find more information about scheduling functions in QuantConnect in the documentation.
Please note that the
Action
function is used in C# code. In Python, you can directly pass the function name without usingAction
.Here is the Python version of the code:
Sources:
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Dharmesh Khalasi
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!