跳至内容

OrderSend

OrderSend

这是用于打开市场订单或放置挂单的主要函数。

int  OrderSend(
   string   symbol,              // symbol
   int      cmd,                 // operation
   double   volume,              // volume
   double   price,               // price
   int      slippage,            // slippage
   double   stoploss,            // stop loss
   double   takeprofit,          // take profit
   string   comment=NULL,        // comment
   int      magic=0,             // magic number
   datetime expiration=0,        // pending order expiration
   color    arrow_color=clrNONE  // color
   );

参数

symbol

[in] 交易代码。

cmd

[in] 操作类型。可以是交易操作枚举中的任何类型。

volume

[in] 订单数量。

price

[in] 订单价格。

slippage

[in] 买入或卖出订单的最大价格偏差。

stoploss

[in] 止损水平。

takeprofit

[in] 获利止损水平。

comment=NULL

[in] 订单评论文本。评论的最后部分可能由服务器更改。

magic=0

[in] 订单魔法数字。可以用作用户定义的标识符。

expiration=0

[in] 订单到期时间(仅适用于挂单)。

arrow_color=clrNONE

[in] 图表上开市箭头的颜色。如果参数缺失或值为CLR_NONE,则图表上不会绘制开市箭头。

返回值

如果交易服务器成功分配了订单号,则返回该号码;如果失败,则返回-1。要获取额外的错误信息,需要调用GetLastError()函数。

注意

在打开市价订单(OP_SELL或OP_BUY)时,只能使用最新买入(卖出)或卖出(买入)价格作为开市价格。如果操作涉及的证券与当前持有的证券不同,必须使用MarketInfo()函数并指定MODE_BID或MODE_ASK参数以获取该证券的最新报价。

无法应用计算出的或非标准化的价格。如果价格线程中没有请求的开市价格,或者价格没有根据小数点后的位数进行标准化,将生成错误129(ERR_INVALID_PRICE)。如果请求的开市价格完全过时,将生成错误138(ERR_REQUOTE),这取决于slippage参数。如果请求的价格过时但存在于价格线程中,订单将以当前价格打开,且仅当当前价格在价格±slippage范围内时。

止损和获利止损水平不能太接近市场。可以使用MarketInfo()函数并指定MODE_STOPLEVEL参数来获取止损的最小距离(以点数为单位)。如果止损水平错误或非标准化,将生成错误130(ERR_INVALID_STOPS)。MODE_STOPLEVEL的零值意味着对止损/获利止损的最小距离没有任何限制,或者交易服务器使用了某些外部机制进行动态水平控制,这些机制无法在客户端终端中体现。在这种情况下,GetLastError()可能会返回错误130,因为MODE_STOPLEVEL在这里实际上是“浮动”的。

在放置挂单时,开市价格不能太接近市场。可以使用MarketInfo()函数并指定MODE_STOPLEVEL参数来获取挂单价格与当前市场价格的最小距离(以点数为单位)。如果挂单的开市价格错误,将生成错误130(ERR_INVALID_STOPS)。

在某些交易服务器中,可以禁用挂单到期时间。在这种情况下,如果到期参数指定了非零值,将生成错误147(ERR_TRADE_EXPIRATION_DENIED)。

在某些交易服务器中,可以限制开放和挂单的总数量。如果超过了这个限制,将不会打开新的订单(或不会放置挂单),交易服务器将返回错误148(ERR_TRADE_TOO_MANY_ORDERS)。

示例:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- get minimum stop level
   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
   Print("Minimum Stop Level=",minstoplevel," points");
   double price=Ask;
//--- calculated SL and TP prices must be normalized
   double stoploss=NormalizeDouble(Bid-minstoplevel*Point,Digits);
   double takeprofit=NormalizeDouble(Bid+minstoplevel*Point,Digits);
//--- place market order to buy 1 lot
   int ticket=OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen);
   if(ticket<0)
     {
      Print("OrderSend failed with error #",GetLastError());
     }
   else
      Print("OrderSend placed successfully");
//---
  }
最后更新于