//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- boucle sur toutes les positions du compte
int total=PositionsTotal();
for(int i=0; i<total; i++)
{
//--- obtient le ticket de la position suivante en sélectionnant automatiquement une position pour accéder à ses propriétés
ulong ticket=PositionGetTicket(i);
if(ticket==0)
continue;
//--- récupère le type de position et affiche l'en-tête de la liste des propriétés réelles de la position
string type=(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE ? "Buy" : "Sell");
PrintFormat("Double properties of an open position %s #%I64u:", type, ticket);
//--- imprime toutes les propriétés réelles de la position sélectionnée sous l'en-tête
PositionPropertiesDoublePrint(15);
}
/*
résultat :
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
*/
}
//+------------------------------------------------------------------+
//| Affiche les propriétés réelles de la position sélectionnée |
//+------------------------------------------------------------------+
void PositionPropertiesDoublePrint(const uint header_width=0)
{
uint w=0;
string header="";
double value=0;
//--- récupère la devise du compte, le symbole de position et le nombre de décimales du symbole
string currency=AccountInfoString(ACCOUNT_CURRENCY);
string symbol =PositionGetString(POSITION_SYMBOL);
int digits =(int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- définit le texte d'en-tête et la largeur du champ d'en-tête
//--- si la largeur de l'en-tête est passée à la fonction égale à zéro, alors la largeur sera la taille de la ligne d'en-tête + 1
header="Volume:";
w=(header_width==0 ? header.Length()+1 : header_width);
//--- récupère et affiche le volume de position avec la largeur d'en-tête spécifiée dans le journal
if(!PositionGetDouble(POSITION_VOLUME, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- affiche la valeur du prix de la position dans le journal
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);
//--- affiche la valeur du StopLoss dans le journal
header="StopLoss:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SL, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- affiche la valeur du TakeProfit dans le journal
header="TakeProfit:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_TP, value))
return;
PrintFormat("%-*s%-.*f", w, header, digits, value);
//--- affiche la valeur 'Prix actuel' dans le journal
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);
//--- affiche la valeur du swap accumulé dans le journal
header="Swap:";
w=(header_width==0 ? header.Length()+1 : header_width);
if(!PositionGetDouble(POSITION_SWAP, value))
return;
PrintFormat("%-*s%-.2f", w, header, value);
//--- affiche la valeur actuelle du bénéfice dans le journal
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);
}
|