millionchart
English

نوع‌ها در حالت سایه

رکوردها و enumها: اعلام، ساخته و خوانده می‌شوند. بررسی‌کنندهٔ نوع، استفادهٔ نادرست از آن‌ها را گزارش می‌کند و کار برنامه را تغییر نمی‌دهد.

بررسی‌کنندهٔ نوع در edition 1 در حالت سایه کار می‌کند: آنچه را پیدا می‌کند گزارش می‌دهد، و اجرای برنامه با آن و بدون آن یکی است. این مثال‌ها یک رکورد و یک فهرست نام‌های ثابت (enum) اعلام می‌کنند.

رکوردها و enumها#

یک نوع رکورد و یک enum که اعلام، ساخته و خوانده می‌شوند.

Cuprel
// Types in shadow mode: a record and an enum are declared, constructed and read, and the checker reports their misuse as findings while the run is unchanged.
indicator "Records and enums"
    edition = 1
    display = pane

enum Trend
    up
    down
    flat

type Reading
    average: float = 0
    trend: Trend = Trend.flat

input length: int = 20, name = "Length", min = 2
let average = tech.sma(close, length)
let reading = Reading(average = average, trend = if close > average then Trend.up else Trend.down)
plot(reading.average, name = "Average")
plot(if reading.trend == Trend.up then 1 else -1, name = "Direction", style = histogram)

ناحیه با یک رکورد#

رکوردی که در یک state نگه داشته می‌شود، با مقداری تازه جایگزین می‌شود، و متدی که روی آن نوع تعریف شده است.

Cuprel
// A record kept in a state, with a method of its own. Zone holds a price band and the number of bars that closed inside it.
// A new band is made every `length` bars from the highest high and lowest low; `zone.touch(close)` is a call of the method defined on the type.
indicator "Zone with a record"
    edition = 1
    display = price

type Zone
    top: float = na
    bottom: float = na
    inside: int = 0

fn Zone.touch(self: Zone, price: float)
    if price <= self.top and price >= self.bottom
        self.inside += 1

input length: int = 30, name = "Length", min = 5
let top = tech.highest(high, length)
let bottom = tech.lowest(low, length)
state zone: Zone = Zone()
if bar.index % length == 0
    zone = Zone(top = top, bottom = bottom)
zone.touch(close)
plot(zone.top, name = "Top", color = color.red)
plot(zone.bottom, name = "Bottom", color = color.green)
plot(zone.inside, name = "Closes inside", display = none)