如何在特定时间范围内保存股票报价数据中的数据
How to save data from stock tick data for particular timeframe
我每秒接收以下格式的股票数据
'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')
在示例中,我一直使用无限循环 - while True
- 到 运行(每天 24 小时)。它应该从库存中获取日期,检查数据中的时间并将其保存在某个地方(文件或数据库)。
最终它可以 运行 注释部分代码以在 9:35 之后停止它,然后你必须在第二天 9:30 之前启动它。您可以手动启动它或使用一些调度程序来启动它 - 即。 cronjob
在 Linux。
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
我每秒接收以下格式的股票数据
'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')
在示例中,我一直使用无限循环 - while True
- 到 运行(每天 24 小时)。它应该从库存中获取日期,检查数据中的时间并将其保存在某个地方(文件或数据库)。
最终它可以 运行 注释部分代码以在 9:35 之后停止它,然后你必须在第二天 9:30 之前启动它。您可以手动启动它或使用一些调度程序来启动它 - 即。 cronjob
在 Linux。
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