Can anyone help me figure out why my spx closing price is off by between 1 and 100 cents on every bar. seems simple to just get the closing prices, yet none of them match either ToS or yahoo official spx close prices:
from QuantConnect.Data.Market import TradeBar
from QuantConnect import Algorithm, Resolution
class spx_closing_price_algorithm(Algorithm.QCAlgorithm):
def initialize(self):
self.set_start_date(2025, 1, 1) # Start date: January 1, 2025
self.set_end_date(2025, 1, 31) # End date: January 31, 2025
self.set_cash(10000) # Set initial cash (not used for this purpose)
# Add SPX index data (daily resolution)
self.spx = self.add_index("SPX", Resolution.Daily).symbol
# Log the symbol to confirm we're using the correct one
self.log(f"Using symbol: {self.spx}")
# Variable to store the previous day's close
self.previous_close = None
def on_data(self, data: TradeBar):
# Check if we have SPX data for the current time
if data.contains_key(self.spx):
# Get the closing price
close_price = data[self.spx].close
# Log detailed information
self.log(f"Date: {self.time.date()} | SPX Closing Price: {close_price} | Data Type: {type(data[self.spx])}")
# Store the current close for the next iteration
self.previous_close = close_price
Mia Alissi
Below is a clear explanation of why the closing price for SPX in QuantConnect might not match sources like Yahoo Finance or Thinkorswim (ToS), as well as suggested steps for an audit:
Daily Bar Construction:
Possible Small Discrepancies:
Aggregation and Filtering Process:
How to Audit Discrepancies:
Further Discussion:
In summary, slight mismatches result from how each data source: • Receives the official closing price. • Logs or delays post-market official auction data. • Consolidates (or does not consolidate) price feeds.
By comparing time-stamped data from each platform, you can see precisely how and when each source tags the “close” each day and confirm the differences.
Sources: Data Preparation - QuantConnect.com, US Equities - QuantConnect.com, Intraday Arbitrage Between Index ETFs, Pairs Trading With Stocks, Greeks and IV Implementation
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.
Scott Schoolmeyer
so how do you get the correct closing price of the spx daily bars? almost all the major data platform have the same number and they don't agree with what qc returns for the closing price.
Scott Schoolmeyer
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!