Hello,
I am trying to run a backtest locally with my personnal data (I have every forex pairs in daily, hourly and on a minute basis). But I do not know what to do, my script is always crashing …
Here is the code I use to test:
from AlgorithmImports import *
class HourlyForexStrategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2023, 1, 1)
self.SetCash(100000)
# Ajouter les paires Forex EURUSD et AUDCAD en résolution horaire
self.eurusd = self.AddForex("EURUSD", Resolution.Hour).Symbol
self.audcad = self.AddForex("AUDCAD", Resolution.Hour).Symbol
# Initialiser les indicateurs de moyennes mobiles
self.sma_fast_eurusd = self.SMA(self.eurusd, 50, Resolution.Hour)
self.sma_slow_eurusd = self.SMA(self.eurusd, 200, Resolution.Hour)
self.sma_fast_audcad = self.SMA(self.audcad, 50, Resolution.Hour)
self.sma_slow_audcad = self.SMA(self.audcad, 200, Resolution.Hour)
def OnData(self, data):
# EURUSD: Achat si la SMA rapide croise au-dessus de la SMA lente
if self.sma_fast_eurusd.Current.Value > self.sma_slow_eurusd.Current.Value and not self.Portfolio[self.eurusd].Invested:
self.SetHoldings(self.eurusd, 0.5)
elif self.sma_fast_eurusd.Current.Value < self.sma_slow_eurusd.Current.Value and self.Portfolio[self.eurusd].Invested:
self.Liquidate(self.eurusd)
# AUDCAD: Achat si la SMA rapide croise au-dessus de la SMA lente
if self.sma_fast_audcad.Current.Value > self.sma_slow_audcad.Current.Value and not self.Portfolio[self.audcad].Invested:
self.SetHoldings(self.audcad, 0.5)
elif self.sma_fast_audcad.Current.Value < self.sma_slow_audcad.Current.Value and self.Portfolio[self.audcad].Invested:
self.Liquidate(self.audcad)
How can I update it to use my own personnal data files?
Mia Alissi
To use your personal data files in a QuantConnect algorithm, you need to create a custom data class and modify your algorithm to use this class. Here's how you can update your code:
Create a Custom Data Class: Define a class inheriting from
PythonData
to handle your custom data. Implement theget_source
andreader
methods to specify the data source and parsing logic.Modify the Algorithm: Use the
AddData
method to add your custom data class to the algorithm.Here's an example of how you can implement these steps:
Key Points:
CustomForexData
class handles reading your CSV files. Adjust the file path and CSV parsing logic according to your data format.AddData
to include your custom data in the algorithm.This setup should help you run your backtest using personal data files. Adjust the file paths and parsing logic as needed to fit your data structure.
Sources: Custom Data - QuantConnect.com, Key Concepts - QuantConnect.com, Key Concepts - QuantConnect.com, CSV Format Example - QuantConnect.com, Custom Data - QuantConnect.com
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.
Adrien Aubourg
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!