{{tag>mql5 mql4 caccountinfo}} ====== MQL5 標準ライブラリのラッパークラスを作ろう 2 (CAccountInfo 編) ====== [[:metatrader:mql:csymbolinfo_wrapper|]] の続き。\\ ラッパークラスを作るに至った経緯は上記ページを参照。\\ 今回は [[https://www.mql5.com/ja/docs/standardlibrary/tradeclasses/caccountinfo|CAccountInfo]] のラッパークラス。\\ ===== ラップしたメソッド ===== * MarginMode * Balance * Credit * Profit * Equity * Margin * FreeMargin * MarginLevel * Currency * OrderProfitCheck * FreeMarginCheck ===== ラッパークラス ClAccountInfoHelper 完成! ===== 3 分クッキング方式( ^ω^)\\ MQL5 版のコードは ''CAccountInfo'' の対応メソッドを呼び出しているだけである。\\ MQL4 版のコードは ''AccountInfoDouble()'' で対応する値を取得している。ただし、''MarginMode()'', ''OrderProfitCheck()'', ''FreeMarginCheck()'' は他と異なった実装になっている。\\ 特に ''MarginMode()'' の戻り値の ENUM_ACCOUNT_MARGIN_MODE は MQL4 に存在しないデータのため、便宜的に enum の宣言を追加し固定値 (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING) を返却している。 === コード === //+------------------------------------------------------------------+ //| AccountInfoHelper.mqh | //+------------------------------------------------------------------+ #ifndef _ACCOUNTINFOHELPER_MQH_ #define _ACCOUNTINFOHELPER_MQH_ //+------------------------------------------------------------------+ //| インクルード | //+------------------------------------------------------------------+ #ifdef _MQL4 enum ENUM_ACCOUNT_MARGIN_MODE { ACCOUNT_MARGIN_MODE_RETAIL_NETTING, ACCOUNT_MARGIN_MODE_EXCHANGE, ACCOUNT_MARGIN_MODE_RETAIL_HEDGING, }; #include #else // _MQL4 #include #endif // _MQL4 //+------------------------------------------------------------------+ //| ClAccountInfoHelperクラス | //+------------------------------------------------------------------+ class ClAccountInfoHelper : public CObject { public: ClAccountInfoHelper(void); virtual ~ClAccountInfoHelper(void); ENUM_ACCOUNT_MARGIN_MODE MarginMode(void) const; // マージンモード double Balance(void) const; // 口座残高 double Credit(void) const; // 与えられたクレジットの額 double Profit(void) const; // 口座の含み損益 double Equity(void) const; // 有効証拠金 double Margin(void) const; // 必要証拠金 double FreeMargin(void) const; // 余剰証拠金 double MarginLevel(void) const; // 証拠金維持率 string Currency(void) const; // 通貨名 string CurrencyJPN(void) const; // 通貨名 double OrderProfitCheck(const string symbol, const ENUM_ORDER_TYPE trade_operation, const double volume, const double price_open, const double price_close) const; double FreeMarginCheck(const string symbol, const ENUM_ORDER_TYPE trade_operation, const double volume, const double price) const; protected: #ifdef _MQL4 #else // _MQL4 CAccountInfo l_AccountInfo; // アカウント情報 #endif // _MQL4 }; //+------------------------------------------------------------------+ //| コンストラクター | //+------------------------------------------------------------------+ ClAccountInfoHelper::ClAccountInfoHelper(void) { } //+------------------------------------------------------------------+ //| デストラクター | //+------------------------------------------------------------------+ ClAccountInfoHelper::~ClAccountInfoHelper(void) { } //+------------------------------------------------------------------+ //| マージンモード | //+------------------------------------------------------------------+ ENUM_ACCOUNT_MARGIN_MODE ClAccountInfoHelper::MarginMode(void) const { #ifdef _MQL4 return(ACCOUNT_MARGIN_MODE_RETAIL_HEDGING); #else return(l_AccountInfo.MarginMode()); #endif } //+------------------------------------------------------------------+ //| 口座残高 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::Balance(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_BALANCE)); #else return(l_AccountInfo.Balance()); #endif } //+------------------------------------------------------------------+ //| 与えられたクレジットの額 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::Credit(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_CREDIT)); #else return(l_AccountInfo.Credit()); #endif } //+------------------------------------------------------------------+ //| 口座の含み損益 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::Profit(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_PROFIT)); #else return(l_AccountInfo.Profit()); #endif } //+------------------------------------------------------------------+ //| 有効証拠金 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::Equity(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_EQUITY)); #else return(l_AccountInfo.Equity()); #endif } //+------------------------------------------------------------------+ //| 必要証拠金 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::Margin(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_MARGIN)); #else return(l_AccountInfo.Margin()); #endif } //+------------------------------------------------------------------+ //| 余剰証拠金 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::FreeMargin(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_FREEMARGIN)); #else return(l_AccountInfo.FreeMargin()); #endif } //+------------------------------------------------------------------+ //| 証拠金維持率 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::MarginLevel(void) const { #ifdef _MQL4 return(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)); #else return(l_AccountInfo.MarginLevel()); #endif } //+------------------------------------------------------------------+ //| 通貨名 | //+------------------------------------------------------------------+ string ClAccountInfoHelper::Currency(void) const { #ifdef _MQL4 return(AccountInfoString(ACCOUNT_CURRENCY)); #else return(l_AccountInfo.Currency()); #endif } //+------------------------------------------------------------------+ //| 通貨名 | //+------------------------------------------------------------------+ string ClAccountInfoHelper::CurrencyJPN(void) const { #ifdef _MQL4 string currency = Currency(); if(currency == "JPY") { currency = "円"; } else if(currency == "USD") { currency = "ドル"; } else if(currency == "EUR") { currency = "ユーロ"; } return(currency); #else string currency = Currency(); if(currency == "JPY") { currency = "円"; } else if(currency == "USD") { currency = "ドル"; } else if(currency == "EUR") { currency = "ユーロ"; } return(currency); #endif } //+------------------------------------------------------------------+ //| 想定利益(損失)額の計算 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::OrderProfitCheck(const string symbol, const ENUM_ORDER_TYPE trade_operation, const double volume, const double price_open, const double price_close) const { #ifdef _MQL4 double profit = EMPTY_VALUE; if(trade_operation == ORDER_TYPE_BUY || trade_operation == ORDER_TYPE_BUY_LIMIT || trade_operation == ORDER_TYPE_BUY_STOP) { profit = (price_close - price_open) * volume * MarketInfo(symbol, MODE_TICKVALUE) / MarketInfo(symbol, MODE_POINT); } else if(trade_operation == ORDER_TYPE_SELL || trade_operation == ORDER_TYPE_SELL_LIMIT || trade_operation == ORDER_TYPE_SELL_STOP) { profit = (price_open - price_close) * volume * MarketInfo(symbol, MODE_TICKVALUE) / MarketInfo(symbol, MODE_POINT); } return(profit); #else return(l_AccountInfo.OrderProfitCheck(symbol, trade_operation, volume, price_open, price_close)); #endif } //+------------------------------------------------------------------+ //| ポジションを持ったと仮定した場合の余剰証拠金 | //+------------------------------------------------------------------+ double ClAccountInfoHelper::FreeMarginCheck(const string symbol, const ENUM_ORDER_TYPE trade_operation, const double volume, const double price) const { #ifdef _MQL4 double freeMargin = 0.0; if(trade_operation == ORDER_TYPE_BUY || trade_operation == ORDER_TYPE_BUY_LIMIT || trade_operation == ORDER_TYPE_BUY_STOP) { ResetLastError(); freeMargin = AccountFreeMarginCheck(symbol, OP_BUY, volume); if(GetLastError() == ERR_NOT_ENOUGH_MONEY) { freeMargin = 0.0; } } else if(trade_operation == ORDER_TYPE_SELL || trade_operation == ORDER_TYPE_SELL_LIMIT || trade_operation == ORDER_TYPE_SELL_STOP) { ResetLastError(); freeMargin = AccountFreeMarginCheck(symbol, OP_SELL, volume); if(GetLastError() == ERR_NOT_ENOUGH_MONEY) { freeMargin = 0.0; } } return(freeMargin); #else return(l_AccountInfo.FreeMarginCheck(symbol, trade_operation, volume, price)); #endif } #endif // _ACCOUNTINFOHELPER_MQH_ //+------------------------------------------------------------------+ === 使用例 === MQL4 のソースをビルドするときはソースに #define _MQL4 を定義することに留意する。 //+------------------------------------------------------------------+ //| Sample.mq4 | //+------------------------------------------------------------------+ #property strict #property copyright "" #property version "1.0" // MQL4用ソースを使用 #define _MQL4 // インクルード #include "AccountInfoHelper.mqh" //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { ClAccountInfoHelper accountInfo; PrintFormat("口座残高 = %s %s", DoubleToString(accountInfo.Balance(), 0), accountInfo.Currency()); PrintFormat("有効証拠金 = %s %s", DoubleToString(accountInfo.Equity(), 0), accountInfo.Currency()); PrintFormat("証拠金維持率 = %s %%", DoubleToString(accountInfo.MarginLevel(), 2)); return(INIT_SUCCEEDED); } // (以下、略)