In the beginning of my code I calculate pivot points: S3,S2,S1,PP,R1,R2,R3 (this order is from lowest to highest value).
In Explore, curPivot never returns a value (not 0, just empty space), even though no syntax errors are shown. I would like to have currently used pivot names preserved, but if it is impossible, I can name them for example P1,...P7.
What would You suggest, considering that I would like it to run smoothly in multi-parameter optimization?
//calculate all pivots: S3,S2,S1,PP,R1,R2,R3
curPivot = 0;
For( i = 1; i < BarCount; i++ )
{
for( n = 0; n <= 6; n++ )
{
curPivot[i] = VarGet(StrExtract("S3,S2,S1,PP,R1,R2,R3",n)+"[i]");
if (curPivot[i] > High[i])
{
//do stuff
break;
}
}
}
@Taisho this is not doing what you expect. Remove the +"[i]").
The "[i]" enclosed in double quotes is simply a string that gets appended to the extracted one.
So in your loop, you are actually assigning to curPivot[i] the {EMPTY} value returned from the nonexisting "S3[i]" ... "R3[i]"variables. Try this snippet:
varName1 = "R3" + "[i]";
VarSet(varName1, 9999); // assign any value to it
myVar1 = VarGet(varName1);
_TRACE(varName1 + " - Type: " + typeOf( myVar1) + " - Value: " + myVar1);
varName2 = "S3" + "[i]";
// try to get the value of a variable that DOES not exists - will return an {EMPTY} value
myVar2 = VarGet(varName2);
_TRACE(varName2 + " - Type: " + typeOf( myVar2) + " - Value: " + myVar2);
(Technically a dynamic var name including the square brackets is valid but it is very odd and I will avoid it; in the context of a string the [ ] are simply treated as other chars and are not operators).
To show how to properly use the created "pivots" vars (that I suppose are arrays) I made a longer snippet to show you how to use the dynamic vars to retrieve their values.
The first part is simply a "fake" initialization of the variables for the S/R "pivots". The truly relevant part start with the comment:
// split into multiple lines to make it more clear
// Tested on a daily interval
// Using previous month prices to calculate some fake S/R lines
mn = Month();
mc = mn != Ref( mn, -1 ); // detect new month bar
vw = ValueWhen( mc, Ref( Avg, -1 ) ); // use the avg price of previous bar
S3 = vw * 0.85;
S2 = vw * 0.90;
S1 = vw * 0.95;
PP = vw;
R1 = vw * 1.05;
R2 = vw * 1.10;
R3 = vw * 1.15;
curPivot = 0;
// use the retrived dynamic var also in exploration
Filter = 1;
SetSortColumns( -2 );
AddColumn( Avg, "Avg. Price" );
AddColumn( mn, "Month", 1 );
AddColumn( IIf( mc, 1, Null ), "Change", 1 );
AddColumn( vw, "Avg pr. eom" );
// split into multiple lines to make it more clear
srList = "S3,S2,S1,PP,R1,R2,R3";
for( n = 0; n < 7; n++ )
{
varName = StrExtract( srList, n );
// assign to curPivot the value of the variable named as varName
curPivot = VarGet( varName );
_TRACE( "Var name is \"" + varName + "\" - Type: " + typeOf( curPivot ) + " - Value of last element of CurPivot: " + LastValue( curPivot ) );
Plot( curPivot, varName, 32 + n, styleLine );
AddColumn( curPivot, varName, 1.2 );
}
Plot( C, "Close", colorDefault, styleCandle );
In each step of the "n" loop each S/R array will be assigned to the curPivot variable and, if needed, you can add an extra internal loop to go over all its individual elements using using curPivot[ j ] if "j" is the internal loop control variable.