AlphaChart

AlphaScript Java

AlphaScript in Java

Java mode reads one class: fields, methods, the constructor, and the Java a method body needs: typed variables, var, final, arrays, List and ArrayList, if, for, for-each, while, do, switch, casts, Math and System.out.println.

A whole indicator

// Java: fields keep their values; setup() runs once, onBar() on every bar.
// Docs: alphachart.dev/docs/alphascript/java
import alphachart.*;

public class MyIndicator extends Indicator {
    @Override
    public void setup() {
        indicator("My indicator", true);
    }

    @Override
    public void onBar() {
        int length = input.integer("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

Everything lives in one class (extends Indicator is welcome but not needed). Fields are set once, the constructor and setup() run once, and onBar() runs on every bar. import and package lines and annotations such as @Override are allowed and skipped.

Keeping values between bars

Fields keep their values from bar to bar.

public class UpBars extends Indicator {
    private int count = 0;                  // fields keep their values
    private final List<Double> closes = new ArrayList<>();

    public void onBar() {
        if (close > open) count++;
        closes.add(close);
        if (closes.size() > 20) closes.remove(0);
        plot(count, "Up bars");
    }
}

Arguments and options

Java has no named arguments: pass them in order. int is a reserved word in Java, so inputs also answer as input.integer and input.decimal:

plot(ta.ema(close, 21), "EMA", Color.ORANGE, 2);       // value, title, color, width
mark("Buy", "below", "triangleUp", Color.GREEN);         // text, position, shape, color
System.out.println("bar " + barIndex + ": " + close);

What is read

  • int, long, double, boolean, String, var, final, Color
  • Plot, Label, Line, Box: the handles plot(), label(), line() and box() give back
  • double[] a = new double[n], new double[]{1, 2}, List<Double> xs = new ArrayList<>() with add, get, set, size, remove
  • methods with types, the constructor, static and private
  • if, for, for (double x : xs), while, do, switch
  • (int) x, Math.*, Double.isNaN, Double.NaN, String.valueOf, Integer.parseInt
  • System.out.println, System.out.printf and String.format (with %b and %n)

What is not

  • more than one class, interfaces, inheritance of your own, generics of your own, lambdas, streams, exceptions

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

Names and numbers

Whole numbers divide as in Java: with two ints, 7 / 2 is 3. Color.RED and color.red are the same colour.

Example: moving average cross

import alphachart.*;

public class MaCross extends Indicator {
    public void setup() { indicator("MA cross", true); }

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

        Plot f = plot(fast, "Fast", Color.ORANGE, 2);
        Plot 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 on " + symbol.name);
        }
        if (ta.crossunder(fast, slow)) {
            mark("Death cross", "above", "triangleDown", Color.RED);
        }
    }
}

The same indicator in every language: examples.