简体   繁体   English

如何使用Interactive Brokers API将订单限制在特定时间内打开并在另一特定时间内关闭

[英]How to restrict order open to a certain time and close it in another certain time with Interactive Brokers API

Using Interactive Brokers API, I would like to restrict order open to a certain time for example not before 09:35, I would also like to close the position at about 5 minutes before the end of the day.I tried to use an if statment with Sys.time() but I didn't work and in addition it is not elegant..How can I fix the error or use another method to full fill my need ? 我想使用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"))

    }

Every broker has a facility to specify the good after time (GAT). 每个经纪人都有一种手段来指定善后(GAT)。 It should be as simple as setting this field in the R interface. 它应该与在R接口中设置此字段一样简单。 In the CRAN IBrokers docs it says the field 在CRAN IBrokers文档中,该字段为

goodAfterTime Trades Good After Time: YYYYMMDD hh:mm:ss or "" goodAfterTime交易良好的时间:YYYYMMDD hh:mm:ss或“”

Here's the order properties API info at IB https://www.interactivebrokers.com/en/software/api/apiguide/java/order.htm 这是IB https://www.interactivebrokers.com/cn/software/api/apiguide/java/order.htm上的订单属性API信息

The trade's "Good After Time," format "YYYYMMDD hh:mm:ss (optional time zone)" 交易的“ Good After Time”,格式为“ YYYYMMDD hh:mm:ss(可选时区)”

Note, MKT orders will be filled practically instantly. 请注意,MKT订单几乎可以立即执行。 A LMT order may take some time. LMT订单可能需要一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM