量化交易软件:赫兹量化中研究PrintFormat并应用现成的示例

概述
在终端中运行的任何应用程序都应该能够向用户通知其状态及其环境的状态。这些数据总是打印在程序日志中,您可以在交易终端的专家选项卡中找到。随后,在解析和分析程序日志时,重要的是所有条目都要采用易于阅读的形式,因为阅读和搜索信息的容易程度取决于此。

MQL5语言具有Print()函数,该函数将行或转换为行的数据打印到日志中。这对于短日志记录来说已经足够了。但是,如果我们向日志发送大量数据,那么最好以易于理解的形式对它们进行格式化。

当数据头(描述)及其相应的值在没有任何格式的情况下写入一行时,在短列表中读取这些数据不会造成困难。然而,如果有很多数据,并且它们都相互连接,具有不同长度的描述和含义,那么所有这些都会变成一长串信息。建议对此类数据进行格式化,并将其转换为单一的表格形式,其中所有标题都具有相同的宽度(标题列),并且与标题对应的数据将位于与标题(数据列)相同的距离处。PrintFormat()正可以这样做。它按照指定的格式将字符和数值集格式化并打印到EA日志中。

PrintFormat(). 工作原理
PrintFormat()函数输入接收一个格式字符串,该字符串描述了数据应该以何种形式显示,以及一组与格式字符串相对应的数据,这些数据将根据它们在日志中的显示方式位于结果字符串中。您可以为每个值设置自己的输出格式。简单来说,它看起来是这样的:

("%Value1要这样显示%Value2要这样显示%Value3要这样显示 ... ... %ValueN要这样显示 一些文本", Value1, Value2, Value3, ... ..., ValueN)
正如您所看到的,在需要在文本中显示某些数据的地方,应该有描述显示输出数据值的方法的格式行。每个格式字符串的前面都有%,这表明下面是输出格式的描述。您可以在格式描述之间编写任何文本(在上面的示例中,“一些文本”位于末尾)。在完成具有格式字符和文本的行的输入后,数据以逗号分隔,其顺序是在格式行中为它们描述输出格式。在上面的例子中,每个格式描述及其相应的值都用相同的颜色标记。

首先,我们写出应该显示在日志中的文本。接下来,在文本中,如果数据应该位于此位置,就输入数据输出格式的符号。

例如: ("%这些文本将位于50个字符宽度的列中%这些double 数据应该在下一列并且向左对齐", string_variable_with_text_of_the_first_column, double data_of_the_second_column);

当然,您不需要文本来描述这一切,因为有特殊格式的字符。它们应该按照严格指定的顺序进行,但不是所有的都应该出现在格式行中。

格式字符串是从左到右读取的。当遇到第一个格式规范(如果有)时,根据给定规范转换和显示格式字符串后的第一个参数的值。第二个格式规范引出第二个参数被转换和显示,依此类推,直到格式字符串结束。

格式规范具有以下形式:

         %[flags][width][.precision][{h | l | ll | I32 | I64}]type

让我们看看格式字符串中每个项目的帮助中写了什么,并对其进行更详细的描述。

百分比符号(%)--开始一个新的格式字符串。在此符号之后,必须至少指定一个说明符,并且必须了解类型说明符(<b1>类型</b1>)是格式化输出所需的唯一字段。也就是说,如果在一行中输入了“%”符号,则必须在其后面指示在此位置显示的数据类型,或者是另一个百分号,以便在文本中写入“%”号,要表明这不是新格式行的开始,而只是文本中的一个百分比符号,您需要编写组合:“%%”。

标志(flags):

-(减号),行文本将在指定的宽度内左对齐。宽度由标志后面的宽度说明符指定。“%-10”表示文本将与字段的左边缘对齐,该字段宽10个字符。这一行中的下一个文本将从10个字符的同一字段的右边缘开始定位。如果此10个字符字段中显示的文本大于10个字符,则以下文本将位于文本的末尾,而不是字段。换句话说,字段的宽度将扩展到其中放置的文本的大小。文本不会沿着字段的宽度进行修剪,相反,字段会沿着文本的长度展开。
void OnStart()
  {
//--- Declare the variables to be printed
   string   header="";  // Data header
   double   value1=0;   // First value
   double   value2=0;   // Second value
   
//--- First line with a header of less than 10 characters and data in 20-character fields
//--- The header and data are pressed to the left edge of their field
   header="10characters";
   value1=10000000;
   value2=20000000;
   PrintFormat("%-10s%-20ld%-20lld",header,(int)value1,(long)value2);
   
//--- Second line with a header of more than 10 characters and data in 20-character fields
//--- The header and data are pressed to the left edge of their field
   header="Over10characters";
   value1=10000000;
   value2=20000000;
   PrintFormat("%-10s%-20ld%-20lld",header,(int)value1,(long)value2);

   /* Sample output:
      10characters10000000            20000000            
      Over10characters10000000            20000000            
   */
  }

+(加号),对于带符号的类型,根据输出数据的值,始终显示+或-符号。例如:+222或-12.35。如果格式化文本的两行是连续的,而在同一数据列的顶行中,输出数据的值是正的,在底行中是负的,那么在视觉上,这些值将相对彼此移动一个字符(通过“-”号)。下面我们将探讨一个消除这种视觉不便的标志。
void OnStart()
  {
//--- Declare the variables to be printed
   string   header="";  // Data header
   double   value1=0;   // First value
   double   value2=0;   // Second value
   
//--- The first line with a header in a 16-character field and data in 20-character fields
//--- The header and data are pressed to the left edge.
//--- Data values of the second field are displayed with +/- signs
   header="Header1";
   value1=-10000000;
   value2=20000000;
   PrintFormat("%-16s%-+20ld%-20lld",header,(int)value1,(long)value2);
   
//--- The second line with a header in a 16-character field and data in 20-character fields
//--- The header and data are pressed to the left edge.
//--- Data values of the second field are displayed with +/- signs
   header="Header2";
   value1=10000000;
   value2=-20000000;
   PrintFormat("%-16s%-+20ld%-20lld",header,(int)value1,(long)value2);
   
   /* Sample output:
      Header1      -10000000           20000000            
      Header2      +10000000           -20000000           
   */
  }

0 (零),在指定的字段宽度内,零被添加到输出值之前。如果标志0是用整数格式(i,u,x,x,o,d)指定的,并且设置了精度规范(例如%04.d),则0将被忽略。换句话说,对于给定精度格式(至少输出这么多字符)的整数类型,这种格式(格式0)的前导零没有优先级,因为它们填充了整个字段,这与精度格式相矛盾。
void OnStart()
  {
//--- Declare the variables to be printed
   string   header="";  // Data header
   double   value1=0;   // First value
   double   value2=0;   // Second value
   
//--- The first line with a header in a 16-character field and data in 20-character fields
//--- The header and data are pressed to the left edge.
//--- Data values of the second field are displayed with +/- signs
//--- The data values of the third field are pressed to the right edge with leading zeros added.
   header="Header1";
   value1=-10000000;
   value2=20000000;
   PrintFormat("%-16s%-+20ld%020lld",header,(int)value1,(long)value2);
   
//--- The second line with a header in a 16-character field and data in 20-character fields
//--- The header and data of the second field are pressed to the left edge.
//--- Data values of the second field are displayed with +/- signs
//--- The data values of the third field are pressed to the right edge with leading zeros added.
   header="Header2";
   value1=10000000;
   value2=-20000000;
   PrintFormat("%-16s%-+20ld%020lld",header,(int)value1,(long)value2);
   
   /* Sample output:
      Header1      -10000000           00000000000020000000
      Header2      +10000000           -0000000000020000000
   */
  }

(空格),如果输出值为带符号且为正,则输出值前面会有一个空格。具有此标志的字段的每个正数据值向右移动一个字符。这允许我们在同一列中不同行中存在正值和负值的情况下直观地对齐值,放置空格而不是负数数据的“-”字符。
void OnStart()
  {
//--- Declare the variables to be printed
   string   header="";  // Data header
   double   value1=0;   // First value
   double   value2=0;   // Second value
   
//--- The first line with a header in a 16-character field and data in 20-character fields
//--- The header and data of the second field are pressed to the left edge.
//--- Data values of the third field are pressed to the right edge
//--- For the second and third fields from the left, a space is added for positive values.
   header="Header1";
   value1=-10000000;
   value2=20000000;
   PrintFormat("%-16s%- 20ld% 20lld",header,(int)value1,(long)value2);
   
//--- The second line with a header in a 16-character field and data in 20-character fields
//--- The header and data of the second field are pressed to the left edge.
//--- Data values of the third field are pressed to the right edge
//--- For the second and third fields from the left, a space is added for positive values.
   header="Header2";
   value1=10000000;
   value2=-20000000;
   PrintFormat("%-16s%- 20ld% 20lld",header,(int)value1,(long)value2);
   
   /* Sample output:
      Header1      -10000000                       20000000
      Header2       10000000                      -20000000
   */
  }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值