OrderCalcMargin

이 함수는 현재 보류 주문 및 오픈 포지션를 고려하지 않고 현재 시장 환경에서 지정된 주문 유형에 필요한 마진을 계산합니다. 계획된 거래 작업에 대한 마진을 평가할 수 있습니다. 값은 계정의 통화로 반환됩니다.

bool  OrderCalcMargin(
   ENUM_ORDER_TYPE       action,           // 주문 유형
   string                symbol,           // 심볼명
   double                volume,           // 양
   double                price,            // 시가
   double&               margin            // 마진 값을 얻기 위한 변수
   );

매개 변수

action

[in]  이 값은 ENUM_ORDER_TYPE 열거값 중 하나일 수 있습니다.

Symbol

[in]  심볼명.

volume

[in]  거래 작업의 양.

price

[in]  시가.

margin

[out]  함수가 성공적으로 실행되는 경우 필요한 마진 값이 기록되는 변수. 연산은 현재 계정에서 보류 주문과 오픈 포지션이 없었던 것처럼 수행됩니다. 마진 값은 여러 요소에 따라 달라지며 시장 환경에 따라 다를 수 있습니다.

반환값

이 함수는 성공하면 true를 반환하고, 그렇지 않으면 false를 반환합니다. 오류에 대한 정보를 얻으려면, GetLastError() 함수를 호출하십시오.

예:

#define   VOLUME     1.0   // 주문 볼륨
#define   DEVIATION  100   // distance for setting a pending order
#define   STOP_LIMIT 50    // order StopLimit distance
 
//+------------------------------------------------------------------+
//| 스크립트 프로그램 시작 함수                                          |
//+------------------------------------------------------------------+
void OnStart()
  {
   string currency=AccountInfoString(ACCOUNT_CURRENCY);
   int array_types[8]={ORDER_TYPE_BUY,
                       ORDER_TYPE_SELL,
                       ORDER_TYPE_BUY_LIMIT,
                       ORDER_TYPE_SELL_LIMIT,
                       ORDER_TYPE_BUY_STOP,
                       ORDER_TYPE_SELL_STOP,
                       ORDER_TYPE_BUY_STOP_LIMIT,
                       ORDER_TYPE_SELL_STOP_LIMIT };
                       
//--- 주문 유형 배열의 루프에서
   for(int i=0; i<(int)array_types.Size(); i++)
     {
      //--- 주문 유형에 따라 ORDER_TYPE_BUY 또는 ORDER_TYPE_SELL 유형을 획득
      ENUM_ORDER_TYPE type=MarketOrderByOrderType((ENUM_ORDER_TYPE)array_types[i]);
      
      //--- 주문 유형에 따라 가격 획득
      double price=PriceByOrderType(_Symbol, type);
      
      //--- 'action'에 지정된 주문 유형에 필요한 마진 금액을 획득 
      double margin=EMPTY_VALUE;
      ResetLastError();
      if(!OrderCalcMargin(type, _Symbol, VOLUME, price, margin))
        {
         Print("OrderCalcMargin() failed. Error ",GetLastError());
         continue;
        }
      //--- 결과를 저널에 인쇄
      PrintFormat("Margin required for %.2f %s order at price %.*f on %s symbol: %.2f %s", VOLUME, OrderTypeDescription((ENUM_ORDER_TYPE)i), _Digits, price, _Symbol, margin, currency);
     }
   /*
   결과:
   Margin required for 1.00 Buy order at price 1.31668 on GBPUSD symbol: 1316.68 USD
   Margin required for 1.00 Sell order at price 1.31661 on GBPUSD symbol: 1316.61 USD
   Margin required for 1.00 Buy Limit order at price 1.31568 on GBPUSD symbol: 1315.68 USD
   Margin required for 1.00 Sell Limit order at price 1.31761 on GBPUSD symbol: 1317.61 USD
   Margin required for 1.00 Buy Stop order at price 1.31768 on GBPUSD symbol: 1317.68 USD
   Margin required for 1.00 Sell Stop order at price 1.31561 on GBPUSD symbol: 1315.61 USD
   Margin required for 1.00 Buy Stop Limit order at price 1.31718 on GBPUSD symbol: 1317.18 USD
   Margin required for 1.00 Sell Stop Limit order at price 1.31611 on GBPUSD symbol: 1316.11 USD
   */
  }
//+------------------------------------------------------------------+
//| 매수 또는 매도 주문 유형 반환                                        |
//+------------------------------------------------------------------+
ENUM_ORDER_TYPE MarketOrderByOrderType(ENUM_ORDER_TYPE type)
  {
   switch(type)
     {
      case ORDER_TYPE_BUY  : case ORDER_TYPE_BUY_LIMIT  : case ORDER_TYPE_BUY_STOP  : case ORDER_TYPE_BUY_STOP_LIMIT  :
        return(ORDER_TYPE_BUY);
      case ORDER_TYPE_SELL : case ORDER_TYPE_SELL_LIMIT : case ORDER_TYPE_SELL_STOP : case ORDER_TYPE_SELL_STOP_LIMIT :
        return(ORDER_TYPE_SELL);
     }
   return(WRONG_VALUE);
  }
//+------------------------------------------------------------------+
//| 주문 유형별 주문 진입 가격 반환                                      |
//+------------------------------------------------------------------+
double PriceByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
  {
   int     digits=0;
   double  point=0;
   MqlTick tick={};
 
//--- 심볼 포인트 값 획득
   ResetLastError();
   if(!SymbolInfoDouble(symbol, SYMBOL_POINT, point))
     {
      Print("SymbolInfoDouble() failed. Error ", GetLastError());
      return 0;
     }
     
//--- 심볼 자리수 획득
   long value=0;
   if(!SymbolInfoInteger(symbol, SYMBOL_DIGITS, value))
     {
      Print("SymbolInfoInteger() failed. Error ", GetLastError());
      return 0;
     }
   digits=(int)value;
   
//--- 심볼별 마지막 가격 가격 획득
   if(!SymbolInfoTick(symbol, tick))
     {
      Print("SymbolInfoTick() failed. Error ", GetLastError());
      return 0;
     }
     
//--- 주문 유형별 가격 반환                                 
   switch(order_type)
     {
      case ORDER_TYPE_BUY              :  return(tick.ask);
      case ORDER_TYPE_SELL             :  return(tick.bid);
      case ORDER_TYPE_BUY_LIMIT        :  return(NormalizeDouble(tick.ask - DEVIATION * point, digits));
      case ORDER_TYPE_SELL_LIMIT       :  return(NormalizeDouble(tick.bid + DEVIATION * point, digits));
      case ORDER_TYPE_BUY_STOP         :  return(NormalizeDouble(tick.ask + DEVIATION * point, digits));
      case ORDER_TYPE_SELL_STOP        :  return(NormalizeDouble(tick.bid - DEVIATION * point, digits));
      case ORDER_TYPE_BUY_STOP_LIMIT   :  return(NormalizeDouble(tick.ask + DEVIATION * point - STOP_LIMIT * point, digits));
      case ORDER_TYPE_SELL_STOP_LIMIT  :  return(NormalizeDouble(tick.bid - DEVIATION * point + STOP_LIMIT * point, digits));
      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");
      case ORDER_TYPE_BUY_LIMIT        :  return("Buy Limit");
      case ORDER_TYPE_SELL_LIMIT       :  return("Sell Limit");
      case ORDER_TYPE_BUY_STOP         :  return("Buy Stop");
      case ORDER_TYPE_SELL_STOP        :  return("Sell Stop");
      case ORDER_TYPE_BUY_STOP_LIMIT   :  return("Buy Stop Limit");
      case ORDER_TYPE_SELL_STOP_LIMIT  :  return("Sell Stop Limit");
      case ORDER_TYPE_CLOSE_BY         :  return("Close By");
      default                          :  return("Unknown order type");
     }
  }

참고 항목

OrderSend(), Order Properties, Trade Operation Types