#define VOLUME 1.0 // 注文量
#define DEVIATION 100 // 終値までのポイント数
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
string currency_profit=SymbolInfoString(_Symbol,SYMBOL_CURRENCY_PROFIT); // 利益通貨
double close_price=ChartPriceOnDropped(); // the price coordinate, corresponding to the point where the script was dropped using the mouse, serves as the close price
//---
for(int i=0; i<=ORDER_TYPE_SELL; i++)
{
ENUM_ORDER_TYPE order_type=(ENUM_ORDER_TYPE)i; // order type: 0 - Buy, 1 - Sell
double open_price=PriceOpenByOrderType(_Symbol, order_type); // open price: for Buy - Ask, for Sell - Bid
//--- 始値が取得できない場合は、ループを続ける
if(open_price==0)
continue;
//--- 終値がゼロの場合(スクリプトがチャートにドラッグされて起動されなかった場合)、価格を計算する
if(close_price==0)
close_price=(order_type==ORDER_TYPE_BUY ? open_price + DEVIATION * _Point : open_price - DEVIATION * _Point);
//--- 渡されたパラメータに基づいて、現在の口座と市場環境に対するおおよその利益サイズを計算する
double profit=0;
ResetLastError();
if(!OrderCalcProfit(order_type, _Symbol, VOLUME, open_price, close_price, profit))
{
Print("OrderCalcProfit() failed. Error ", GetLastError());
continue;
}
//--- 受け取った利益値を操作ログに表示する
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);
}
/*
結果:
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
*/
}
//+------------------------------------------------------------------+
//| 注文タイプによる始値を返す |
//+------------------------------------------------------------------+
double PriceOpenByOrderType(const string symbol, const ENUM_ORDER_TYPE order_type)
{
MqlTick tick={};
//--- 銘柄ごとの最新価格を取得する
if(!SymbolInfoTick(symbol, tick))
{
Print("SymbolInfoTick() failed. Error ", GetLastError());
return 0;
}
//--- 注文タイプに応じて価格を返す
switch(order_type)
{
case ORDER_TYPE_BUY : return(tick.ask);
case ORDER_TYPE_SELL : return(tick.bid);
default : return(0);
}
}
//+------------------------------------------------------------------+
//| 注文タイプの説明を返す |
//+------------------------------------------------------------------+
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");
}
}
|