OrderCalcProfit

函数为当前账户计算利润,在当前的交易市场条件下,以参量为基础传递。函数使用交易操作的结构进行再评估,值是返回的当前账户的值。

bool  OrderCalcProfit(
   ENUM_ORDER_TYPE       action,           // 订单类型 (ORDER_TYPE_BUY 或者 ORDER_TYPE_SELL)
   string                symbol,           // 交易品种名称
   double                volume,           // 交易量
   double                price_open,       // 开盘价
   double                price_close,      // 收盘价
   double&               profit            // 为获得利润值的变量
   );

参量

action

[in]  订单类型,可以是 ENUM_ORDER_TYPE 项目里ORDER_TYPE_BUY 或 ORDER_TYPE_SELL值中的一个。

symbol

[in]  交易品种名称。

volume

[in]  交易操作成交量。

price_open

[in]  开盘价。

price_close

[in]  收盘价。

profit

[out] 计算利润的变量值在函数成功执行后编写进去,估计利润值依据许多因素,并在不同的市场环境中加以区别。

返回值

如果成功,函数返回true,否则返回false。如果指定无效订单类型,函数返回false,为了获得错误信息,可以调用 GetLastError()

示例:

 
#define   VOLUME     1.0   // 订单交易量
#define   DEVIATION  100   // 平仓价格之前的点数
 
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
   string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT);   // 利润货币
   double close_price=ChartPriceOnDropped(); // 价格坐标, 对应着使用鼠标拖曳脚本的点位, 用于平仓价格
 
//---
   for(int i=0i<=ORDER_TYPE_SELLi++)
     {
      ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i;                 // 订单类型: 0 - 买, 1 - 卖
      double open_price=PriceOpenByOrderType(_Symbolorder_type);   // 下单价格: 对于买 - Ask, 对于卖 - Bid
     
      //--- 如果未获得开单价格, 继续循环
      if(open_price==0)
         continue;
      
      //--- 如果平仓价格为0 (脚本不是通过拖曳到图表上而运行的), 计算价格
      if(close_price==0)
         close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
      
      //--- 根据传入的参数计算当前账户和市场环境下的利润大小
      double profit=0;
      ResetLastError();
      if(!OrderCalcProfit(order_type_SymbolVOLUMEopen_priceclose_priceprofit))
        {
         Print("OrderCalcProfit() failed. Error "GetLastError());
         continue;
        }
      
      //--- 在日志中打印收到的利润值
      PrintFormat("Estimated profit for %.2f %s position on %s with opening price of %.*f and closing price of %.*f: %.2f %s",
                  VOLUMEOrderTypeDescription(order_type), _Symbol_Digitsopen_price_Digitsclose_priceprofitcurrency_profit);
      
     }
   /*
   结果:
   Estimated profit for 1.00 Buy position on EURUSD with opening price of 1.10757 and closing price of 1.10881124.00 USD
   Estimated profit for 1.00 Sell position on EURUSD with opening price of 1.10753 and closing price of 1.10881: -128.00 USD
   */
  }
//+------------------------------------------------------------------+
//| 根据订单类型返回开单价格                                             |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbolconst ENUM_ORDER_TYPE order_type)
  {
   MqlTick tick={};
 
//--- 取得交易品种的最新价格
   if(!SymbolInfoTick(symboltick))
     {
      Print("SymbolInfoTick() failed. Error "GetLastError());
      return 0;
     }
     
//--- 返回根据订单类型的价格
   switch(order_type)
     {
      case ORDER_TYPE_BUY              :  return(tick.ask);
      case ORDER_TYPE_SELL             :  return(tick.bid);
      default                          :  return(0);
     }
  }
//+------------------------------------------------------------------+
//| 返回订单类型的描述                                                  |
//+------------------------------------------------------------------+
string OrderTypeDescription(const ENUM_ORDER_TYPE order_type)
  {
   switch(order_type)
     {
      case ORDER_TYPE_BUY              :  return("Buy");
      case ORDER_TYPE_SELL             :  return("Sell");
      default                          :  return("Unknown order type");
     }
  }

另见

OrderSend()订单属性交易操作类型