Lean icon indicating copy to clipboard operation
Lean copied to clipboard

Daily Closing Price & Indicator Descrepancy When Changing Resolution

Open DerekMelchin opened this issue 4 years ago • 5 comments

Expected Behavior

  1. The daily price of a forex symbol should be the same, regardless of the resolution of data we select.
  2. Indicator values should be the same at the end of each day when we initialize indicators with a daily resolution but change the symbol resolution.

Actual Behavior

  1. In this backtest, the daily closing price of a forex pair differs when changing the symbol resolution.
  2. The logs listed below show the following findings of the backtest:
    • When selecting equities in the backtest:
      • The ATR values are equal during the first 2 days for all the resolutions, but then they start to diverge.
      • The EMA values are equal during the first 3 days for all the resolutions, but then the minute resolution's values start to diverge while the hourly and daily resolutions remain equal.
    • When selecting forex in the backtest:
      • The ATR values are different at each time step across all the resolutions.
      • The EMA values for the minute and hour resolutions are equal. Only the first EMA value on daily resolution is equal to the other resolutions.

Logs: fx-daily.txt fx-hour.txt fx-minute.txt equities-daily.txt equities-minute.txt equities-hour.txt

Potential Solution

Unknown. The _previousInput property of indicators is private, so I cannot see which values the indicators are being updated with.

Reproducing the Problem

Utilize this backtest.

System Information

Run in the browser in the Algorithm Lab. Windows 10.

Checklist

  • [x] I have completely filled out this template
  • [x] I have confirmed that this issue exists on the current master branch
  • [x] I have confirmed that this is not a duplicate issue by searching issues
  • [x] I have provided detailed steps to reproduce the issue

DerekMelchin avatar Jul 15 '20 18:07 DerekMelchin

@DerekMelchin Please bisect if it's the indicators or the consolidators by creating daily consolidators for the same period.

If consolidators show the same issue compare the daily bars created by the consolidators over the 2 days vs the underlying daily bars and we should be able to see where it's coming from.

If minute-consolidated bars are different look at the minute data to see where the difference comes from. e.g. Could be different high-low or if identical then could be the consolidators not using all the data.

jaredbroad avatar Feb 17 '23 11:02 jaredbroad

rs show the same issue compare the daily bars created by the consolidators over the 2 days vs the underlying daily bars and we should be able to see where it's coming from.

@Marinovsky let's take a look at this one

Martin-Molinero avatar Feb 06 '24 17:02 Martin-Molinero

Regarding this:

In this backtest, the daily closing price of a forex pair differs when changing the symbol resolution.

This is due to the timezone of the data. There is a shift of 3 hours between the data at daily and hour resolution of the forex symbol:

image

Still, this bug had already been pointed out (see https://github.com/QuantConnect/Lean/issues/6964)

Marinovsky avatar Feb 08 '24 17:02 Marinovsky

Regarding this:

When selecting forex in the backtest:

  • The ATR values are different at each time step across all the resolutions.

After consolidating Hour and Minute data into daily consolidator I found that the bars generated differed for 2020/7/3. It turned out that the last two datapoints of the EURGBP minute data for 2020/7/3 were not taken by the consolidator since it's time was beyond the trading close time for forex securities in Oanda Market:

image

image

Logs from the algo: 2020-07-01 00:00:00 ConsolidatorOnData(): Data: EURGBP: Bid: O: 0.90745 Bid: H: 0.90754 Bid: L: 0.90744 Bid: C: 0.90754 Ask: O: 0.90766 Ask: H: 0.90774 Ask: L: 0.90763 Ask: C: 0.90774

However, for Hour resolution those two datapoints were taken:

image image

Thus @Martin-Molinero proposed that we need to reprocess our hour/daily data since we probably updated the MHDB and didn't reprocess it.

Marinovsky avatar Feb 09 '24 21:02 Marinovsky

Regarding this:

When selecting equities in the backtest:

  • The ATR values are equal during the first 2 days for all the resolutions, but then they start to diverge.
  • The EMA values are equal during the first 3 days for all the resolutions, but then the minute resolution's values start to diverge while the hourly and daily resolutions remain equal.

After running the algo attached in this issue on local and QC Cloud I found the logs don't differ. I also consolidated minute/hour data into daily but the logs were still the same for all resolutions.

Logs when the symbol was using Minute resolution:

2020-04-21 00:00:00 Launching analysis for 400b1ff1b6ab7c2f2c19feb81ca31fb1 with LEAN Engine v2.5.0.0.16236
2020-04-21 00:00:00 Algorithm starting warm up...
2020-07-01 00:00:00 ATR: 6.201240964607231  EMA: 281.65593761324
2020-07-01 00:01:00 Algorithm finished warming up.
2020-07-02 09:31:00 ATR: 5.997500765063857  EMA: 282.08269552064235
2020-07-06 09:31:00 ATR: 5.920123412273582  EMA: 282.5730775066563
2020-07-07 09:31:00 ATR: 5.866569191039754  EMA: 283.1078469087875
2020-07-08 09:31:00 ATR: 5.728747563465486  EMA: 283.8009661058939
2020-07-09 09:31:00 ATR: 5.563500346075094  EMA: 284.3452485270745
2020-07-10 09:31:00 ATR: 5.601149913784016  EMA: 284.95747507605194
2020-07-10 17:00:00 Algorithm Id:(400b1ff1b6ab7c2f2c19feb81ca31fb1) completed in 0.67 seconds at 25k data points per second. Processing total of 16,947 data points.

Logs when the symbol resolution was Hour

2020-04-21 00:00:00 Launching analysis for 0938ff4553fd0b11f55905a0bec02306 with LEAN Engine v2.5.0.0.16236
2020-04-21 00:00:00 Algorithm starting warm up...
2020-07-01 00:00:00 ATR: 6.201240964607231  EMA: 281.65593761324
2020-07-01 01:00:00 Algorithm finished warming up.
2020-07-02 10:00:00 ATR: 5.997500765063857  EMA: 282.08269552064235
2020-07-06 10:00:00 ATR: 5.920123412273582  EMA: 282.5730775066563
2020-07-07 10:00:00 ATR: 5.866569191039754  EMA: 283.1078469087875
2020-07-08 10:00:00 ATR: 5.728747563465486  EMA: 283.8009661058939
2020-07-09 10:00:00 ATR: 5.563500346075094  EMA: 284.3452485270745
2020-07-10 10:00:00 ATR: 5.601149913784016  EMA: 284.95747507605194
2020-07-10 17:00:00 Algorithm Id:(0938ff4553fd0b11f55905a0bec02306) completed in 0.42 seconds at 2k data points per second. Processing total of 658 data points.

Logs for consolidating minute data into daily:

2020-04-21 00:00:00 Launching analysis for BasicTemplateFrameworkAlgorithm with LEAN Engine v2.5.0.0
2020-04-21 00:00:00 Algorithm starting warm up...
2020-07-01 00:00:00 ConsolidatorOnData(): Data: SPY: O: 288.4115 H: 294.2839 L: 288.2407 C: 292.5383 V: 92711959
2020-07-01 00:01:00 Algorithm finished warming up.
2020-07-02 09:31:00 ConsolidatorOnData(): Data: SPY: O: 293.6862 H: 295.8871 L: 293.2118 C: 294.5874 V: 65705932
2020-07-06 09:31:00 ConsolidatorOnData(): Data: SPY: O: 298.1166 H: 299.5017 L: 295.5266 C: 296.2097 V: 63072936
2020-07-07 09:31:00 ConsolidatorOnData(): Data: SPY: O: 300.1468 H: 301.3801 L: 299.3688 C: 300.7824 V: 55271220
2020-07-08 09:31:00 ConsolidatorOnData(): Data: SPY: O: 299.1981 H: 301.2283 L: 297.2912 C: 297.6802 V: 76100561
2020-07-09 09:31:00 ConsolidatorOnData(): Data: SPY: O: 298.4486 H: 300.0709 L: 296.6556 C: 299.957 V: 51909690
2020-07-10 09:31:00 ConsolidatorOnData(): Data: SPY: O: 300.5926 H: 300.8298 L: 294.7392 C: 298.2494 V: 79073715
2020-07-10 16:58:00 Algorithm Id:(BasicTemplateFrameworkAlgorithm) completed in 4.32 seconds at 4k data points per second. Processing total of 16,637 data points.

Logs when consolidating hour data into daily:

2020-04-21 00:00:00 Launching analysis for BasicTemplateFrameworkAlgorithm with LEAN Engine v2.5.0.0
2020-04-21 00:00:00 Algorithm starting warm up...
2020-07-01 00:00:00 ConsolidatorOnData(): Data: SPY: O: 288.4115 H: 294.2839 L: 288.2407 C: 292.5383 V: 92711959
2020-07-01 10:00:00 Algorithm finished warming up.
2020-07-02 10:00:00 ConsolidatorOnData(): Data: SPY: O: 293.6862 H: 295.8871 L: 293.2118 C: 294.5874 V: 65705935
2020-07-06 10:00:00 ConsolidatorOnData(): Data: SPY: O: 298.1166 H: 299.5017 L: 295.5266 C: 296.2097 V: 63072943
2020-07-07 10:00:00 ConsolidatorOnData(): Data: SPY: O: 300.1468 H: 301.3801 L: 299.3688 C: 300.7824 V: 55271224
2020-07-08 10:00:00 ConsolidatorOnData(): Data: SPY: O: 299.1981 H: 301.2283 L: 297.2912 C: 297.6802 V: 76100565
2020-07-09 10:00:00 ConsolidatorOnData(): Data: SPY: O: 298.4486 H: 300.0709 L: 296.6556 C: 299.957 V: 51909692
2020-07-10 10:00:00 ConsolidatorOnData(): Data: SPY: O: 300.5926 H: 300.8298 L: 294.7392 C: 298.2494 V: 79073719
2020-07-10 16:00:00 Algorithm Id:(BasicTemplateFrameworkAlgorithm) completed in 3.62 seconds at 0k data points per second. Processing total of 102 data points.

Marinovsky avatar Feb 12 '24 14:02 Marinovsky