For 循环不接受数组。如何解决此 Amibroker 代码?
For loop doesn’t accept array. How to work around for this Amibroker code?
以下代码将计算事件发生时某些柱的百分比高低范围,并计算高低范围的平均值。
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<num_events_in_range; i++)
的这一行违反了 Amibroker 语法。 For 循环不接受数组类型。 num_events_in_range
是一个数组。
如何修改此代码以解决此问题?
for循环的语法是
for ( init-expression ; cond-expression ; loop-expression )
在您的代码中,i < num_events_in_range
是一个无效条件,因为 i
是一个整数,而 num_events_in_range
是一个数组。
代替num_events_in_range
,使用num_events_in_range
的长度
试试这个
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 循环的语法,您需要一个可以为 true 或 false 的表达式。正如@Shylajhaa 所解释的,不能在单个整数值和数组之间进行比较。
这很简单,因为它归结为 for 循环的基本结构。
这是您的错误:
The code doesn't work in Amibroker because of this line for (i=1;i<
num_events_in_range;i++) which violates Amibroker syntax. For loop
does not accept array type. num_events_in_range is an array.
正如您的错误提示,这是有问题的行:
for (i=1;i<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
属性。它 returns 数组中有多少项的数值。
在这种情况下:
num_events_in_range.length
给你一个数字,即 num_events_in_range
数组中的项目数。
最后但同样重要的是,您必须告诉 for 循环它的迭代因子。尽管 i++(索引每次通过每个项目无一例外地向自身加一到 运行)是此功能的最常见用途,但您可以将其更改为 i+2 或 i+3 并且索引将计入2 秒和 3 秒。
例如:
2s,(假设起点为0):0,2,4,6,8,10,12,14,16,18...直到索引到达等于或高于其比较数的点。
对于您的具体问题,您可以使用
end_value = LastValue(num_events_in_range);
for (i = 1; i < end_value; i++);
相反:
Syntax
for ( init-expression ; cond-expression ; loop-expression ) statement
Execution of a for statement proceeds as follows:
The init-expression, is evaluated. This specifies the initialization for the loop. There is no restriction on the type of init-expression.
The cond-expression, is evaluated. This expression must have arithmetic type. It is evaluated before each iteration. Three results are possible:
If cond-expression is true (nonzero), statement is executed; then loop-expression, if any, is evaluated. The loop-expression is evaluated after each iteration. There is no restriction on its type. Side effects will execute in order. The process then begins again with the evaluation of cond-expression.
If cond-expression is false
(0), execution of the for statement terminates and control passes to the next statement in the program.
为了有一个有效的cond-expression
,你应该比较相同类型的变量integer
,例如,在AFL你应该照顾BarCount
这里是关于 AFL 中数组的快速回顾
An array is simply a list (or row) of values. In some books it may be referred to as a vector. Each numbered row of values in the example represents an individual array. Amibroker has stored in its database 6 arrays for each symbol. One for opening price, one for the low price, one for the high price, one for the closing price and one for volume (see the rows labelled 1-5 below) and one for open interest. These can be referenced in AFL as open, low, high, close, volume, openint or o, l, h, c, v, oi.
All array indices in AFL are zero-based, i.e. counting bars starting from bar 0 (oldest). The latest (newest) bar has an index of (BarCount-1). The 10-element array will have BarCount = 10 and indices going from 0 to 9 as shown below
示例:
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
来源:
祝你好运;)
AmiBroker 官方论坛中已有解决方案。
不需要 BarCount
循环。
https://forum.amibroker.com/t/for-loop-doesnt-accept-array-how-to-work-around-for-this-code/14466
令人惊讶的是@SCcagg5 使用相同的变量名 end_value
加上通过 LastValue() 转换为数字。
以下代码将计算事件发生时某些柱的百分比高低范围,并计算高低范围的平均值。
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<num_events_in_range; i++)
的这一行违反了 Amibroker 语法。 For 循环不接受数组类型。 num_events_in_range
是一个数组。
如何修改此代码以解决此问题?
for循环的语法是
for ( init-expression ; cond-expression ; loop-expression )
在您的代码中,i < num_events_in_range
是一个无效条件,因为 i
是一个整数,而 num_events_in_range
是一个数组。
代替num_events_in_range
,使用num_events_in_range
试试这个
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 循环的语法,您需要一个可以为 true 或 false 的表达式。正如@Shylajhaa 所解释的,不能在单个整数值和数组之间进行比较。
这很简单,因为它归结为 for 循环的基本结构。 这是您的错误:
The code doesn't work in Amibroker because of this line for (i=1;i< num_events_in_range;i++) which violates Amibroker syntax. For loop does not accept array type. num_events_in_range is an array.
正如您的错误提示,这是有问题的行:
for (i=1;i<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
属性。它 returns 数组中有多少项的数值。
在这种情况下:
num_events_in_range.length
给你一个数字,即 num_events_in_range
数组中的项目数。
最后但同样重要的是,您必须告诉 for 循环它的迭代因子。尽管 i++(索引每次通过每个项目无一例外地向自身加一到 运行)是此功能的最常见用途,但您可以将其更改为 i+2 或 i+3 并且索引将计入2 秒和 3 秒。
例如: 2s,(假设起点为0):0,2,4,6,8,10,12,14,16,18...直到索引到达等于或高于其比较数的点。
对于您的具体问题,您可以使用
end_value = LastValue(num_events_in_range);
for (i = 1; i < end_value; i++);
相反:
Syntax
for ( init-expression ; cond-expression ; loop-expression ) statement
Execution of a for statement proceeds as follows:
The init-expression, is evaluated. This specifies the initialization for the loop. There is no restriction on the type of init-expression.
The cond-expression, is evaluated. This expression must have arithmetic type. It is evaluated before each iteration. Three results are possible: If cond-expression is true (nonzero), statement is executed; then loop-expression, if any, is evaluated. The loop-expression is evaluated after each iteration. There is no restriction on its type. Side effects will execute in order. The process then begins again with the evaluation of cond-expression.
If cond-expression is
false
(0), execution of the for statement terminates and control passes to the next statement in the program.
为了有一个有效的cond-expression
,你应该比较相同类型的变量integer
,例如,在AFL你应该照顾BarCount
这里是关于 AFL 中数组的快速回顾
An array is simply a list (or row) of values. In some books it may be referred to as a vector. Each numbered row of values in the example represents an individual array. Amibroker has stored in its database 6 arrays for each symbol. One for opening price, one for the low price, one for the high price, one for the closing price and one for volume (see the rows labelled 1-5 below) and one for open interest. These can be referenced in AFL as open, low, high, close, volume, openint or o, l, h, c, v, oi.
All array indices in AFL are zero-based, i.e. counting bars starting from bar 0 (oldest). The latest (newest) bar has an index of (BarCount-1). The 10-element array will have BarCount = 10 and indices going from 0 to 9 as shown below
示例:
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
来源:
祝你好运;)
AmiBroker 官方论坛中已有解决方案。
不需要 BarCount
循环。
https://forum.amibroker.com/t/for-loop-doesnt-accept-array-how-to-work-around-for-this-code/14466
令人惊讶的是@SCcagg5 使用相同的变量名 end_value
加上通过 LastValue() 转换为数字。