MQL4プログラミング中にStack overflowエラーが出て躓いた話

MQL4プログラミング中にStack overflowエラーが出て躓いた話

サムネ
こんにちは fx-on.comエンジニアの岩淵です。 皆さんは昨夜のFOMCでトレードしたでしょうか? 私は起きれませんでしたorz 最近MQL4をゴリゴリと書いていたら、Stack overflowというエラーが発生しました。 お恥ずかしながら長年MQL4を弄っておきながら、 このエラーに出会ったのが初めてでしたので、 解決にだいぶ手間取ってしまいました。 そこで備忘録的に記事に残したいと思います。 問題形式にしたのでプログラマの方は原因を考えてみてくださいw

問題

こちらが今回問題のコード 保有中のポジション情報を取得するクラスとなっております。 例えばTicket()を実行すると保有中のポジションのチケット番号を返す仕組みです。 このクラスからインスタンスを作成してTicket()を実行したところStack overflowとなりました。 何が問題でしょうか?
class Sample{
   private:
      int magic;
   public:
      Sample(int value);
      int    Ticket(void);
      string Symbol(void);
};

Sample::Sample(int value){
   this.magic = value;
}

int Sample::Ticket(void){
   for(int i = OrdersTotal() - 1; i >= 0; i++){
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) return(-1);
      if(OrderMagicNumber() != this.magic) continue;
      if(OrderSymbol() != Symbol()) continue;
      
      return(OrderTicket());
   }
   return(-1);
}

string Sample::Symbol(void){
   for(int i = OrdersTotal() - 1; i >= 0; i++){
      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) return(NULL);
      if(OrderMagicNumber() != this.magic) continue;
      if(OrderSymbol() != Symbol()) continue;
      
      return(OrderSymbol());
   }
   return(NULL);
}

ヒント1 スタックオーバーフローとは?

Google先生に尋ねたところ、wikipediaの記事を紹介して頂きました。 wikipediaによる事例
int f()
 {
   g();
 }

 int g()
 {
   f();  
 }
関数fを実行すると、関数gが実行される。 関数gを実行すると、関数fが実行される。 このように関数が終わりなく交互に実行されてプログラムが使用する容量をオーバーすることが原因でスタックオーバーフローが発生する。

ヒント2 問題の行

Ticket()内の処理を少しずつコメントアウトしていくと、次の1行に問題があることが分かりました。
if(OrderSymbol() != Symbol()) continue;

答え

原因は同じクラス内でSymbol()という名前のメソッドを作成していたことです。 if(OrderSymbol() != Symbol()) におけるSymbol()は、通貨ペア名を返すMQL4デフォルト関数を想定しておりましたが、同じクラス内に存在するSymbol()メソッドが優先して実行されておりました。 そしてSymbol()にも同じく if(OrderSymbol() != Symbol()) という処理が入っておりました。 このSymbol()によって再びSymbol()メソッドが実行され、Symbol()メソッドが実行され、Symbol()メソッドが実行され、Symbol()メソッドが実行され・・・・ といった具合に無限に実行されてオーバーフローとなったようです。 いかがでした? こんなの簡単すぎだよ~と思った方もいるかもしれませんが、デバッグしているときの自分は完全にif(OrderSymbol() != Symbol()) continue; にしか目が行ってなかったので解決に時間がかかってしまいました。 視野を広げるって大事ですね。 正直コーディングの段階でメソッド名にSymbolって大丈夫か?とは思いましたが、 コンパイルしてもエラー出てないし、まぁいいや~で作った結果がこれです。
■ Important Matters and Risk Warnings Regarding Investment Products 【Financial Instruments Business Operator】
GogoJungle Inc.
Registration: Director-General of the Kanto Local Finance Bureau (Financial Instruments Business) No. 1960
【Member Association】
Japan Investment Advisers Association
【Fees and Expenses】
The use of software, e-books, investment advisory services, etc., provided on our platform involves purchase prices determined for each product.
Additionally, when conducting actual financial instrument transactions (such as FX or CFD trading), costs such as transaction fees and spreads (the difference between the sell and buy price) may be incurred through the connected brokerage firms.
【Margin and Risk of Loss】
Transactions such as FX (Foreign Exchange Margin Trading) and CFD (Contract for Difference) allow for trading in amounts exceeding the deposited margin.
Therefore, due to sudden fluctuations in target indices (currency, commodity prices, etc.), there is a risk of incurring losses that exceed the deposited margin (loss exceeding principal).
Furthermore, financial instrument transactions involve risks such as price fluctuations, which may result in losses falling below or exceeding the initial investment principal.
【Nature of Services and Trading Entity】
The Company operates a platform (marketplace) providing investment software and information; we do not engage in the buying, selling, mediation, brokerage, or agency of financial instruments on behalf of customers.
The services and information provided are intended as reference for investment decisions and do not guarantee future profits or specific results.
While some services include investment advice, they do not constitute a mandate or solicitation to trade specific financial instruments.
Final investment decisions and the execution of trades must be made at the customer’s own risk and discretion.
GogoJungle Inc.
Registration: Director-General of the Kanto Local Finance Bureau (Financial Instruments Business) No. 1960
Member Association: Japan Investment Advisers Association
GogoJungle Inc.
Registration: Director-General of the Kanto Local Finance Bureau (Financial Instruments Business) No. 1960
Member Association: Japan Investment Advisers Association
Financial Services AgencyJapan Investment Advisers AssociationFinancial Instruments Mediation Assistance CenterSecurities and Exchange Surveillance Commission

Copyright © 2026 GogoJungle Inc. All Rights Reserved.