# Syntax & Commands

{% hint style="info" %}
Strategies generated via the LLM-assisted builder must still conform to the syntax and execution model described below.
{% endhint %}

Below are **all the supported syntax, variables and functions** available when building trading strategies using the **WAKOS / memejob LUA Strategy Builder**.

The environment is a **sandboxed LUA DSL** designed for **safe, fast, deterministic backtesting and execution**. It is **not full LUA**.

Not all commands are required for every strategy; most strategies use a small subset of the primitives listed below.

### 1. Execution Model (Important)

Understanding the execution model is critical before writing strategies.

* The strategy script is executed **once per bar**
* Scripts are **stateless** by default (no memory across bars)
* The engine manages: Positions and Order execution
* Your script expresses **intent** via return values

#### Return Values

```lua
return 1   -- Buy / Long / Add (DCA)
return -1  -- Exit or Short (engine-defined)
return 0   -- Do nothing / Hold
```

### 2. Supported Lua Syntax

The strategy builder supports a **subset of LUA**.

#### ✅ Supported commands

* `local` variable declarations
* Numeric arithmetic: `+ - * /`
* Comparisons: `> < >= <= ==`
* Boolean logic: `and`, `or`
* Conditional blocks: `if / elseif / end`
* Single-return strategy flow

Example:

```lua
local e21 = ema(21)

if close < e21 then
    return 1
elseif close > e21 then
    return -1
end

return 0
```

#### ❌ Not Supported (by design)

* Loops (`for`, `while`)
* Functions
* Tables / arrays
* Persistent state across bars
* LUA standard libraries (`math.*`, `string.*`, etc.)

### 3. Core Market Data Variables

All market data variables are instances of **`ValueWithHistory`**.

They support:

* Direct access (`close`)
* Historical indexing (`close[1]`, `close[2]`)

#### Price Variables

| Variable | Description                   |
| -------- | ----------------------------- |
| `open`   | Opening price of current bar  |
| `high`   | Highest price of current bar  |
| `low`    | Lowest price of current bar   |
| `close`  | Closing price of current bar  |
| `volume` | Trading volume of current bar |

Example:

```lua
local current = close
local previous = close[1]

if close > close[1] then
    return 1
end
```

### 4. Derived Price Variables

Calculated prices derived from OHLC values.

| Variable | Formula                          | Description          |
| -------- | -------------------------------- | -------------------- |
| `hl2`    | (high + low) / 2                 | Mid price            |
| `hlc3`   | (high + low + close) / 3         | Typical price        |
| `hlcc4`  | (high + low + close + close) / 4 | Close-weighted price |

All support historical indexing.

Example:

```lua
if hlc3 > hlc3[1] then
    return 1
end
```

### 5. Bar Event Context (`ev`)

The global `ev` table provides context about the current bar event.

#### Fields

| Field         | Type   | Description                      |
| ------------- | ------ | -------------------------------- |
| `ev.kind`     | string | `Opened`, `Updated`, or `Closed` |
| `ev.bar_span` | number | Bar interval in minutes          |

Example:

```lua
if ev.kind == "Closed" then
    return 0
end
```

### 6. Moving Average Indicators (Module MA)

#### EMA – Exponential Moving Average

```lua
ema(periods[, window="1m"])
```

Parameters:

* `periods` (number, required)
* `window` (string | number, optional)

Examples:

```lua
local e3 = ema(3)
local e21 = ema(21, "15m")
local e55 = ema(55, 60)
```

Asset-specific EMA:

```lua
local btc_ema = ema("BTC", 14, "15m")
```

***

#### SMA – Simple Moving Average

```lua
sma(window[, resolution="1m"])
```

Examples:

```lua
local sma_15m = sma("15m")
local sma_1h = sma("1h", "5m")
```

***

#### WMA – Weighted Moving Average

```lua
wma(periods, window, weights)
```

Examples:

```lua
local wma_val = wma(14, "15m", 14)
```

***

#### MACD – Moving Average Convergence Divergence

```lua
macd([small_periods=12[, large_periods=26[, window="1m"]]])
```

Examples:

```lua
local m = macd()
local m_fast = macd(9, 21, "15m")
```

> Note: MACD always uses the **primary asset**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.memejob.fun/memejob/introducing-ai-agents/memejob-agent-setup-guide/creating-a-strategy/syntax-and-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
