我每秒钟都会收到以下格式的股票数据
'ohlc':{‘关闭’:75.95,‘高’:83.5,‘低’:64.6,‘开放’:75.95},最后价格: 75.0,时间戳‘: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')发布于 2019-11-03 21:07:00
例如,我使用无穷大循环- 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.')
# breakhttps://stackoverflow.com/questions/58673204
复制相似问题