#define VOLUME 1.0 // volume de l'ordre
#define DEVIATION 100 // nombre de points avant le prix de clôture
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT); // devise du profit
double close_price=ChartPriceOnDropped(); // la coordonnée du prix correspondant au point où le script a été déposé avec la souris, sert de prix de clôture
//---
for(int i=0; i<=ORDER_TYPE_SELL; i++)
{
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i; // type de l'ordre : 0 - Buy, 1 - Sell
double open_price=PriceOpenByOrderType(_Symbol, order_type); // prix d'ouverture : Ask pour Buy, Bid pour Sell
//--- si le prix d'ouverture n'est pas reçu, continue la boucle
if(open_price==0)
continue;
//--- si le prix de clôture est nul (le script n'a pas été lancé en glissant sur le graphique), calcule le prix
if(close_price==0)
close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
//--- calcule le montant approximatif du profit pour le compte courant et l'environnement de marché en fonction des paramètres transmis
double profit=0;
ResetLastError();
if(!OrderCalcProfit(order_type, _Symbol, VOLUME, open_price, close_price, profit))
{
Print("OrderCalcProfit() failed. Error ", GetLastError());
continue;
}
//--- affiche dans le journal le profit reçu
PrintFormat("Estimated profit for %.2f %s position on %s with opening price of %.*f and closing price of %.*f: %.2f %s",
VOLUME, OrderTypeDescription(order_type), _Symbol, _Digits, open_price, _Digits, close_price, profit, currency_profit);
}
/*
résultat :
Estimated profit for 1.00 Buy position on EURUSD with opening price of 1.10757 and closing price of 1.10881: 124.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
*/
}
//+------------------------------------------------------------------+
//| Retourne le prix d'ouverture selon le type de l'ordre |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
MqlTick tick={};
//--- récupère les derniers prix par symbole
if(!SymbolInfoTick(symbol, tick))
{
Print("SymbolInfoTick() failed. Error ", GetLastError());
return 0;
}
//--- renvoie le prix en fonction du type de l'ordre
switch(order_type)
{
case ORDER_TYPE_BUY : return(tick.ask);
case ORDER_TYPE_SELL : return(tick.bid);
default : return(0);
}
}
//+------------------------------------------------------------------+
//| Retourne la description du type de l'ordre |
//+------------------------------------------------------------------+
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");
}
}
|