OrderCalcProfit

이 함수는 전달된 매개 변수를 기반으로 현재 시장 조건에서 현재 계정의 이익을 계산합니다. 이 함수는 거래 작업의 결과를 사전 평가하는 데 사용됩니다. 값은 계정의 통화로 반환됩니다.

bool  OrderCalcProfit(
   ENUM_ORDER_TYPE       action,           // 주문 유형 (ORDER_TYPE_BUY or 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 - Sell
      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(), Order Properties, Trade Operation Types