跳至内容

CustomRatesDelete

CustomRatesDelete

删除指定时间间隔内自定义交易品种价格历史的全部柱形图。

int  CustomRatesDelete(
   const string     symbol,       // 交易品种名称
   datetime         from,         // 开始日期
   datetime         to            // 结束日期
   );

参数

交易品种

[in] 自定义交易品种名称。

from

[in] 即将移除的指定范围内价格历史的第一个柱形图时间。

to

[in] 即将移除的指定范围内价格历史的最后柱形图时间。

返回值

已删除柱形图的数量, 错误情况下为-1。

示例:

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

#define   CUSTOM_SYMBOL_NAME     Symbol()+".C"     // 自定义交易品种名称
#define   CUSTOM_SYMBOL_PATH     "Forex"           // 创建的交易品种所在组的名称
#define   CUSTOM_SYMBOL_ORIGIN   Symbol()          // 自定义交易品种的基础交易品种名称

#define   DATARATES_COUNT        4                 // 发送到日志的柱形数量

//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 取得创建自定义交易品种时的错误代码
   int create=CreateCustomSymbol(CUSTOM_SYMBOL_NAME, CUSTOM_SYMBOL_PATH, CUSTOM_SYMBOL_ORIGIN);

//--- 如果错误代码不为0 (创建交易品种成功) 也不为 5304 (交易品种已经被创建) - 退出
   if(create!=0 && create!=5304)
      return;

//--- 获取标准交易品种的柱数
   int bars=Bars(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1);

//--- 把标准交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组
   MqlRates rates[]={};
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_ORIGIN, PERIOD_M1, 0, bars, rates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_ORIGIN, bars, GetLastError());
      return;
     }

//--- 把复制的数据设为自定义交易品种的分钟历史
   ResetLastError();
   if(CustomRatesUpdate(CUSTOM_SYMBOL_NAME, rates)<0)
     {
      PrintFormat("CustomRatesUpdate(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
      return;
     }

//--- 在更新历史数据之后,读取自定义交易品种的柱形数量
   bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);

//--- 把自定义交易品种1分钟时段的所有柱形数据读取至 MqlRates 数组
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
      return;
     }

//--- 把自定义交易品种1分钟历史中的最新 DATARATES_COUNT 个柱的数据打印到日志中
   int digits=(int)SymbolInfoInteger(CUSTOM_SYMBOL_NAME, SYMBOL_DIGITS);
   PrintFormat("Last %d bars of the custom symbol's minute history:", DATARATES_COUNT);
   ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_COUNT);

//--- 删除自定义交易品种1分钟历史中从倒数第二个柱开始的两个柱的数据
   datetime time_from= rates[bars-3].time;
   datetime time_to  = rates[bars-2].time;
   ResetLastError();
   int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME, time_from, time_to);
   if(deleted<0)
     {
      PrintFormat("CustomRatesDelete(%s) failed. Error %d", CUSTOM_SYMBOL_NAME, GetLastError());
      return;
     }

//--- 在删除两个柱的历史数据之后,再次读取自定义交易品种的柱数
   bars=Bars(CUSTOM_SYMBOL_NAME, PERIOD_M1);

//--- 再次读取自定义交易品种1分钟时段所有剩余柱的数据
   ResetLastError();
   if(CopyRates(CUSTOM_SYMBOL_NAME, PERIOD_M1, 0, bars, rates)!=bars)
     {
      PrintFormat("CopyRates(%s, PERIOD_M1, 0, %d) failed. Error %d", CUSTOM_SYMBOL_NAME, bars, GetLastError());
      return;
     }

//--- 在日志中打印更新后的自定义交易品种1分钟历史中的最后 DATARATES_COUNT 个柱
   PrintFormat("\nLast %d bars after applying CustomRatesDelete() with %d deleted bars:", DATARATES_COUNT, deleted);
   ArrayPrint(rates, digits, NULL, bars-DATARATES_COUNT, DATARATES_COUNT);

//--- 在图表注释区显示脚本终止键的提示
   Comment(StringFormat("Press 'Esc' to exit or 'Del' to delete the '%s' symbol and exit", CUSTOM_SYMBOL_NAME));
//--- 在无尽循环中等待按下 Esc 或 Del 键
   while(!IsStopped() && TerminalInfoInteger(TERMINAL_KEYSTATE_ESCAPE)==0)
     {
      Sleep(16);
      //--- 当按下 Del 时, 删除所创建的自定义交易品种和它的数据
      if(TerminalInfoInteger(TERMINAL_KEYSTATE_DELETE)<0)
        {
         //--- 删除柱形数据
         int deleted=CustomRatesDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX);
         if(deleted>0)
            PrintFormat("%d history bars of the custom symbol '%s' were successfully deleted", deleted, CUSTOM_SYMBOL_NAME);

         //--- 删除报价数据
         deleted=CustomTicksDelete(CUSTOM_SYMBOL_NAME, 0, LONG_MAX);
         if(deleted>0)

//--- 删除交易品种
         if(DeleteCustomSymbol(CUSTOM_SYMBOL_NAME))
            PrintFormat("Custom symbol '%s' deleted successfully", CUSTOM_SYMBOL_NAME);
         break;
        }
     }
//--- 退出前清空图表
   Comment("");
   /*
   结果:
   Last 4 bars of the custom symbol's minute history:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.06.18 20:53:00 1.07341 1.07347 1.07336 1.07343            38        0             0
   [1] 2024.06.18 20:54:00 1.07344 1.07354 1.07344 1.07353            21        0             0
   [2] 2024.06.18 20:55:00 1.07353 1.07362 1.07351 1.07356            32        0             0
   [3] 2024.06.18 20:56:00 1.07356 1.07358 1.07352 1.07354            24        0             0

   Last 4 bars after applying CustomRatesDelete() with 2 deleted bars:
                    [time]  [open]  [high]   [low] [close] [tick_volume] [spread] [real_volume]
   [0] 2024.06.18 20:51:00 1.07357 1.07358 1.07347 1.07349            25        0             0
   [1] 2024.06.18 20:52:00 1.07349 1.07350 1.07336 1.07341            31        0             0
   [2] 2024.06.18 20:53:00 1.07341 1.07347 1.07336 1.07343            38        0             0
   [3] 2024.06.18 20:56:00 1.07356 1.07358 1.07352 1.07354            24        0             0
   */
  }
//+------------------------------------------------------------------+
//| 创建一个自定义交易品种, 返回错误代码                                  |
//+------------------------------------------------------------------+
int CreateCustomSymbol(const string symbol_name, const string symbol_path, const string symbol_origin=NULL)
  {
//--- 定义自定义交易品种所基于的交易品种名称
   string origin=(symbol_origin==NULL ? Symbol() : symbol_origin);

//--- 如果创建自定义交易品种失败并且错误代码不是 5304, 在日志中报告
   ResetLastError();
   int error=0;
   if(!CustomSymbolCreate(symbol_name, symbol_path, origin))
     {
      error=GetLastError();
      if(error!=5304)
         PrintFormat("CustomSymbolCreate(%s, %s, %s) failed. Error %d", symbol_name, symbol_path, origin, error);
     }
//--- 成功
   return(error);
  }
//+------------------------------------------------------------------+
//| 删除一个自定义交易品种                                              |
//+------------------------------------------------------------------+
bool DeleteCustomSymbol(const string symbol_name)
  {
//--- 从市场报价窗口中隐藏交易品种
   ResetLastError();
   if(!SymbolSelect(symbol_name, false))
     {
      PrintFormat("SymbolSelect(%s, false) failed. Error %d", GetLastError());
      return(false);
     }

//--- 如果删除自定义交易品种失败,在日志中报告并返回 'false'
   ResetLastError();
   if(!CustomSymbolDelete(symbol_name))
     {
      PrintFormat("CustomSymbolDelete(%s) failed. Error %d", symbol_name, GetLastError());
      return(false);
     }
//--- 成功
   return(true);
  }

另见

CustomRatesReplaceCustomRatesUpdateCopyRates