Strings
11 functions
STRCAT(A, B)Concatenate two plain strings.
- A(string)
- First string. Must be a constant, not a per-bar series.
- B(string)
- Second string, appended to the first.
// fixed label — both parts are constants, so STRCAT is enough
TITLE := STRCAT('MA', '20');
DRAWTEXT(ISLASTBAR, MA(CLOSE, 20), TITLE), COLORYELLOW;
// if either side changes bar by bar you need VARCAT insteadSTRCAT6(A, B, C, D, E, F)Concatenate six plain strings.
- A(string)
- First of six constant strings.
- B(string)
- The remaining pieces follow in order, C through F.
// saves nesting STRCAT five deep
HEADER := STRCAT6('O', ' | ', 'H', ' | ', 'L', ' | C');
DRAWTEXT_FIX(ISLASTBAR, 0.02, 0.03, 0, HEADER), COLORGRAY;VARCAT(A, B)Concatenate two series of strings, bar by bar.
- A(string)
- First part. May vary bar by bar.
- B(string)
- Second part. May vary bar by bar.
// the value changes on every bar, so VARCAT, not STRCAT
DRAWTEXT(ISLASTBAR, HIGH, VARCAT('C=', VAR2STR(CLOSE, 2))), COLORWHITE;
// chain it for three pieces
PCT := VAR2STR((CLOSE - OPEN) / OPEN * 100, 2);
DRAWTEXT(ISLASTBAR, LOW, VARCAT(VARCAT('chg ', PCT), '%')), COLORWHITE;VARCAT6(A, B, C, D, E, F)Concatenate six series of strings.
- A(string)
- First of six parts, each of which may vary bar by bar.
- B(string)
- The remaining parts follow in order, C through F.
// a one-line OHLC readout without nesting VARCAT five times
LINE := VARCAT6('O ', VAR2STR(OPEN, 2),
' H ', VAR2STR(HIGH, 2),
' C ', VAR2STR(CLOSE, 2));
DRAWTEXT_FIX(ISLASTBAR, 0.02, 0.05, 0, LINE), COLORWHITE;VAR2STR(X, N)Format every value of X as a string with N decimals.
- X(series)
- The series to format. Each bar gets its own string.
- N(integer)
- Decimal places to show — not a bar count.
// per-bar formatting: each bar's own close
DRAWTEXT(ISLASTBAR, HIGH, VARCAT('C ', VAR2STR(CLOSE, 2)));
// 0 decimals for counts
BARS := BARSLAST(CROSS(MA5, MA20));
DRAWTEXT(ISLASTBAR, LOW, VARCAT('age ', VAR2STR(BARS, 0)));CON2STR(X, N)Format the final value of X as a string with N decimals.
- X(series)
- The series whose LAST value is taken — every bar gets the same string.
- N(integer)
- Decimal places to show.
// the difference from VAR2STR matters when the label is not
// pinned to the last bar: CON2STR repeats the latest value
LATEST := CON2STR(CLOSE, 2);
DRAWTEXT(MOD(BARPOS, 20) = 0, HIGH, LATEST), COLORGRAY;STRFORMAT(FORMAT, …)Positional formatting: '{0}-{1}' substitutes the arguments; '{0:0.00}' sets decimals.
- FORMAT(string)
- Template with {0}, {1}, … placeholders. Add :0.00 inside a placeholder to set decimals.
- …(series)
- The values substituted in, in order.
// clearer than three nested VARCATs
DRAWTEXT(ISLASTBAR, LOW,
STRFORMAT('O {0:0.00} C {1:0.00}', OPEN, CLOSE)), COLORWHITE;
// placeholders may repeat and appear out of order
DRAWTEXT(ISLASTBAR, HIGH,
STRFORMAT('{1} bars, chg {0:0.00}%',
(CLOSE - OPEN) / OPEN * 100,
BARSLASTCOUNT(CLOSE > OPEN)));STRSPACE(A)Append a space to a string.
- A(string)
- The string to pad on the right.
// nudge a label off the candle it sits on, without
// resorting to pixel offsets like XMOVE
DRAWTEXT(ISLASTBAR, HIGH, STRSPACE(STRSPACE('top'))), ALIGN0;FINDSTR(A, B)1 if B occurs inside A, otherwise 0.
- A(string)
- The string to search in.
- B(string)
- The substring to look for. Matches anywhere, not just at the start.
// route the script by instrument name
ISBANK: FINDSTR(STKNAME, 'BANK');
// different parameters for different instruments
LEN := IF(FINDSTR(CODE, 'BTC'), 30, 20);
MA_ADAPTED: MA(CLOSE, LEN), COLORYELLOW;STRCMP(A, B)0 if the strings are equal, non-zero otherwise.
- A(string)
- First string.
- B(string)
- Second string.
// note the polarity: 0 means EQUAL, so test for = 0
EXACT := STRCMP(CODE, 'BTCUSDT') = 0;
// FINDSTR is the one that returns 1 on a match
LOOSE := FINDSTR(CODE, 'BTC');STRLEN(A)Length of the string.
- A(string)
- The string to measure.
// pick a font size that keeps a long label inside the pane
LABEL := VARCAT(STKNAME, VAR2STR(CLOSE, 2));
DRAWTEXT(ISLASTBAR AND STRLEN(LABEL) > 12, HIGH, LABEL), FONTSIZE10;
DRAWTEXT(ISLASTBAR AND STRLEN(LABEL) <= 12, HIGH, LABEL), FONTSIZE14;