如何在 python 交易模型中创建功能时间条件

How do I create functioning time conditional in python trading model?

本文关键字:创建 功能 时间 条件 模型 python 交易      更新时间:2023-10-16

我正在尝试创建一个交易模型,我想在一定时间后执行交易。如何运行控制台,并让订单在特定时间执行?请展示代码实现的示例?

localtime = time.asctime(time.localtime(time.time())) if localtime >"time": #execute order

最简单的实现是使用线程。打开一个线程,每隔一秒左右根据您的条件检查时间,例如

while (localtime < time_to_execute):
    time.sleep(1)
    localtime = (refresh the local time value)
*some code to execute a trade*

链接的资源包含有关如何打开和加入线程的大量信息,因此您可以将其设置为随心所欲地运行(连续、交易后退出等(

我想通了。不是最好的,但仍然有效。请告知是否有更好的方法。谢谢。

方法:Order_execution_function先行,因为它将保存运行条件订单的代码。然而,要在指定的时间段运行代码,执行条件(if 语句,price>200...(必须包装在Time_Conditional函数中,以便仅在满足Time_Conditional函数时才读取执行代码,然后允许Order_execution_function执行。

from datetime import datetime
from threading import Timer
    def Order_execution_function(self):
         x = datetime.today()
         y = x.replace(year=2019, month=7, day=26, hour=15, minute=36, second=15, microsecond=0)
         delta_t = y - x
         secs = delta_t.seconds + 1
              def Time_Conditional(): 
                   #execute code, if statements...
                   #execute code, if statements...
                   #execute code, if statements...
         t = Timer(secs, Time_Conditional)
         t.start()