The exchange.SetDirection()
function is used to set the order direction of the {@fun/Trade/exchange.Buy exchange.Buy} function, {@fun/Trade/exchange.Sell exchange.Sell} function when placing orders for futures contracts.
exchange.SetDirection(direction)
The direction
parameter is used to set the direction of the futures contract when the order is placed. The optional values are: "buy"
, "closesell"
, "sell"
, "closebuy"
.
direction
true
string
function main(){
// For example, set to OKX futures contract of this week
exchange.SetContractType("this_week")
// Set leverage to 5 times
exchange.SetMarginLevel(5)
// Set the order type to long
exchange.SetDirection("buy")
// Place an order for 2 contracts at 10,000
exchange.Buy(10000, 2)
exchange.SetMarginLevel(5)
exchange.SetDirection("closebuy")
exchange.Sell(1000, 2)
}
def main():
exchange.SetContractType("this_week")
exchange.SetMarginLevel(5)
exchange.SetDirection("buy")
exchange.Buy(10000, 2)
exchange.SetMarginLevel(5)
exchange.SetDirection("closebuy")
exchange.Sell(1000, 2)
void main() {
exchange.SetContractType("this_week");
exchange.SetMarginLevel(5);
exchange.SetDirection("buy");
exchange.Buy(10000, 2);
exchange.SetMarginLevel(5);
exchange.SetDirection("closebuy");
exchange.Sell(1000, 2);
}
The exchange.SetDirection()
function sets the correspondence between the direction of the futures contract transaction and the order placement function:
Order placement functions | The direction set by the parameters of the SetDirection function | Remarks |
---|---|---|
exchange.Buy | “buy” | Buy and open long positions |
exchange.Buy | “closesell” | Buy and close short positions |
exchange.Sell | “sell” | Sell and open short positions |
exchange.Sell | “closebuy” | Sell and close long positions |
{@fun/Trade/exchange.Buy exchange.Buy}, {@fun/Trade/exchange.Sell exchange.Sell}
exchange.SetMarginLevel exchange.SetContractType