Hi all,
I would like to combine custom and quantconnect data in a custom data source. My use case: the KMLM ETF was issued End of 2020. I have more historical data from the underlying index. Therefore, I would like to combine both in a custom datasource that uses historical index data before 2021, and quantconnect data for the ETF after 2021-1-1. Since the algo trades a lot of different tickers, i would like to combine both data source into a single custom data source.
Yet, I cannot figure out, how to access quantconnect data inside the custom data function. Here is my current approach. Any suggestions are highly appreciated:
import pandas as pd
from datetime import datetime
from AlgorithmImports import *
class HybridKMLMXData(PythonData):
def __init__(self):
super().__init__()
self.equity_symbol = Symbol.Create("KMLM", SecurityType.Equity, Market.USA)
def GetSource(self, config, date, isLiveMode):
source = "https://gist.githubusercontent.com/chrswrmr/3a7522de7c9429e6dc2c8d29627e6e33/raw/510aeeaa5dc64dbd2e59ee5138ac66edff9018ca/KMLMX_rev.csv"
return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
try:
if not line.strip() or line.startswith("date"):
return None
parts = line.split(",")
parsed_date = pd.Timestamp(parts[0])
if parsed_date < datetime(2021, 1, 1):
data = HybridKMLMXData()
data.Symbol = config.Symbol
data.Time = parsed_date
value = round(float(parts[1]), 4)
data.Value = value
data.Close = value
return data
else:
data = HybridKMLMXData()
data.Symbol = config.Symbol
data.Time = parsed_date
try:
equity_data = SubscriptionManager.GetDataPoint(self.equity_symbol, date, Resolution.Daily)
if equity_data is not None:
data.Value = equity_data
data.Close = equity_data
else:
data.Value = float(parts[1]) # Fallback to CSV data
data.Close = float(parts[1])
except:
data.Value = float(parts[1]) # Fallback to CSV data
data.Close = float(parts[1])
return data
except Exception as e:
print(f"Error processing data: {e}")
return None
class CustomDataAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 12, 1)
self.SetEndDate(2021, 1, 31)
self.SetCash(100000)
# Only add the hybrid data source
self.symbol = self.AddData(HybridKMLMXData, "KMLMX", Resolution.Daily).Symbol
def OnData(self, slice):
# Access KMLMX data
data = slice.Get(HybridKMLMXData).get(self.symbol)
if data:
self.Log(f"KMLMX - Date: {data.Time}, Close: {data.Close}")
Mia Alissi
Sources: Custom Data - QuantConnect.com, Key Concepts - QuantConnect.com, Custom Data Type History Algorithm, Requesting Data - QuantConnect.com, Key Concepts - 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.
Kryz
The main problem I see is in the reader method where you're trying to call self.History(). In a PythonData class, you don't have direct access to the QCAlgorithm's History method. The PythonData class doesn't inherit from QCAlgorithm. How can this be fixed?
Kryz
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!