我正在尝试创建一个交易模型,我想在一段时间后执行交易。如何运行控制台,并在特定时间执行订单?请显示代码实现示例?
localtime = time.asctime(time.localtime(time.time()))
if localtime >"time":
#execute order
答案 0 :(得分:0)
最简单的实现是使用threading。打开一个线程,每秒钟左右根据您的情况检查一次时间,例如
while (localtime < time_to_execute):
time.sleep(1)
localtime = (refresh the local time value)
*some code to execute a trade*
链接的资源包含有关如何打开和连接线程的大量信息,因此您可以将其设置为按需要运行(连续,在交易后退出等)
答案 1 :(得分:0)
我知道了。不是最好的,但仍然可以工作。请告知是否有更好的方法。谢谢。
方法:Order_execution_function首先执行,因为它将保存运行条件订单的代码。但是要在指定的时间段运行代码,必须将执行条件(如果语句,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()