Reference
Strategy language
A strategy is a few lines: which lines to calculate, when to enter, when to exit and where the stop is. The keywords are English, as in TradingView, and Russian keywords work too. Case does not matter.
How a strategy is built
One line, one thought. Lines that start with # or // are comments. First you define the lines (name = expression), then the entry and exit rules, then the risk.
Conditions are evaluated at the candle’s close, and the trade goes through at the next candle’s open, so the test cannot buy at a price that is already gone.
# Moving-average crossover with a stop fast = input(20, 5, 100, 5) slow = input(100, 50, 300, 10) f = ema(close, fast) s = ema(close, slow) plot f, s buy when crossover(f, s) sell when crossunder(f, s) stop 8%
Entries and exits
A rule is a keyword, then when (the Russian words for “when” and “if” work too) and a condition. Several lines of the same rule are joined with “or”.
buy when …enter long: buy at the next candle’s open
sell when …exit the long
short when …enter short; if shorts are off in the settings, it simply exits the long
cover when …exit the short
exit long when … · exit short when …the same as sell and cover
Stop, take-profit, trailing stop, time, size
Levels are set from the entry price, in percent or in ATR: the average candle range over 14 candles, taken on the signal candle.
stop 5% · stop 2 atra stop-loss from the entry price; if a candle opens beyond the stop, the exit is at the open
take 10% · take 3 atra take-profit; if one candle hits both the stop and the take-profit, the stop counts
trail 10% · trail 3 atra trailing stop behind the best price; it moves only in the direction of profit
hold 20exit at the open after 20 candles in the trade
breakeven 3%after a 3% gain, the stop moves to the entry price
size 50%the share of capital in a trade (100% by default, no leverage)
Slider parameters
input(default, min, max, step) turns a number into a slider. With two parameters, the lab builds a map of how the result changes at neighboring values.
n = input(200, 50, 300, 10) avg = sma(close, n) buy when close > avg sell when close < avg
Prices and indicators
Prices: open, high, low, close, volume (their Russian names work too). Lengths are whole numbers or parameters.
sma(x, n) · ema(x, n) · rma(x, n)simple, exponential and smoothed (Wilder’s) averages
rsi(x, n)the relative strength index, from 0 to 100
macd(x, 12, 26) · macd_signal(x, 12, 26, 9) · macd_hist(…)MACD, the signal line and the histogram
bb_upper(x, 20, 2) · bb_lower(x, 20, 2) · bb_mid(x, 20)Bollinger Bands
atr(14) · adx(14) · stoch(14)average true range, ADX trend strength, stochastic oscillator
highest(x, n) · lowest(x, n)the high and the low over n candles, including the current one
stdev(x, n) · roc(x, n) · change(x, n)standard deviation, change in percent and in points
abs · min · max · sqrt · log · roundmath
month · weekday · day · yearcalendar: month 1–12, day of the week 1–7 (Monday is 1)
bars_per_yearhow many candles a year this market has: about 252 for stocks, 365 for crypto
Conditions
Comparisons: >, <, >=, <=, ==, !=. Connectives: and, or, not (their Russian equivalents work too). A value from a few candles back goes in square brackets: close[1] is yesterday’s close.
crossover(a, b)a crossed b from below on this candle
crossunder(a, b)a crossed b from above
rising(x, n) · falling(x, n)x is above (below) all its values over the previous n candles
close > highest(high, 20)[1]a breakout above the high of the previous 20 candles: [1] leaves out the current one
How trades are executed
The signal comes at the close, and the trade goes through at the next candle’s open, with slippage against you. A fee is charged on every buy and every sell.
The stop and the take-profit are checked inside the candle. If the open is already beyond the stop, the exit is at the open price. If one candle hits both levels, the stop counts: the order of moves inside a candle is unknown.
One position, no leverage and no adding to it. The test starts once all indicators are calculated, and buy and hold starts on the same date.
What the model does not know
Taxes, dividends on individual stocks, interest on cash out of the market, the cost of shorting and leverage, the funding rate of crypto futures, lot sizes and order book liquidity. That is why a test result is an upper estimate, not an expectation.