使用 Interactive Brokers API,我想限制在某个时间打开订单,例如不早于 09:35,我还想在一天结束前大约 5 分钟关闭头寸。我尝试使用 if 语句使用 Sys.time() 但我没有工作,此外它也不优雅..如何修复错误或使用其他方法来完全满足我的需求?
Hour<-as.integer(format(Sys.time(), "%H"))
Minute<-as.integer(format(Sys.time(), "%M"))
print(lastValue)
library(IBrokers)
options("scipen"=4)
myconid = 3
twsobj = twsConnect(myconid)
Sys.sleep(2)
myorderid = as.integer(reqIds(twsobj))
print(myorderid)
Sys.sleep(2)
if(lastValue>0.5 && Hour > 16 && Minute > 35 ){
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid ,"BUY",1,"MKT"))
print("IT WAS A BUY ORDER")
Sys.sleep(10)
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid + 1 ,"SELL",1,"MKT"))
} else{
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid , "SELL" , 1 , "MKT"))
print("IT WAS A SELL ORDER")
Sys.sleep(10)
placeOrder(twsobj,Contract=twsSTK("SPY"),Order=twsOrder(myorderid + 1 , "BUY" ,1, "MKT"))
}