MT4 OrderSendのカスタマイズ関数





MT4での注文オーダーは下記「OrderSend」関数を利用しますが、パラメータが多い割に全てのパラメータを利用する訳ではありませ。

int OrderSend( 
    string symbol,            // symbol 
    int cmd,                  // operation 
    double volume,            // volume 
    double price,             // price 
    int slippage,             // slippage 
    double stoploss,          // stop loss 
    double takeprofit,        // take profit 
    string comment=NULL,      // comment 
    int magic=0,              // magic number 
    datetime expiration=0,    // pending order expiration 
    color arrow_color=clrNONE // color 
); 

そこで、下記カスタム関数を設けて利便性を高めています。

/*------------------------------------------------------
関数名 MyOrderSendP

内容 ポジションを建てるための注文処理

引数 int type
       double lots         :ロット数
       double price=0      :価格値
       int pos_id=0        :ポジションID
       double stoploss=0   :損切値
       double takeprofit=0 :利益確定値
       string comment=""  :コメント
 
戻り値 true:正常終了、false:異常終了

-------------------------------------------------------*/
bool MyOrderSendP(int type, double lots, double price=0, int pos_id=0 ,double stoploss=0, double takeprofit=0, string comment="")
{
    // 矢印の色データ
    color ArrowColor[2] = {clrBlue, clrRed};

    // 注文済みの場合は処理を中断
    // ※MyOrderTypeはカスタム関数
    if(MyOrderType(pos_id) != OP_NONE){
        return true; 
    }

    // 価格の正規化
    price = NormalizeDouble(price, _Digits); 

    // 買値、売値取得
    if(type == OP_BUY) price = Ask; //成行注文の買値
    if(type == OP_SELL) price = Bid; //成行注文の売値

    // 注文送信
    int ret = OrderSend(_Symbol, type, lots, price, Slippage, stoploss, takeprofit,
                        IntegerToString(MagicNumber[pos_id]) + ":" + comment, 
                        IntegerToString(MagicNumber[pos_id]), 0, ArrowColor[type]); 

    // 注文エラー 
    if(ret == -1){
        int err = GetLastError();
        Print("MyOrderSendP : ", err, " " , ErrorDescription(err));
        return false;
    }

    // 正常終了
    return true; 
}