millionchart
English

قطعیت و سقف‌ها

یک برنامه روی یک داده همیشه همان خروجی را می‌دهد: seedها، تاریخچه‌ای که موتور نگه می‌دارد، شبکهٔ قیمت و بودجهٔ عملیات.

موتور قطعی است: برنامه‌ای که دو بار روی همان کندل‌ها اجرا شود همان خروجی را می‌دهد. این مثال‌ها بخش‌هایی از این وعده را نشان می‌دهند که یک برنامه با آن‌ها سروکار دارد: seed یک عدد تصادفی، تاریخچهٔ محدود، شبکهٔ قیمت و بودجهٔ عملیات.

عددهای تصادفی seed‌دار#

math.random با یک seed صریح در هر اجرا همان است.

Cuprel
// math.random is deterministic: the same seed and the same call site give the same numbers on every run and every machine.
// The seed is part of the call, so changing it changes the numbers and nothing else does.
indicator "Seeded noise"
    edition = 1
    display = pane

input seed: int = 7, name = "Seed", min = 0
let noise = math.random(-1, 1, seed = seed)
plot(noise, name = "Noise", style = histogram, color = color.gray)
plot(tech.sma(noise, 20), name = "Average of the noise", color = color.orange)

خواندن کندل‌های قبلی#

close[n] و پنجره‌ای درون تاریخچه‌ای که موتور نگه می‌دارد.

Cuprel
// Reading earlier bars: `close[n]` is the close n bars ago, and the engine keeps a bounded history for every series.
// This compares the close with the close 50 bars ago, 100 bars ago, and the highest close of the last 200.
indicator "Look back"
    edition = 1
    display = pane

let change_50 = close - close[50]
let change_100 = close - close[100]
let from_peak = close - tech.highest(close, 200)
plot(change_50, name = "Change over 50 bars", color = color.blue)
plot(change_100, name = "Change over 100 bars", color = color.purple)
plot(from_peak, name = "From the 200-bar high", color = color.red)

پنجره‌ای فراتر از تاریخچه#

طول ثابتی که از سقف تاریخچه بلندتر باشد، پیش از اجرا رد می‌شود.

Cuprel · خطا E-LIMIT-HISTORY-STATIC
// Mistake: a window longer than the history the engine keeps cannot be filled, so the run is refused before it starts (E-LIMIT-HISTORY-STATIC).
indicator "Window beyond the history"
    edition = 1
    display = price

plot(tech.sma(close, 100000), name = "Too long")

پنجره‌ای درون تاریخچه#

برنامهٔ اصلاح‌شده طولی درون سقف به کار می‌برد.

Cuprel
// The fix: a window inside the history cap. The numbers of the cap are on the determinism page.
indicator "Window inside the history"
    edition = 1
    display = price

plot(tech.sma(close, 200), name = "SMA 200")

حد ضرر روی شبکهٔ قیمت#

symbol.snap قیمت را به شبکهٔ تیک نماد گرد می‌کند.

Cuprel
// Prices live on a grid of ticks. symbol.snap rounds a price to the nearest tick, so a stop or a target is a price the market can have.
strategy "Stops on the price grid"
    edition = 1
    display = price

setup:
    capital = 10000
    size = sizing.lots(0.1)

let range = tech.atr(14)
let fast = tech.ema(close, 8)
let slow = tech.ema(close, 21)
if tech.crosses_above(fast, slow)
    orders.enter("Long", side.long)
    orders.exit(
        "Long exit",
        "Long",
        stop = symbol.snap(close - 2 * range),
        limit = symbol.snap(close + 3 * range),
    )
plot(fast, name = "Fast", color = color.green)
plot(slow, name = "Slow", color = color.red)

حلقه‌ای که هرگز تمام نمی‌شود#

بودجهٔ عملیات هر کندل اجرا را متوقف می‌کند.

Cuprel · خطا E-LIMIT-OPS
// Mistake: a loop that never ends. Every bar has a budget of operations, and a run has a cap; the engine stops the run and names the bar.
indicator "Endless loop"
    edition = 1
    display = pane

mut n = 0
while true
    n += 1
plot(n)