AlphaScript C++
AlphaScript in C++
C++ mode reads what C mode reads, and the parts of C++ an indicator needs: auto, std::string, std::vector, range-based for, std::cout, ta::sma-style names and options as designated initializers.
A whole indicator
// C++: globals keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/cpp
void setup() {
indicator("My indicator", true);
}
void onBar() {
const int length = input.int("Length", 20, {.min = 1});
const double average = ta::sma(close, length);
plot(average, "Average", {.color = color::blue, .width = 2});
if (ta::crossover(close, average)) {
mark("Up", {.position = "below", .shape = "triangleUp", .color = color::green});
}
}How the file runs
As in C: globals once, setup() once if there is one, onBar() on every bar. #include and using namespace std; are allowed and skipped.
Keeping values between bars
Globals and static locals keep their values.
std::vector<double> closes; // a global keeps its value
void onBar() {
closes.push_back(close);
if (closes.size() > 20) closes.erase(closes.begin());
double sum = 0;
for (auto x : closes) sum += x;
plot(sum / closes.size(), "Mean of 20");
}Arguments and options
Options as designated initializers (C++20), or in order:
plot(ta::ema(close, 21), "EMA", {.color = color::orange, .width = 2});
mark("Buy", {.position = "below", .shape = "triangleUp", .color = color::green});
std::cout << "bar " << barIndex << ": " << close << std::endl;What is read
- everything C mode reads
auto [line, direction] = ta::supertrend(3, 10);(structured bindings),std::format("{:.2f}", x)auto,const auto&,std::string,std::vector<double>withpush_back,pop_back,size,back,front,v[i],erase(v.begin() + i),insert(v.end(), x)for (auto x : v)using namespace std;: thenmax,sqrt,cout,vector,stringwork withoutstd::ta::sma,color::red(same asta.sma,color.red)std::max,std::min,std::abs,std::sqrt,std::to_string,std::cout << …
What is not
- classes, templates, namespaces of your own, lambdas, references to change arguments
- pointers,
newanddelete: usestd::vector
Anything not read is pointed out with its line, in plain words.
Names and numbers
Whole-number division works as in C++: two ints divide to an int.
Example: moving average cross
void setup() { indicator("MA cross", true); }
void onBar() {
const double fast = ta::ema(close, input.int("Fast", 20, {.min = 1}));
const double slow = ta::ema(close, input.int("Slow", 50, {.min = 1}));
auto f = plot(fast, "Fast", {.color = color::orange, .width = 2});
auto s = plot(slow, "Slow", {.color = color::blue, .width = 2});
fill(f, s, {.color = color::alpha(color::green, 0.12), .colorDown = color::alpha(color::red, 0.12)});
if (ta::crossover(fast, slow)) {
mark("Golden cross", {.position = "below", .shape = "triangleUp", .color = color::green});
alert(std::format("Golden cross on {}", symbol.name));
}
if (ta::crossunder(fast, slow)) {
mark("Death cross", {.position = "above", .shape = "triangleDown", .color = color::red});
}
}The same indicator in every language: examples.