StrFormat limited to SelectedValue

Hello! I am trying to access static variables based on strings of DateNum and TimeNum. Unfortunately, using StrFormat only SelectedValue is used on the dates and the output is always the string of these at the last bar in Analysis. Are there any other options to do that?

The stoploss values come from a column in a CSV file that is processed earlier to generate Buy and Sell signals and I am trying to add stoploss values into static variables to refer back to them after the file was processed. Similar to vlookup in Excel.

entry_date_str = Name()+"_"+StrFormat( "%07.07g", DateNum() )+StrFormat( "%07.07g", TimeNum() );
stoploss = StaticVarGet(entry_date_str);

entry_date = StrFormat( "%07.07g", DateNum() )+StrFormat( "%07.07g", TimeNum() );

AddTextColumn(entry_date_str, "entry_date_str"); //Only last value is shown here instead of DateTime and TimeNum strings by bar

There isn't an array of strings in AFL. So you can not get an entire array of datenum (or any other array) stored as string just because you insert DateNum() as function argument there to StrFormat or WriteIf or any other AFL string function. If you insert array of numbers as argument to such functions then you will get element of array as string output. So you rather have to insert numbers.

BTW why using multiple times Strformat in single line?
Use it one single time per single line.

entry_date_str = StrFormat( "%s_%07.0f_%07.0f", Name(), SelectedValue(DateNum()), SelectedValue(TimeNum()));

or just using datetime instead of datenum and timenum

entry_date_str = StrFormat( "%s_%s", Name(), DateTimeToStr(SelectedValue(DateTime())));

Or using DateTimeFormat instead of DateTimeToStr.

entry_date_str = StrFormat( "%s_%s", Name(), DateTimeFormat("%y%m%d_%H%M%S", SelectedValue(DateTime())));

or like this without StrFormat

entry_date_str = Name() + DateTimeFormat("_%y%m%d_%H%M%S", SelectedValue(DateTime()));
1 Like

I don't know what you do exactly but why not reading from CSV and storing the data to matrix?
Besides entire matrix can be stored to static var also.
Then you may use LookUp function or what ever you wanna do and access that matrix data.

So for example

// NOTE: This code is incomplete and just a snippet/draft!

// some sample trigger
trigger = ParamTrigger( "Store file data to matrix", "CLICK HERE" );

if ( trigger ) // if triggered
    // read from file and populate matrix

    // .....
    // store matrix  to staticvar
    StaticVarSet( "...." , yadda_yadda_mat);

// call static variable 
static_mat = StaticVarGet( "...." );// stored matrix

if ( static_mat ) // if matrix filled
   // do some stuff with it

@trongart you are doing it wrong.

Instead of storing strings, store DateNums and TimeNums (or better yet DateTime()s) in static variables. If you need them for later display you can call StrFormat LATER when you need to use ANY bar value.

dt = StaticVarGet("PreviouslyStoredDateTime");
str = DateTimeToStr( dt[ bar_we_are_interested_in ] );

AddColumn( dt, "DateTimes", formatDateTime ); // formatDateTime allows AddColumn to display timestamps
1 Like