#define DEVIATION 5 // allowed deviation from the price
#define VOLUME 1.0 // order volume
#define EXPERT_MAGIC 123 // MagicNumber
#define DIRECTION ORDER_TYPE_BUY // opened position direction (ORDER_TYPE_BUY or ORDER_TYPE_SELL)
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- リクエスト、検証、および結果の構造体を宣言して初期化する
MqlTradeRequest request={};
MqlTradeCheckResult check ={};
MqlTradeResult result ={};
//--- リクエストパラメータを準備する
PrepareRequest(_Symbol, DIRECTION, VOLUME, request);
//--- 取引リクエストパラメータを確認する
ResetLastError();
bool res=OrderCheck(request, check);
if(!res)
{
PrintFormat("Trade request verification completed with error %d\nServer retcode: %u, comment: %s", GetLastError(), check.retcode, check.comment);
return;
}
//--- 取引リクエストの確認が成功した - 取引リクエスト検証構造体のフィールドの説明を表示する
Print("Trade request verification completed successfully");
MqlTradeCheckResultPrint(check, 14);
//--- 取引リクエストを送る
if(!OrderSend(request, result))
Print("OrderSend error ", GetLastError()); // if unable to send the request, display the error code
//--- 操作についての情報
PrintFormat("Trade request result: retcode=%u, deal=%I64u, order=%I64u", result.retcode, result.deal, result.order);
/*
result with disabled auto trading in the client terminal:
Trade request verification completed with error 4752
Server retcode: 10027, comment: AutoTrading disabled by client
enable auto trading and check again on a closed market:
Experts automated trading is enabled
Trade request verification completed successfully
Retcode: 0
Balance: 10779.50 USD
Equity: 10779.50 USD
Profit: 0.00 USD
Margin: 1104.79 USD
Margin free: 9674.71 USD
Margin level: 975.71 %
Comment: Done
OrderSend error 4756
Trade request result: retcode=10018, deal=0, order=0
check on the open market:
Trade request verification completed successfully
Retcode: 0
Balance: 10779.50 USD
Equity: 10779.50 USD
Profit: 0.00 USD
Margin: 110.46 USD
Margin free: 10669.04 USD
Margin level: 9758.74 %
Comment: Done
Trade request result: retcode=10009, deal=2777010968, order=2802818813
*/
}
//+------------------------------------------------------------------+
//| 取引リクエストのパラメータを準備する |
//+------------------------------------------------------------------+
void PrepareRequest(const string symbol, const ENUM_ORDER_TYPE order_type, const double volume, MqlTradeRequest &request)
{
ENUM_ORDER_TYPE type=(DIRECTION !=ORDER_TYPE_BUY ? ORDER_TYPE_SELL : DIRECTION);
double price=(DIRECTION==ORDER_TYPE_BUY ? SymbolInfoDouble(Symbol(), SYMBOL_ASK) : SymbolInfoDouble(Symbol(), SYMBOL_BID));
//--- request parameters
request.action = TRADE_ACTION_DEAL; // trading operation type
request.symbol = symbol; // symbol
request.volume = volume; // volume
request.type = type; // order type
request.price = price; // open price
request.deviation = DEVIATION; // allowed deviation from the price
request.magic = EXPERT_MAGIC; // order MagicNumber
}
//+------------------------------------------------------------------+
//| 取引リクエストの検証結果のフィールドを |
//| 操作ログに表示する |
//+------------------------------------------------------------------+
void MqlTradeCheckResultPrint(const MqlTradeCheckResult &check, const uint header_width=0)
{
//--- 口座の通貨とその小数点以下の桁数を取得する
string currency=AccountInfoString(ACCOUNT_CURRENCY);
int digits =(int)AccountInfoInteger(ACCOUNT_CURRENCY_DIGITS);
//--- ヘッダーのテキストとヘッダーフィールドの幅を定義する
//--- ヘッダーの幅が関数に渡され、ゼロと等しい場合、幅はヘッダー行のサイズ+1になる
string header="Retcode:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- 指定された幅のヘッダーで戻りコードを操作ログに表示する
PrintFormat("%-*s%-u", w, header, check.retcode);
//--- 取引操作を実行した後の残高値を操作ログに表示する
header="Balance:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.*f %s", w, header, digits, check.balance, currency);
//--- 取引操作を実行した後の資産の値を操作ログに表示する
header="Equity:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.*f %s", w, header, digits, check.equity, currency);
//--- 取引操作を実行した後の浮動利益値を操作ログに表示する
header="Profit:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.*f %s", w, header, digits, check.profit, currency);
//--- 必要な取引操作に必要な証拠金の金額を操作ログに表示する
header="Margin:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.*f %s", w, header, digits, check.margin, currency);
//--- 取引操作を行った後に残る資本の値を操作ログに表示する
header="Margin free:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.*f %s", w, header, digits, check.margin_free, currency);
//--- 必要な取引操作を完了した後に設定される証拠金レベルを操作ログに表示する
header="Margin level:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-.2f %%", w, header, check.margin_level);
//--- レスポンスコードとエラー説明のコメントを操作ログに表示する
header="Comment:";
w=(header_width==0 ? header.Length()+1 : header_width);
PrintFormat("%-*s%-s", w, header, check.comment);
}
|