見出し画像

MT4の履歴を画面に出力するMQL4ソースコード


これはMT4の履歴ですが

これはOrderSelect()で取得したMODE_HISTORYの履歴です。

入出金はOpenPriceが0になるので、入出金を取得したい場合は有効かと思います。

例えば、OpenPriceが0かつProfitがプラスなら入金です。

ドローダウンのインジケーターにも応用できるかも。

あと最近はやりのモンテカルロ法のやつとかね?

ちなみにindex 0 から順番に古くなるので、最新は OrdersHistoryTotal() - 1です。



OrderSelect()の挙動を調べるようです。

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input int input_sonota_width = 200; // 列幅の設定
input int input_y_spacing = 30;     // 行間の設定

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   //---
   DisplayTradeHistory();
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   // No need to do anything on each tick for this example
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   // Clean up objects when the expert is removed
   CleanUpObjects();
  }
//+------------------------------------------------------------------+
//| Custom function to create an object                              |
//+------------------------------------------------------------------+
void CreateObject(string name, string text, int magic_number_list_index, int koumoku_index)
  {
   if (!ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0))
     {
      Print("Failed to create object: ", name);
      return;
     }
   ObjectSetInteger(0, name, OBJPROP_XDISTANCE, input_sonota_width * koumoku_index);
   ObjectSetInteger(0, name, OBJPROP_YDISTANCE, 30 + magic_number_list_index * input_y_spacing);
   ObjectSetString(0, name, OBJPROP_TEXT, text);
   ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 11);
   ObjectSetInteger(0, name, OBJPROP_COLOR, clrWhite);
   ObjectSetString(0, name, OBJPROP_FONT, "MS Gothic");
  }
//+------------------------------------------------------------------+
//| Function to print orders table                                   |
//+------------------------------------------------------------------+
void DisplayTradeHistory()
  {
   int totalOrders = OrdersHistoryTotal();
   string headers[] = {"Ticket", "Type", "Lots", "Symbol", "Open Price", "Close Price", "Profit", "Open Time", "Close Time", "Comment", "Commission", "Expiration", "Magic Number", "Stop Loss", "Swap", "Take Profit"};
   
   // Create headers
   for (int j = 0; j < ArraySize(headers); j++)
     {
      CreateObject("Header" + IntegerToString(j), headers[j], 0, j);
     }

   // Loop through all historical orders
   for (int i = 0; i < totalOrders; i++)
     {
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
         // Get order details
         string details[16];
         details[0] = IntegerToString(OrderTicket());
         details[1] = OrderType() == OP_BUY ? "Buy" : "Sell";
         details[2] = DoubleToString(OrderLots(), 2);
         details[3] = OrderSymbol();
         details[4] = DoubleToString(OrderOpenPrice(), 5);
         details[5] = DoubleToString(OrderClosePrice(), 5);
         details[6] = DoubleToString(OrderProfit(), 2);
         details[7] = TimeToString(OrderOpenTime(), TIME_DATE | TIME_MINUTES);
         details[8] = TimeToString(OrderCloseTime(), TIME_DATE | TIME_MINUTES);
         details[9] = OrderComment();

         
         // Create row
         for (int j = 0; j < ArraySize(details); j++)
           {
            CreateObject("OrderRow_" + IntegerToString(i) + "_" + IntegerToString(j), details[j], i + 1, j);
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| Function to clean up objects                                     |
//+------------------------------------------------------------------+
void CleanUpObjects()
  {
   int totalOrders = OrdersHistoryTotal();
   
   // Delete headers
   for (int j = 0; j < 16; j++)
     {
      ObjectDelete("Header" + IntegerToString(j));
     }
   
   // Loop through all historical orders and delete rows
   for (int i = 0; i < totalOrders; i++)
     {
      for (int j = 0; j < 16; j++)
        {
         ObjectDelete("OrderRow_" + IntegerToString(i) + "_" + IntegerToString(j));
        }
     }
  }
//+------------------------------------------------------------------+

ここから先は

0字
このマガジンで読み放題です。

EA開発者のためのサンプルコード集

期間限定 PayPay支払いすると抽選でお得に!

この記事が気に入ったらサポートをしてみませんか?