【问题标题】:Summing Results from a function running every 30 seconds对每 30 秒运行一次的函数的结果求和
【发布时间】:2021-06-08 16:22:27
【问题描述】:

我制定了一个简单的交易策略,每 30 秒执行一次。我在函数中定义了策略:

def strategy():
    # strategy
    time.sleep(30)
    print(f'The Result from the Trade is: {result}')

然后我通过一个while循环运行它:

while True:
    strategy()

这绝对不是最好的实现,但它只适用于测试策略。它向我展示了每笔交易的结果。但是,我想让它运行一整天,并将每笔交易的结果累积并存储在一个变量中,这样我就有了一整天所有交易的最终结果。

我一直在研究如何实现这一点,但我找不到任何适合我现有结构的东西。有人可以帮忙吗?

【问题讨论】:

    标签: python python-3.x while-loop algorithmic-trading


    【解决方案1】:

    如果您想总结返回的值,您可以执行以下操作

    strategy_output = 0
    while True:
         strategy_output =+ strategy()
    

    如果您想将所有值保存到最后一个循环,您可以将值保存在与执行时间对应的字典中

    from datetime import datetime
    
    strategy_output_dict = {}
    while True:
         now = datetime.now()
         current_time = now.strftime("%H:%M:%S")
         strategy_output = strategy()
         strategy_output_dict[current_time] = strategy_output
    

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 2014-03-05
      • 2014-10-28
      • 2011-11-06
      • 2015-01-18
      • 1970-01-01
      • 2012-01-09
      • 1970-01-01
      相关资源
      最近更新 更多