【问题标题】:How to save data from stock tick data for particular timeframe如何从特定时间范围内的股票报价数据中保存数据
【发布时间】:2019-11-02 16:49:34
【问题描述】:

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

'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')

【问题讨论】:

  • 嘿@tanjiro,在您的问题中您提到您需要在特定时间范围内存储一些值。在代码中,您只提供了一些时间。“5 分钟的数据”如何发挥作用?
  • 我不明白你的代码。当您从服务器获取新数据时,您只需检查start_time &lt;= time_from_timestamp &lt;= end_time
  • 你的 while 循环没用。您总是在while 中检查相同的值。在while 内部,您必须从服务器获取新值并从 tis 数据中获取时间,并将其用于您在while 中使用的变量中
  • @stupidwolf 请把代码放在一边,我需要的是从上午 9:30 到上午 9:35 的传入数据,应该保存,即股票的高值和低值,以及数据的任何变化9:35 之后的高低不应该在保存的变量中考虑。
  • @furas 请把代码放在一边,我需要的是从上午 9:30 到上午 9:35 的传入数据,应该保存,即股票的高值和低值,以及数据的任何变化9:35 之后的高低不应该在保存的变量中考虑

标签: python time timedelta stock


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 2014-06-19
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多