{{tag>mql5 mql4 cpositioninfo}} ====== MQL5 標準ライブラリのラッパークラスを作ろう 3 (CPositionInfo 編) ====== 前回までの記事は以下を参照。 * [[:metatrader:mql:csymbolinfo_wrapper|]] * [[:metatrader:mql:caccountinfo_wrapper|]] 今回は [[https://www.mql5.com/ja/docs/standardlibrary/tradeclasses/cpositioninfo|CPositionInfo]] のラッパークラス。\\ ===== ラップしたメソッド ===== * PositionsTotal * Ticket * Time * PositionType * Magic * PriceOpen * StopLoss * TakeProfit * Commission * Swap * Volume * Profit * Symbol * SelectByTicket * SelectByIndex ===== ラッパークラス ClPositionInfoHelper 完成! ===== MQL5 版のコードはこれまでと同様に ''CPositionInfo'' の対応メソッドを呼び出しているだけである。\\ 一方、MQL4 版のコードは ''OrderXXXX()'' 関数で対応する値を取得できるものはそうしているが、''PositionTotal()'', ''PositionType()'', ''SelectByTicket()'', ''SelectByIndex()'' は独自実装している。\\ ポジション情報の取得方法が MQL5 で変わったためこのような形になった。(MQL4 では ''OrderSelect()'' 関数でポジションを参照するが、MQL5では ''PositionSelect()'' 関数で参照するように変更された) === コード === //+------------------------------------------------------------------+ //| PositionInfoHelper.mqh | //+------------------------------------------------------------------+ #ifndef _POSITIONINFOHELPER_MQH_ #define _POSITIONINFOHELPER_MQH_ //+------------------------------------------------------------------+ //| インクルード | //+------------------------------------------------------------------+ #ifdef _MQL4 enum ENUM_POSITION_TYPE { POSITION_TYPE_BUY, POSITION_TYPE_SELL, }; #include #else // _MQL4 #include #endif // _MQL4 //+------------------------------------------------------------------+ //| ClPositionInfoHelperクラス | //+------------------------------------------------------------------+ class ClPositionInfoHelper : public CObject { public: ClPositionInfoHelper(void); virtual ~ClPositionInfoHelper(void); int PositionTotal(); // ポジション総数(MQL4ビルド時はオーダー総数である点に注意) ulong Ticket(void) const; // チケット番号 datetime Time(void) const; // 注文時間 ENUM_POSITION_TYPE PositionType(void) const; // ポジション種類 long Magic(void) const; // マジックナンバー double PriceOpen(void) const; // 注文価格 double StopLoss(void) const; // 決済逆指値 double TakeProfit(void) const; // 決済指値 double Commission(void) const; // 手数料 double Swap(void) const; // スワップ double Volume(void) const; // ボリューム double Profit(void) const; // 現在利益 string Symbol(void) const; // シンボル bool SelectByTicket(const ulong ticket); // ポジション選択 bool SelectByIndex(const int index); // ポジション選択 protected: #ifdef _MQL4 #else // _MQL4 CPositionInfo l_PositionInfo; // ポジション情報 #endif // _MQL4 }; //+------------------------------------------------------------------+ //| コンストラクター | //+------------------------------------------------------------------+ ClPositionInfoHelper::ClPositionInfoHelper(void) { } //+------------------------------------------------------------------+ //| デストラクター | //+------------------------------------------------------------------+ ClPositionInfoHelper::~ClPositionInfoHelper(void) { } //+------------------------------------------------------------------+ //| ポジション総数(MQL4ビルド時はオーダー総数である点に注意) | //+------------------------------------------------------------------+ int ClPositionInfoHelper::PositionTotal() { #ifdef _MQL4 return(OrdersTotal()); #else return(PositionsTotal()); #endif } //+------------------------------------------------------------------+ //| チケット番号 | //+------------------------------------------------------------------+ ulong ClPositionInfoHelper::Ticket(void) const { #ifdef _MQL4 return((ulong)OrderTicket()); #else return(l_PositionInfo.Ticket()); #endif } //+------------------------------------------------------------------+ //| ポジション種類 | //+------------------------------------------------------------------+ ENUM_POSITION_TYPE ClPositionInfoHelper::PositionType(void) const { #ifdef _MQL4 switch(OrderType()) { case OP_BUY: case OP_BUYLIMIT: // ないはず case OP_BUYSTOP: // ないはず return (POSITION_TYPE_BUY); case OP_SELL: case OP_SELLLIMIT: // ないはず case OP_SELLSTOP: // ないはず return (POSITION_TYPE_SELL); default: return (POSITION_TYPE_BUY); } #else return(l_PositionInfo.PositionType()); #endif } //+------------------------------------------------------------------+ //| 注文時間 | //+------------------------------------------------------------------+ datetime ClPositionInfoHelper::Time(void) const { #ifdef _MQL4 return(OrderOpenTime()); #else return(l_PositionInfo.Time()); #endif } //+------------------------------------------------------------------+ //| マジックナンバー | //+------------------------------------------------------------------+ long ClPositionInfoHelper::Magic(void) const { #ifdef _MQL4 return(OrderMagicNumber()); #else return(l_PositionInfo.Magic()); #endif } //+------------------------------------------------------------------+ //| 注文価格 | //+------------------------------------------------------------------+ double ClPositionInfoHelper::PriceOpen(void) const { #ifdef _MQL4 return(OrderOpenPrice()); #else return(l_PositionInfo.PriceOpen()); #endif } //+------------------------------------------------------------------+ //| 決済逆指値 | //+------------------------------------------------------------------+ double ClPositionInfoHelper::StopLoss(void) const { #ifdef _MQL4 return(OrderStopLoss()); #else return(l_PositionInfo.StopLoss()); #endif } //+------------------------------------------------------------------+ //| 決済指値 | //+------------------------------------------------------------------+ double ClPositionInfoHelper::TakeProfit(void) const { #ifdef _MQL4 return(OrderTakeProfit()); #else return(l_PositionInfo.TakeProfit()); #endif } //+------------------------------------------------------------------+ //| 手数料 | //+------------------------------------------------------------------+ double ClPositionInfoHelper::Commission(void) const { #ifdef _MQL4 return(OrderCommission()); #else return(l_PositionInfo.Commission()); #endif } //+------------------------------------------------------------------+ //| スワップ | //+------------------------------------------------------------------+ double ClPositionInfoHelper::Swap(void) const { #ifdef _MQL4 return(OrderSwap()); #else return(l_PositionInfo.Swap()); #endif } //+------------------------------------------------------------------+ //| ボリューム | //+------------------------------------------------------------------+ double ClPositionInfoHelper::Volume(void) const { #ifdef _MQL4 return(OrderLots()); #else return(l_PositionInfo.Volume()); #endif } //+------------------------------------------------------------------+ //| 現在利益 | //+------------------------------------------------------------------+ double ClPositionInfoHelper::Profit(void) const { #ifdef _MQL4 return(OrderProfit()); #else return(l_PositionInfo.Profit()); #endif } //+------------------------------------------------------------------+ //| シンボル | //+------------------------------------------------------------------+ string ClPositionInfoHelper::Symbol(void) const { #ifdef _MQL4 return(OrderSymbol()); #else return(l_PositionInfo.Symbol()); #endif } //+------------------------------------------------------------------+ //| ポジション選択 | //+------------------------------------------------------------------+ bool ClPositionInfoHelper::SelectByTicket(const ulong ticket) { #ifdef _MQL4 if(OrderSelect((int)ticket, SELECT_BY_TICKET)) { // 決済済みチケットや待機注文の場合はポジション選択失敗として扱う return(OrderCloseTime() == 0 && OrderType() != OP_BUYLIMIT && OrderType() != OP_BUYSTOP && OrderType() != OP_SELLLIMIT && OrderType() != OP_SELLSTOP); } else { return(false); } #else return(l_PositionInfo.SelectByTicket(ticket)); #endif } //+------------------------------------------------------------------+ //| ポジション選択 | //+------------------------------------------------------------------+ bool ClPositionInfoHelper::SelectByIndex(const int index) { #ifdef _MQL4 if(OrderSelect(index, SELECT_BY_POS, MODE_TRADES)) { // 決済済みチケットや待機注文の場合はポジション選択失敗として扱う return(OrderCloseTime() == 0 && OrderType() != OP_BUYLIMIT && OrderType() != OP_BUYSTOP && OrderType() != OP_SELLLIMIT && OrderType() != OP_SELLSTOP); } else { return(false); } #else return(l_PositionInfo.SelectByIndex(index)); #endif } #endif // _POSITIONINFOHELPER_MQH_ //+------------------------------------------------------------------+ === 使用例 === MQL4 のソースをビルドするときはソースに #define _MQL4 を定義することに留意する。 //+------------------------------------------------------------------+ //| Sample.mq4 | //+------------------------------------------------------------------+ #property strict #property copyright "" #property version "1.0" // MQL4用ソースを使用 #define _MQL4 // インクルード #include "SymbolInfoHelper.mqh" #include "PositionInfoHelper.mqh" //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { ClSymbolInfoHelper symbolInfo; if(!symbolInfo.Name(Symbol())) { Print("Error initializing symbol."); return(INIT_FAILED); } if(!symbolInfo.RefreshRates()) { Print("Error initializing symbol."); return(INIT_FAILED); } int magic = 1234; ClPositionInfoHelper positionInfo; int total = positionInfo.PositionTotal(); // ポジション総数(ただし、MQL4ビルド時はオーダー総数である点に注意) for(int i = 0; i < total; i++) { // ポジション選択 if(positionInfo.SelectByIndex(i)) { // 選択されたポジションのシンボル、マジックナンバーを確認 if(positionInfo.Symbol() == symbolInfo.Name() && positionInfo.Magic() == magic) { // ポジション情報を出力 PrintFormat("チケットNo. = %d", positionInfo.Ticket()); PrintFormat("タイプ = %s", positionInfo.PositionType() == POSITION_TYPE_BUY ? "POSITION_TYPE_BUY" : "POSITION_TYPE_SELL"); PrintFormat("オープン価格 = %s", DoubleToString(positionInfo.PriceOpen(), symbolInfo.Digits())); } } } return(INIT_SUCCEEDED); } // (以下、略)