OrderCalcProfit

この関数は、渡されたパラメータに基づいて、現在の銘柄での現在の口座の利益を計算します。これは、取引操作の結果の事前評価のために使用されます。値は口座の預金通貨で返されます。

bool  OrderCalcProfit(
  ENUM_ORDER_TYPE      action,          // 注文の種類 (ORDER_TYPE_BUY または ORDER_TYPE_SELL)
  string                symbol,          // 銘柄名
  double                volume,          // ボリューム
  double                price_open,      // 始値
  double                price_close,      // 終値
  double&               profit            // 利益値の取得に使用される変数
  );

パラメータ

動作

[in] ENUM_ORDER_TYPE 列挙のいずれか(ORDER_TYPE_BUY または ORDER_TYPE_SELL)の値を持つ注文の種類

symbol

[in]  銘柄名

ボリューム

[in]  取引操作のボリューム

price_open

[in]  始値

price_close

[in]  終値

profit

[out]  関数の実行が成功した場合に計算された利益の値を格納する変数。見積りされた利益の値は多くの要因に依存し、市場環境によって異なることがあります。

戻り値

成功の場合は true、それ以外の場合は false。無効な注文の種類が指定された場合、この関数はfalseを返します。エラー情報を取得するには GetLastError() 関数が呼ばれます。

例:

 
#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");
    }
 }

参照

OrderSend()注文プロパティ取引操作の種類