# Historical tick data

**URL:** <https://forum.alpaca.markets/t/historical-tick-data/17275>\
**Category:** Alpaca Market Data\
**Created:** [July 5, 2025, 7:01am UTC](https://forum.alpaca.markets/t/historical-tick-data/17275 "2025-07-05T07:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tokusuke\_Utsugi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/tokusuke_utsugi/32/5505_2.png) [@Tokusuke\_Utsugi](https://forum.alpaca.markets/u/Tokusuke_Utsugi)\
**Post date:** [July 5, 2025, 7:01am UTC](https://forum.alpaca.markets/t/historical-tick-data/17275/1 "2025-07-05T07:01:53Z")

</div>

I’m trying to extract tick trade data, it begins 4:00:00 (pre market) but ends just before 16:00.  
I can’t understand why it includes after hours trades?  
Can anyone help me?  
My code is below.  
def collect\_and\_store\_data(symbol, target\_date, client, conn):  
ny\_tz = pytz.timezone(‘America/New\_York’)  
start\_of\_day\_naive = datetime(target\_date.year, target\_date.month, target\_date.day)  
base\_dt = ny\_tz.localize(start\_of\_day\_naive)

```
total_inserted_count = 0
cursor = conn.cursor()
try:
    for i in range(96):
        start_dt_aware = base_dt + timedelta(minutes=i * 15)
        end_dt_aware = base_dt + timedelta(minutes=(i + 1) * 15) - timedelta(microseconds=1)
        request_params = StockTradesRequest(
            symbol_or_symbols=symbol,
            start=start_dt_aware,
            end=end_dt_aware
        )
        trades_response = client.get_stock_trades(request_params)

        if symbol in trades_response.data and trades_response.data[symbol]:
            trade_list = trades_response.data[symbol]
            trade_data_to_insert = [
                (t.timestamp, t.symbol, t.price, t.size, t.exchange, t.conditions, t.tape)
                for t in trade_list
            ]
            
            sql = """
            INSERT INTO trades (time, symbol, price, size, exchange, conditions, tape)
            VALUES %s ON CONFLICT DO NOTHING;
            """
            execute_values(cursor, sql, trade_data_to_insert)
            
            inserted_count = len(trade_data_to_insert)
            total_inserted_count += inserted_count
            print(f" {inserted_count}trades counted")
        else:
            print(f" no data ")
    conn.commit()
    print(f" :+1: 合計 {total_inserted_count} trades commited")

except Exception as e:
    print(f"\n :x: {symbol} error occurred roll back: {e}", file=sys.stderr)
    conn.rollback()
finally:
    cursor.close()

```

---

<div class="post-metadata">

**Author:** ![Gergely\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/gergely_alpaca/32/3210_2.png) [@Gergely\_Alpaca](https://forum.alpaca.markets/u/Gergely_Alpaca)\
**Post date:** [July 7, 2025, 7:24am UTC](https://forum.alpaca.markets/t/historical-tick-data/17275/2 "2025-07-07T07:24:43Z")

</div>

> [@Tokusuke\_Utsugi](#):
>
> I’m trying to extract tick trade data, it begins 4:00:00 (pre market) but ends just before 16:00.  
> I can’t understand why it includes after hours trades?  
> Can anyone help me?

Your script starts at New York midnight and it also ends at midnight (96 \* 15 minutes = 1440 minutes = 24 hours), so naturally it includes after hours trades.

The easiest way to fix this is to add these two lines after the the `start_dt_aware` line:

```py
if start_dt_aware.hour >= 16:
    break

```

---

<div class="post-metadata">

**Author:** ![Tokusuke\_Utsugi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/tokusuke_utsugi/32/5505_2.png) [@Tokusuke\_Utsugi](https://forum.alpaca.markets/u/Tokusuke_Utsugi)\
**Post date:** [July 28, 2025, 11:48pm UTC](https://forum.alpaca.markets/t/historical-tick-data/17275/3 "2025-07-28T23:48:53Z")

</div>

Thank you for your help, I managed to extract data from pre-market to after hours.
