//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- en un ciclo por todas las posiciones en la cuenta
int total=PositionsTotal();
for(int i=0; i<total; i++)
{
//--- obtenemos el ticket de la siguiente posición, seleccionando una posición de forma automática para acceder a sus propiedades
ulong ticket=PositionGetTicket(i);
if(ticket==0)
continue;
//--- obtenemos el tipo de la posición y mostramos el encabezado de la lista de propiedades reales de la posición
string type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE ? "Buy" : "Sell");
PrintFormat("Double properties of an open position %s #%I64u:", type, ticket);
//--- imprimimos bajo el encabezado todas las propiedades reales de la posición seleccionada
PositionPropertiesDoublePrint(15);
}
/*
resultado:
Double properties of an open position Buy #2807075208:
Volume: 1.00
Price open: 1.10516
StopLoss: 0.00000
TakeProfit: 0.00000
Price current: 1.10518
Swap: 0.00
Profit: 2.00 USD
*/
}
//+------------------------------------------------------------------+
//| Muestra en el registro las propiedades reales de la posición seleccionada
//+------------------------------------------------------------------+
void PositionPropertiesDoublePrint(const uint header_width=0)
{
uint w=0;
string header="";
double value=0;
//--- obtenemos la divisa de la cuenta, el símbolo de la posición y el número de decimales del símbolo
string currency=AccountInfoString(ACCOUNT_CURRENCY);
string symbol =PositionGetString(POSITION_SYMBOL);
int digits =(int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- definimos el texto del encabezado y la anchura del campo del encabezado
//--- si la anchura del encabezado se ha transmitido a la función como igual a cero, entonces la anchura será el tamaño de la línea del encabezado + 1
header="Volume:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- obtenemos y mostramos en el registro el volumen de la posición con un encabezado de la anchura establecida
if(!PositionGetDouble(POSITION_VOLUME, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- mostramos en el registro el valor del precio de la posición
header="Price open:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_OPEN, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- mostramos en el regisro el valor de StopLoss
header="StopLoss:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SL, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- mostramos en el registro el valor de TakeProfit
header="TakeProfit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_TP, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- mostramos en el registro el valor Price current
header="Price current:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PRICE_CURRENT, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- mostramos en el registro el valor de swap acumulado
header="Swap:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SWAP, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- mostramos en el registro el valor de beneficio actual
header="Profit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_PROFIT, value))
return;
PrintFormat("%-*s%-.2f %s", w, header, value, currency);
}
|