如何在特定时间范围内从股票报价数据中保存数据

问题描述 投票:1回答:1

我每秒都会收到以下格式的库存数据

'ohlc':{'close':75.95,'high':83.5,'low':64.6,'open':75.95},last_price':75.0,timestamp':datetime.datetime(2019,11,2, 11,20,15)

由于我的交易时间从上午9:30开始,所以我只想保存前五分钟的股票最高和最低价格,因此股票的最高和最低价格应该在9:30到9:35之间上午。以下是我正在使用的代码,但无法获得结果。请帮我解决这个问题。基本上,我需要保存5分钟的数据,但是我不知道该怎么做。

start_time = datetime.time(9, 30)
end_time = datetime.time(9, 35)
current_time = datetime.datetime.now().time()
candle_start_time = current_time >= start_time and current_time <= end_time

breakout_time_start = current_time >= start_time

while candle_start_time is True:
    print('time started')
while current_time > end_time:
    print('time extended')
while current_time < end_time:
    print('time extended 1')
python time timedelta stock
1个回答
0
投票

例如,我使用无限循环-while True-一直运行(每天24小时)。它应该从库存中获取日期,检查数据中的时间并将其保存在某个位置(文件或数据库)。

最终,它可以运行代码的注释部分以在9:35之后停止它,然后您必须在第二天9:30之前启动它。您可以手动启动它,也可以使用一些调度程序来启动它-即。 [Linux上的cronjob

import datetime

# --- functions ---

def get_from_stock():
    # TODO: get current data from stock
    return {
        'ohlc': {'close': 75.95, 'high': 83.5, 'low': 64.6, 'open':75.95},
        'last_price': 75.0,
        'timestamp': datetime.datetime(2019, 11, 2, 11, 20, 15),
    }

# --- main ---

start_time = datetime.time(9, 30)
end_time   = datetime.time(9, 35)

while True:

    data = get_from_stock()

    data_time = data['timestamp'].time()

    if start_time <= data_time <= end_time:
        print('Time:', data_time)
        print('High:', data['ohlc']['high'])
        print('Low:', data['ohlc']['low'])
        print('TODO: save it')

    #if data_time > end_time:
    #    print('Time:', data_time)
    #    print('It is end for today. Run again tomorrow before 9:00 AM.')
    #    break
© www.soinside.com 2019 - 2024. All rights reserved.