//+------------------------------------------------------------------+ //| 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_ //+------------------------------------------------------------------+