Java and QTScript side by side
The same two strategies written in Java and in QTScript, so you can compare what each language asks of you and see that the platform runs them the same way.
Two strategies, each written twice. Both versions of each one are registered by the same POST /strategy, and the platform treats them the same way from then on. Choose a language in the tabs: the choice is kept for the other tabs of the page and for the next pages that have them.
RSI reversion
Buy when the 14-period RSI of a minute window is below a level, and sell when it is above another. The two levels are parameters, and the strategy accepts every pair quoted in USDT.
strategy "RSI reversion"
param rsiLow = 30 "Oversold"
param rsiHigh = 70 "Overbought"
instruments */usdt
setup:
rsi(14) window m1 {
if (actual < rsiLow) emitBuy(price);
if (actual > rsiHigh) emitSell(price);
} import com.wualabs.qtsurfer.engine.core.instrument.Instrument;
import com.wualabs.qtsurfer.engine.core.state.StateStore;
import com.wualabs.qtsurfer.engine.indicators.helpers.WindowTimeRTIndicator.WindowTime;
import com.wualabs.qtsurfer.engine.indicators.helpers.group.InstrumentGroupRTIndicator;
import com.wualabs.qtsurfer.engine.strategy.AbstractTickerStrategy;
import com.wualabs.qtsurfer.engine.strategy.AbstractWindowListener;
import com.wualabs.qtsurfer.engine.strategy.StrategyProperty;
public class RsiReversion extends AbstractTickerStrategy {
@StrategyProperty(name = "rsiLow", description = "Oversold", defaultValue = "30")
private int rsiLow;
@StrategyProperty(name = "rsiHigh", description = "Overbought", defaultValue = "70")
private int rsiHigh;
@Override
public boolean acceptInstrument(Instrument instrument) {
return instrument.quote().equalsIgnoreCase("usdt");
}
@Override
protected void setupIndicators(InstrumentGroupRTIndicator indicators) {
indicators
.addPrice()
.rsi(14)
.window("rsi14", WindowTime.m1, new RsiListener(this, indicators));
}
private class RsiListener extends AbstractWindowListener {
RsiListener(AbstractTickerStrategy s, InstrumentGroupRTIndicator ind) {
super(s, ind);
}
@Override
public void onChange(StateStore store, double prev, double actual) {
double price = indicators.getValue("price");
if (actual < rsiLow) emitBuy(price);
if (actual > rsiHigh) emitSell(price);
}
}
}What the QTScript file leaves out is the ceremony: the package imports, the class and its base class, the annotation of each parameter, the instrument filter as a method, and the listener class with its constructor. The decision itself, two comparisons and two signals, is the same two if lines in both; Java only reads the price first.
EMA cross with a position flag
Two moving averages read by name, and a flag in the store that the instrument’s windows share, so that the strategy buys once when the fast one crosses above the slow one and sells once when it crosses back.
strategy "EMA cross"
param fast = 9 "Fast EMA"
param slow = 21 "Slow EMA"
instruments */usdt
setup:
ema(fast)
ema(slow)
window price s5 {
double f = value("ema" + fast);
double s = value("ema" + slow);
if (f > s && !store.is("long")) {
store.set("long");
emitBuy(price);
}
if (f < s && store.is("long")) {
store.unset("long");
emitSell(price);
}
} import com.wualabs.qtsurfer.engine.core.instrument.Instrument;
import com.wualabs.qtsurfer.engine.core.state.StateStore;
import com.wualabs.qtsurfer.engine.indicators.helpers.WindowTimeRTIndicator.WindowTime;
import com.wualabs.qtsurfer.engine.indicators.helpers.group.InstrumentGroupRTIndicator;
import com.wualabs.qtsurfer.engine.strategy.AbstractTickerStrategy;
import com.wualabs.qtsurfer.engine.strategy.AbstractWindowListener;
import com.wualabs.qtsurfer.engine.strategy.StrategyProperty;
public class EmaCross extends AbstractTickerStrategy {
@StrategyProperty(name = "fast", description = "Fast EMA", defaultValue = "9")
private int fast;
@StrategyProperty(name = "slow", description = "Slow EMA", defaultValue = "21")
private int slow;
@Override
public boolean acceptInstrument(Instrument instrument) {
return instrument.quote().equalsIgnoreCase("usdt");
}
@Override
protected void setupIndicators(InstrumentGroupRTIndicator indicators) {
indicators
.addPrice()
.ema(fast)
.ema(slow)
.window("price", WindowTime.s5, new CrossListener(this, indicators));
}
private class CrossListener extends AbstractWindowListener {
CrossListener(AbstractTickerStrategy s, InstrumentGroupRTIndicator ind) {
super(s, ind);
}
@Override
public void onChange(StateStore store, double prev, double price) {
double f = indicators.getValue("ema" + fast);
double s = indicators.getValue("ema" + slow);
if (f > s && !store.is("long")) {
store.set("long");
emitBuy(price);
}
if (f < s && store.is("long")) {
store.unset("long");
emitSell(price);
}
}
}
}The flag is the same call in both, store.set("long"), because a body is plain Java in QTScript too. The names of the two indicators are built from the parameters: ema with the period registers ema9 and ema21 by default, and the body reads them with value("ema" + fast) in QTScript and indicators.getValue("ema" + fast) in Java.
Where each one fits
QTScript is the shorter route when a strategy is a handful of indicators and the rules for their windows. Java is the route when it needs every tick, other instruments, an indicator of your own or helper types. A QTScript file that outgrows its language is a Java class with the ceremony left out, so moving over is never a dead end. The strategies page has the whole picture, and the QTScript examples and the Java examples have more strategies to read.