#define VOLUME 1.0 // Auftragsvolumen
#define DEVIATION 100 // Abstand in Punkten für das Schließen
//+------------------------------------------------------------------+
//| Skript Programm Start Funktion |
//+------------------------------------------------------------------+
void OnStart()
{
string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT); // Gewinnwährung
double close_price=ChartPriceOnDropped(); // als Schlusskurs dient die Preiskoordinate, die dem Punkt entspricht, an dem das Skript mit der Maus abgelegt wurde.
//---
for(int i=0; i<=ORDER_TYPE_SELL; i++)
{
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i; // Order-Typ: 0 - Buy, 1 - Sell
double open_price=PriceOpenByOrderType(_Symbol, order_type); // Eröffnungspreis: für Buy - Ask, für Sell - Bid
//--- wenn der Eröffnungskurs nicht empfangen wird, Schleife fortsetzen
if(open_price==0)
continue;
//--- wenn der Schlusskurs null ist (das Skript wurde nicht durch Ziehen auf das Diagramm gestartet), wird der Kurs berechnen
if(close_price==0)
close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
//--- Berechnen der ungefähren Gewinnhöhe für das aktuelle Konto und das Marktumfeld basierend auf den übergebenen Parametern
double profit=0;
ResetLastError();
if(!OrderCalcProfit(order_type, _Symbol, VOLUME, open_price, close_price, profit))
{
Print("OrderCalcProfit() failed. Error ", GetLastError());
continue;
}
//--- den erhaltenen Gewinn im Journal ausdrucken
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);
}
/*
Ergebnis:
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
*/
}
//+------------------------------------------------------------------+
//| Rückgabe des Eröffnungspreises nach Auftragsart |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
MqlTick tick={};
//--- Aktuelle Preise eines Symbols abrufen
if(!SymbolInfoTick(symbol, tick))
{
Print("SymbolInfoTick() failed. Error ", GetLastError());
return 0;
}
//--- Preis abhängig von der Auftragsart zurückgeben
switch(order_type)
{
case ORDER_TYPE_BUY : return(tick.ask);
case ORDER_TYPE_SELL : return(tick.bid);
default : return(0);
}
}
//+------------------------------------------------------------------+
//| Rückgabe der Beschreibung des Auftragstyps |
//+------------------------------------------------------------------+
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");
}
}
|