【问题标题】:For loop doesn’t accept array. How to work around for this Amibroker code?For 循环不接受数组。如何解决此 Amibroker 代码?
【发布时间】:2019-08-29 17:29:13
【问题描述】:

以下代码将计算事件发生时某些柱的高低范围百分比,并计算高低范围的平均值。

event_up = get_event_up();
event_down = get_event_low();   
num_events_in_range = get_num_events_in_range();

sum_pct_updown_dist = 0;
for (i=1;i<num_events_in_range; i++) //for cannot accept array type 
{
    event_up_high_i = ValueWhen(event_up, High, i);
    event_down_low_i = ValueWhen(event_down, Low, i);
    pct_updown_dist_i = (event_up_high_i-event_down_low_i)/event_up_high_i*100;
    sum_pct_updown_dist = sum_pct_updown_dist + pct_updown_dist_i;
}
avg_pct_updown_dist = sum_pct_updown_dist/num_events_in_range;

代码在 Amibroker 中不起作用,因为 (i=1;i&lt;num_events_in_range; i++) 的这一行违反了 Amibroker 语法。 For 循环不接受数组类型。 num_events_in_range 是一个数组。

如何修改此代码以解决此问题?

【问题讨论】:

    标签: amibroker


    【解决方案1】:

    for循环的语法是

    for ( init-expression ; cond-expression ; loop-expression )
    

    在您的代码中,i &lt; num_events_in_range 是一个无效条件,因为 i 是一个整数,num_events_in_range 是一个数组。

    代替num_events_in_range,使用num_events_in_range的长度

    【讨论】:

      【解决方案2】:

      试试这个

      event_up = get_event_up();
      event_down = get_event_low();   
      num_events_in_range = get_num_events_in_range();
      
      sum_pct_updown_dist = 0;
      for (var i=1;i<num_events_in_range.length; i++) //for cannot accept array type 
      {
          event_up_high_i = ValueWhen(event_up, High, i);
          event_down_low_i = ValueWhen(event_down, Low, i);
          pct_updown_dist_i = (event_up_high_i-event_down_low_i)/event_up_high_i*100;
          sum_pct_updown_dist = sum_pct_updown_dist + pct_updown_dist_i;
      }
      avg_pct_updown_dist = sum_pct_updown_dist/num_events_in_range;
      

      您需要更新计算平均值的最后一行。

      avg_pct_updown_dist = sum_pct_updown_dist/num_events_in_range;
      

      在这里,num_events_in_range 会产生错误。根据 for 循环的语法,您需要一个可以为真或假的表达式。正如@Shylajhaa 所解释的,不能在单个整数值和数组之间进行比较。

      【讨论】:

        【解决方案3】:

        这很简单,因为它归结为 for 循环的基本结构。 这是你的错误:

        代码在 Amibroker 中不起作用,因为 (i=1;i

        正如你的错误所暗示的,这是问题所在:

        for (i=1;i&lt;num_events_in_range; i++)

           where, ^^^^^^^^^^^^^^^^^^^ this is the problem
        

        在for循环的结构中:

        首先将索引 (i) 声明为:

        let i = [your starting point, usually 0 because is the first item in the array, in this case 1)

        然后你必须告诉 for 循环你什么时候要停止索引。为此,您将索引与数字类型值进行比较。当 i 等于或大于此数字时,它将按照指示停止。

        您的错误是因为:数组不是数字类型。

        因此无法将索引与数组进行比较。

        您必须将索引与数字进行比较,如果您希望 for 循环在给定数组持续的时间内一直运行,那么 .length 就发挥了作用。

        考虑它的最佳方式是 for 循环将迭代给定数组上的每个项目。

        为此,我们使用数组的.length 属性。它返回一个数值,表示有多少项目位于数组中。

        在这种情况下:

        num_events_in_range.length 给你一个数字,num_events_in_range 数组中的项目数。

        最后但并非最不重要的一点是,您必须告诉 for 循环其迭代的因素。虽然 i++(索引每次添加一个以遍历每个项目,无一例外)是此功能最常见的用途,但您可以将其更改为 i+2 或 i+3,索引将以 2s 和 3s 计数。

        例如: 2s,(假设起点为0):0,2,4,6,8,10,12,14,16,18...直到索引到达与它的比较数相同或更高的点。

        【讨论】:

          【解决方案4】:

          对于您的具体问题,您可以使用

          end_value = LastValue(num_events_in_range);
          for (i = 1; i < end_value; i++);
          

          反过来:

          语法

          for ( init-expression ; cond-expression ; loop-expression ) statement

          for 语句的执行过程如下:

          初始化表达式是求值的。这指定了循环的初始化。对 init-expression 的类型没有限制。

          对 cond 表达式求值。此表达式必须具有算术类型。它在每次迭代之前进行评估。三个结果是可能的: 如果 cond-expression 为真(非零),则执行语句;然后评估循环表达式(如果有)。循环表达式在每次迭代后进行评估。它的类型没有限制。副作用将按顺序执行。然后该过程再次开始计算 cond-expression。

          如果 cond-expression 为 false (0),则 for 语句的执行终止,控制权转移到程序中的下一条语句。

          为了获得有效的cond-expression,您应该比较相同类型的变量integer,例如在AFL中您应该注意BarCount

          这里快速回顾一下 AFL 中的数组

          数组只是值的列表(或行)。在某些书中,它可能被称为向量。示例中每行编号的值代表一个单独的数组。 Amibroker 在其数据库中为每个交易品种存储了 6 个数组。一种是开盘价,一种是低价,一种是高价,一种是收盘价,一种是成交量(见下面标有 1-5 的行),另一种是未平仓合约。这些在 AFL 中可以引用为 open、low、high、close、volume、openint 或 o、l、h、c、v、oi。

          AFL 中的所有数组索引都是从零开始的,即从柱 0(最旧的)开始计数柱。最新(最新)柱的索引为 (BarCount-1)。 10 元素数组的 BarCount = 10 和索引从 0 到 9 如下所示

          示例:

          myema[ 0 ] = Close[ 0 ];
          for( i = 1; i < BarCount; i++ )
          {
              myema[ i ] = 0.1 * Close[ i ] + 0.9 * myema[ i - 1 ];
          }
          
          for( i = 0; i < BarCount; i = i + 3 ) // increment by 3 every iteration
          

          来源:

          祝你好运;)

          【讨论】:

            【解决方案5】:

            官方 AmiBroker 论坛中已存在解决方案。 不需要BarCount 循环。 https://forum.amibroker.com/t/for-loop-doesnt-accept-array-how-to-work-around-for-this-code/14466

            令人惊讶的是@SCcagg5 使用了相同的变量名称end_value 加上通过 LastValue() 转换为数字。

            【讨论】:

              猜你喜欢
              • 2020-07-18
              • 2012-12-03
              • 1970-01-01
              • 1970-01-01
              • 2019-05-17
              • 2023-01-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多