OrderCalcMargin

La funzione calcola il margine richiesto per il tipo di ordine specificato, sul corrente account, nel corrente contesto di mercato che non tiene conto dei correnti ordini pendenti e posizioni aperte. Permette la valutazione del margine per l'operazione di trade prevista. Il valore viene restituito nella valuta del conto.

bool  OrderCalcMargin(
   ENUM_ORDER_TYPE       action,           // tipo di ordine
   string                symbol,           // nome del simbolo
   double                volume,           // volume
   double                price,            // prezzo di apertura
   double&               margin            // valore per ottenere il valore del margine
   );

Parametri

action

[in] Il tipo di ordine, può essere uno dei valori dell'enumerazione ENUM_ORDER_TYPE.

symbol

[in] Nome del Simbolo.

volume

[in] volume delle operazioni di trade.

price

[in] Prezzo di apertura.

margin

[out] La variabile, per cui il valore del margine richiesto sarà scritto nel caso la funzione viene eseguita correttamente. Il calcolo viene eseguito come se non c'erano ordini pendenti e posizioni aperte, sul corrente account. Il valore del margine dipende da molti fattori, e può variare in diversi contesti di mercato.

Valore restituito

La funzione restituisce true in caso di successo, altrimenti restituisce false. Al fine di ottenere informazioni sull' errore, chiamare la funzione GetLastError().

Esempio:

#define   VOLUME     1.0   // volume dell'ordine
#define   DEVIATION  100   // distanza per l'impostazione dell'ordine pendente
#define   STOP_LIMIT 50    // distanza ordine StopLimit
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
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 };
                       
//--- in un ciclo tramite l'array dei tipi di ordine
   for(int i=0i<(int)array_types.Size(); i++)
     {
      //--- a seconda del tipo di ordine, ottenere il tipo ORDER_TYPE_BUY o ORDER_TYPE_SELL
      ENUM_ORDER_TYPE type=MarketOrderByOrderType((ENUM_ORDER_TYPE)array_types[i]);
      
      //--- a seconda del tipo di ordine, ottenere il prezzo
      double price=PriceByOrderType(_Symboltype);
      
      //--- ottenere l'importo del margine richiesto per il tipo di ordine specificato in 'action' 
      double margin=EMPTY_VALUE;
      ResetLastError();
      if(!OrderCalcMargin(type_SymbolVOLUMEpricemargin))
        {
         Print("OrderCalcMargin() failed. Error ",GetLastError());
         continue;
        }
      //--- stampare il risultato nel journal
      PrintFormat("Margin required for %.2f %s order at price %.*f on %s symbol: %.2f %s"VOLUMEOrderTypeDescription((ENUM_ORDER_TYPE)i), _Digitsprice_Symbolmargincurrency);
     }
   /*
   risultato:
   Margin required for 1.00 Buy order at price 1.31668 on GBPUSD symbol1316.68 USD
   Margin required for 1.00 Sell order at price 1.31661 on GBPUSD symbol1316.61 USD
   Margin required for 1.00 Buy Limit order at price 1.31568 on GBPUSD symbol1315.68 USD
   Margin required for 1.00 Sell Limit order at price 1.31761 on GBPUSD symbol1317.61 USD
   Margin required for 1.00 Buy Stop order at price 1.31768 on GBPUSD symbol1317.68 USD
   Margin required for 1.00 Sell Stop order at price 1.31561 on GBPUSD symbol1315.61 USD
   Margin required for 1.00 Buy Stop Limit order at price 1.31718 on GBPUSD symbol1317.18 USD
   Margin required for 1.00 Sell Stop Limit order at price 1.31611 on GBPUSD symbol1316.11 USD
   */
  }
//+------------------------------------------------------------------+
//| Restituire il tipo di ordine Buy o Sell                          |
//+------------------------------------------------------------------+
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);
  }
//+------------------------------------------------------------------+
//| Restituire il prezzo di apertura per il tipo di ordine           |
//+------------------------------------------------------------------+
double PriceByOrderType(const string symbolconst ENUM_ORDER_TYPE order_type)
  {
   int     digits=0;
   double  point=0;
   MqlTick tick={};
 
//--- ottenere il valore del Punto per il simbolo
   ResetLastError();
   if(!SymbolInfoDouble(symbolSYMBOL_POINTpoint))
     {
      Print("SymbolInfoDouble() failed. Error "GetLastError());
      return 0;
     }
     
//--- ottenere il valore delle Cifre del simbolo
   long value=0;
   if(!SymbolInfoInteger(symbolSYMBOL_DIGITSvalue))
     {
      Print("SymbolInfoInteger() failed. Error "GetLastError());
      return 0;
     }
   digits=(int)value;
   
//--- ottenere gli ultimi prezzi del simbolo
   if(!SymbolInfoTick(symboltick))
     {
      Print("SymbolInfoTick() failed. Error "GetLastError());
      return 0;
     }
     
//--- restituire il prezzo a seconda del tipo di ordine
   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 * pointdigits));
      case ORDER_TYPE_SELL_LIMIT       :  return(NormalizeDouble(tick.bid + DEVIATION * pointdigits));
      case ORDER_TYPE_BUY_STOP         :  return(NormalizeDouble(tick.ask + DEVIATION * pointdigits));
      case ORDER_TYPE_SELL_STOP        :  return(NormalizeDouble(tick.bid - DEVIATION * pointdigits));
      case ORDER_TYPE_BUY_STOP_LIMIT   :  return(NormalizeDouble(tick.ask + DEVIATION * point - STOP_LIMIT * pointdigits));
      case ORDER_TYPE_SELL_STOP_LIMIT  :  return(NormalizeDouble(tick.bid - DEVIATION * point + STOP_LIMIT * pointdigits));
      default                          :  return(0);
     }
  }
//+------------------------------------------------------------------+
//| Restituire la descrizione del tipo di ordine                     |
//+------------------------------------------------------------------+
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");
     }
  }

Vedi anche

OrderSend(), Proprietà degli Ordini, Tipi di Operazioni di Trade