AlphaChart

AlphaScript C

AlphaScript in C

C mode reads C’s core: typed variables, functions, arrays, if, for, while, do, switch, ++ and --, compound assignments, casts, #define constants and printf.

A whole indicator

// C: globals keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/c
#include <alphachart.h>

void setup(void) {
    indicator("My indicator", true);
}

void onBar(void) {
    int length = input.int("Length", 20, 1);
    double average = ta.sma(close, length);

    plot(average, "Average", color.blue, 2);

    if (ta.crossover(close, average)) {
        mark("Up", "below", "triangleUp", color.green);
    }
}

How the file runs

Globals are set once, setup() runs once if there is one, and onBar() runs on every bar. onBar() is required. #include lines are allowed and skipped: the library is always there.

Keeping values between bars

Globals and static locals keep their values from bar to bar.

int upBars = 0;                 /* a global keeps its value */

void onBar(void) {
    static double highest = 0;  /* so does a static local */
    if (close > open) upBars++;
    highest = fmax(highest, close);
    plot(upBars, "Up bars");
}

Arguments and options

C has no named arguments: pass them in order, or as designated initializers:

plot(ta.ema(close, 21), "EMA", color.orange, 2);        /* value, title, color, width */
mark("Buy", "below", "triangleUp", color.green);           /* text, position, shape, color */
plot(ta.ema(close, 50), "Slow", (PlotOptions){ .color = color.blue, .width = 2 });
printf("bar %d: %.2f\n", barIndex, close);

What is read

  • int, long, float, double, bool, char* (text), color
  • plot_t, label_t, line_t, box_t: the handles plot(), label(), line() and box() give back
  • double v[10], double v[] = {1, 2, 3}
  • functions with types, static locals, const
  • if, for, while, do, switch (with fall-through), break, continue
  • (int) x, (double) x
  • printf, puts, fabs, fmax, fmin, sqrt, pow, floor, ceil, isnan, NAN, M_PI

What is not

  • pointers (apart from char* for text), struct, union, typedef, malloc
  • files, the standard library outside the names above

Anything not read is pointed out with its line, in plain words.

Names and numbers

Whole numbers divide as in C: with two ints, 7 / 2 is 3; (double) 7 / 2 is 3.5. Assigning to an int drops the decimals. Variables declared without a value start at 0. fmax and fmin skip a NAN, as in C (math.max gives na instead).

Example: moving average cross

#include <alphachart.h>

void setup(void) {
    indicator("MA cross", true);
}

void onBar(void) {
    double fast = ta.ema(close, input.int("Fast", 20, 1));
    double slow = ta.ema(close, input.int("Slow", 50, 1));

    plot_t f = plot(fast, "Fast", color.orange, 2);
    plot_t s = plot(slow, "Slow", color.blue, 2);
    fill(f, s, color.alpha(color.green, 0.12), "Cloud", color.alpha(color.red, 0.12));

    if (ta.crossover(fast, slow)) {
        mark("Golden cross", "below", "triangleUp", color.green);
        alert("Golden cross");
    }
    if (ta.crossunder(fast, slow)) {
        mark("Death cross", "above", "triangleDown", color.red);
    }
}

The same indicator in every language: examples.