exchange.SetDirection


exchange.SetDirection(direction)
exchange.SetDirection(direction, type)

```direction```参数用于设置期货合约下单时的方向,可选值为:```"buy"```、```"closesell"```、```"sell"```、```"closebuy"```、```closesell_today```、```closebuy_today```。 其中```closebuy_today```,```closesell_today```指平今仓。```closebuy```/```closesell```为平昨仓。
direction
true
string
对于CTP协议传统期货,可以设置第二个参数```type```为```"1"```或者```"2"```或者```"3"```,分别指"投机","套利","套保",不设置默认为投机。
type
false
string

```javascript
function main(){
    // 鉴于测试代码,不使用商品期货策略一般架构,这里仅仅判断exchange.IO("status")函数,判断连接期货公司前置机成功后立即执行测试代码。股票证券无需使用exchange.IO("status")判断连接状态
    while(!exchange.IO("status")) {
        Sleep(1000)
    }

    // 举例设置为螺纹钢主力合约
    exchange.SetContractType("rb888")    
    // 设置下单类型为做多
    exchange.SetDirection("buy")
    // 以10000的价格,合约数量为2张下单。注意,这里的下单价格为举例,具体测试的时候可以自行设置、改动
    exchange.Buy(10000, 2)
    // 设置下单类型为平多
    exchange.SetDirection("closebuy")
    exchange.Sell(1000, 2)
}
def main():
    while not exchange.IO("status"):
        Sleep(1000)

    exchange.SetContractType("rb888")
    exchange.SetDirection("buy")
    exchange.Buy(10000, 2)
    exchange.SetDirection("closebuy")
    exchange.Sell(1000, 2)
void main() {
    while(exchange.IO("status") == 0) {
        Sleep(1000);
    }

    exchange.SetContractType("rb888");
    exchange.SetDirection("buy");
    exchange.Buy(10000, 2);
    exchange.SetDirection("closebuy");
    exchange.Sell(1000, 2);
}

exchange.SetDirection()函数设置期货合约交易的方向和下单函数之间的对应关系:

下单函数 SetDirection函数的参数设置的方向 备注
exchange.Buy “buy” 买入开多仓
exchange.Buy “closesell” 买入平空仓
exchange.Buy “closesell_today” 买入平空仓(今仓)
exchange.Sell “sell” 卖出开空仓
exchange.Sell “closebuy” 卖出平多仓
exchange.Sell “closebuy_today” 卖出平多仓(今仓)

{@fun/Trade/exchange.Buy exchange.Buy}, {@fun/Trade/exchange.Sell exchange.Sell}