Hi,

According to documentation, History(symbol, periods, resolution), and all variations based on “periods”, should return a number of bars specified as “periods”. Here is a doc string:

“Gets the historical data for the specified symbol. The exact number of bars will be returned.”

But this is not the case. The code rather computes a start time, starting from a targeted end time, and rewinding time the specified amount of bars. So for instance, if you request 1000 second bars, it will go back 1000 seconds from the current time. But that does not guarantee you will get 1000 second bars. It will only be the case if the asset traded at least once every second over that 1000 seconds period. In other words, it will only be the case if there is a second bar for each single second over the 1000 seconds period, which can easily not be the case.

The code would rather need to simply request 1000 second bars from the given end time (going in the past) in the DB.

Here is an example showing the problem. The example will only work as long as the DB stays as-is. Right now, SPY data ends on June 13th in the DB but we are June 15th. If the DB gets updated, the example can execute differently and return a different number of bars.
 

class HyperActiveOrangePigeon(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 6, 14)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        symbol = self.AddEquity("SPY", Resolution.Second, fillDataForward=False, extendedMarketHours=True).Symbol
        h = self.History(symbol, periods=1_000, resolution=Resolution.Second)
        self.Debug(h)
        self.Debug(len(h))

    def OnData(self, data: Slice):
        pass

And here is the log:

2022-06-14 00:00:00 :	askclose askhigh asklow askopen asksize bidclose bidhigh bidlow bidopen bidsize close high low open volume symbol time SPY R735QTJ8XC9X 2022-06-13 19:43:22 375.40 375.40 375.40 375.40 200.0 375.36 375.36 375.36 375.36 100.0 375.360 375.360 375.36 375.36 25.0 2022-06-13 19:43:34 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.380 375.380 375.38 375.38 138.0 2022-06-13 19:43:41 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.360 375.360 375.36 375.36 70.0 2022-06-13 19:43:57 375.40 375.40 375.40 375.40 200.0 375.32 375.36 375.32 375.36 500.0 375.360 375.360 375.36 375.36 100.0 2022-06-13 19:43:59 375.42 375.42 375.40 375.40 500.0 375.30 375.40 375.30 375.32 500.0 375.400 375.400 375.38 375.38 5000.0 2022-06-13 19:44:08 375.39 375.40 375.39 375.39 500.0 375.29 375.29 375.29 375.29 500.0 375.400 375.400 375.40 375.40 14.0 2022-06-13 19:44:22 375.41 375.41 375.41 375.41 500.0 375.31 375.31 375.31 375.31 300.0 375.310 375.360 375.31 375.36 100.0 2022-06-13 19:44:34 375.39 375.41 375.38 375.41 1000.0 375.31 375.31 375.31 375.31 300.0 375.360 375.360 375.36 375.36 13.0 2022-06-13 19:44:59 375.41 375.41 375.41 375.41 500.0 375.31 375.32 375.31 375.32 1300.0 375.400 375.400 375.40 375.40 5.0 2022-06-13 19:45:00 375.39 375.41 375.39 375.41 500.0 375.31 375.32 375.31 375.31 1300.0 375.390 375.390 375.39 375.39 221.0 2022-06-13 19:45:31 375.46 375.46 375.46 375.46 500.0 375.37 375.37 375.35 375.37 500.0 375.370 375.400 375.37 375.40 200.0 2022-06-13 19:45:34 375.46 375.47 375.46 375.46 500.0 375.37 375.46 375.37 375.37 500.0 375.460 375.460 375.46 375.46 300.0 2022-06-13 19:45:39 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.370 375.410 375.37 375.41 42.0 2022-06-13 19:45:40 375.40 375.46 375.40 375.46 100.0 375.36 375.36 375.36 375.36 500.0 375.370 375.370 375.37 375.37 3.0 2022-06-13 19:45:42 375.44 375.44 375.40 375.40 1000.0 375.41 375.41 375.35 375.35 300.0 375.400 375.400 375.40 375.40 101.0 2022-06-13 19:45:45 375.42 375.44 375.42 375.44 500.0 375.35 375.41 375.35 375.41 500.0 375.410 375.410 375.41 375.41 301.0 2022-06-13 19:45:59 375.45 375.45 375.40 375.40 500.0 375.35 375.35 375.35 375.35 500.0 375.400 375.400 375.40 375.40 1.0 2022-06-13 19:46:08 375.49 375.49 375.47 375.47 1000.0 375.40 375.40 375.35 375.35 500.0 375.400 375.400 375.40 375.40 401.0 2022-06-13 19:46:25 375.52 375.52 375.52 375.52 1000.0 375.42 375.42 375.42 375.42 500.0 375.420 375.420 375.42 375.42 102.0 2022-06-13 19:46:28 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.450 375.450 375.45 375.45 36.0 2022-06-13 19:46:39 375.52 375.52 375.51 375.52 1000.0 375.50 375.50 375.50 375.50 200.0 375.500 375.500 375.50 375.50 9.0 2022-06-13 19:46:44 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.500 375.500 375.50 375.50 25.0 2022-06-13 19:46:45 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.520 375.520 375.52 375.52 133.0 2022-06-13 19:46:46 375.53 375.53 375.52 375.52 1000.0 375.50 375.50 375.50 375.50 200.0 375.520 375.520 375.52 375.52 500.0 2022-06-13 19:46:47 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.520 375.520 375.52 375.52 2.0 2022-06-13 19:46:48 375.53 375.53 375.53 375.53 1000.0 375.42 375.50 375.42 375.50 500.0 375.500 375.500 375.50 375.50 267.0 2022-06-13 19:46:55 375.53 375.53 375.53 375.53 1000.0 375.42 375.42 375.42 375.42 500.0 375.530 375.530 375.53 375.53 204.0 2022-06-13 19:47:24 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.590 375.590 375.55 375.55 5.0 2022-06-13 19:47:42 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.520 375.520 375.52 375.52 2.0 2022-06-13 19:47:57 375.62 375.62 375.60 375.60 500.0 375.52 375.53 375.50 375.50 500.0 375.570 375.570 375.57 375.57 517.0 2022-06-13 19:48:01 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.570 375.570 375.57 375.57 7.0 2022-06-13 19:48:07 375.61 375.62 375.59 375.62 500.0 375.52 375.53 375.52 375.52 500.0 375.550 375.550 375.55 375.55 48.0 2022-06-13 19:48:09 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.600 375.600 375.60 375.60 4.0 2022-06-13 19:48:18 375.59 375.59 375.59 375.59 500.0 375.50 375.50 375.50 375.50 1000.0 375.570 375.570 375.57 375.57 3.0 2022-06-13 19:48:22 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.590 375.590 375.59 375.59 40.0 2022-06-13 19:48:32 375.59 375.59 375.59 375.59 1000.0 375.50 375.52 375.50 375.52 500.0 375.550 375.550 375.55 375.55 7.0 2022-06-13 19:48:34 375.56 375.57 375.56 375.57 500.0 375.50 375.50 375.50 375.50 500.0 375.550 375.550 375.55 375.55 18.0 2022-06-13 19:48:36 375.57 375.57 375.56 375.56 500.0 375.50 375.50 375.50 375.50 500.0 375.520 375.520 375.52 375.52 25.0 2022-06-13 19:48:38 375.51 375.57 375.51 375.57 500.0 375.43 375.50 375.41 375.50 500.0 375.500 375.520 375.50 375.52 520.0 2022-06-13 19:48:49 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.480 375.480 375.48 375.48 30.0 2022-06-13 19:48:52 375.57 375.57 375.57 375.57 500.0 375.49 375.49 375.48 375.48 500.0 375.500 375.500 375.50 375.50 40.0 2022-06-13 19:48:54 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.510 375.510 375.51 375.51 2.0 2022-06-13 19:48:58 375.59 375.59 375.57 375.57 500.0 375.50 375.51 375.49 375.49 1000.0 375.510 375.510 375.51 375.51 73.0 2022-06-13 19:49:03 375.57 375.59 375.56 375.59 500.0 375.50 375.50 375.47 375.50 500.0 375.510 375.510 375.51 375.51 50.0 2022-06-13 19:49:07 375.59 375.59 375.59 375.59 500.0 375.50 375.50 375.50 375.50 500.0 375.500 375.500 375.50 375.50 25.0 2022-06-13 19:49:09 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 375.510 375.510 375.51 375.51 26.0 2022-06-13 19:49:10 NaN NaN
2022-06-14 00:00:00 :	151

Thanks for reading,

Fred