gpt4 book ai didi

python - 如何保存特定时间范围的股票报价数据中的数据

转载 作者:太空宇宙 更新时间:2023-11-03 20:11:47 25 4
gpt4 key购买 nike

我每秒都会收到以下格式的股票数据

'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:30 之间:上午 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')

最佳答案

在示例中,我使用无限循环 - 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

关于python - 如何保存特定时间范围的股票报价数据中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58673204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com