Param is not defined

I have following code that used to hardcode the param “pricecnt” as 20 and it worked well,

var R0 {i in 1..20} = value0[i]-value_z;
var R_1{i in 1..20} = (sum{j in insts} prices1[j,i]*pos[j])-value_z;
var avggain = sum{i in 1..20} R_1[i]*probs[i]-mids*2;

until I replaced the number into the param, when error occurs:

...
param pricecnt;
...
var R0 {i in 1..pricecnt} = value0[i]-value_z;
var R_1 {i in 1..pricecnt} = (sum{j in insts} prices1[j,i]*pos[j])-value_z;
var avggain = sum{i in 1..pricecnt} R_1[i]*probs[i]-mids*2;

the error message is:

> amplin, line 45 (offset 742):
	pricecnt is not defined
context:  var value0{i in  >>> 1..pricecnt} <<< =sum{j in insts} prices0[j,i]*pos[j];

And I did see

param pricecnt := 20;

in the dat file.

What is the right way to define such range? Thanks!

The error message says that parameter pricecnt is not defined where it appears in the definition of variable value0. This is most likely happening because the parameter’s definition,

param pricecnt;

occurs after the variable’s definition,

var value0 {i in  1..pricecnt} 
   = sum {j in insts} prices0[j,i] * pos[j];

You can fix the problem by moving the definition of pricecnt earlier in the model, so that it appears before the definition of value0. (In fact it should appear before any model statement that refers to pricecnt.)