millionchart
English

اندازهٔ پوزیشن و نتیجهٔ معامله‌ها

استراتژی‌ای که اندازهٔ ورود را از قاعدهٔ setup و ارزش تیک نماد می‌گیرد، و استراتژی‌ای که تعداد و سود معامله‌های خودش را می‌خواند.

یک استراتژی می‌تواند دو نوع سؤال از تستر بپرسد: یک ورود چقدر بزرگ است (قاعدهٔ اندازهٔ setup، و تبدیل پول بین ارز نماد و ارز حساب) و معامله‌هایش تا اینجا چطور بوده‌اند. داده ساختگی است، پس این برنامه‌ها فراخوانی‌ها را نشان می‌دهند، نه نتیجه‌ای که ارزش معامله داشته باشد.

سقف زیان پیش از ورود#

استراتژی‌ای که مقدار را از setup می‌پرسد، پولِ در معرض ریسک را به ارز حساب می‌گوید و ورودهایی را که بیش از یک سقف ریسک دارند کنار می‌گذارد.

Cuprel
// Check an entry against a loss limit before placing it. The setup's size rule gives the quantity, the symbol's tick value turns a stop distance into money,
// and sizing.to_account states that money in the account's currency (sizing.to_symbol goes the other way).
strategy "Loss limit before entry"
    edition = 1
    display = price

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

input max_loss: float = 1.5, name = "Largest loss accepted", min = 0.1
input length: int = 20, name = "Length", min = 2
let basis = tech.sma(close, length)
let stop_distance = 2 * tech.atr(length)
let qty = sizing.default_qty(close)
let at_risk = sizing.to_account(qty * symbol.ticks(stop_distance) * symbol.tick_value)
if tech.crosses_above(close, basis) and at_risk <= max_loss
    orders.enter("Long", side.long)
    orders.exit("Long exit", "Long", stop = symbol.snap(close - stop_distance))
plot(basis, name = "Basis", color = color.orange)
plot(sizing.to_symbol(max_loss), name = "Limit in the symbol's currency", display = none)

شمارش معامله‌ها#

یک استراتژی شکست کانال که تعداد معامله‌ها، سودها و پوزیشن خودش را می‌خواند و در یک جدول می‌نویسد.

Cuprel
// A strategy that reads its own results: the counts of closed, winning, losing and open trades, the largest winning trade found with a loop over trades.closed.profit,
// the profit of the open trade, the net profit and the size of the position. The tally is written into a table on the last bar.
strategy "Trade tally"
    edition = 1
    display = price

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

fn row(t: table, line: int, label: text, value: text)
    table.cell(t, 0, line, label, h_align = left)
    table.cell(t, 1, line, value)

input length: int = 24, name = "Channel length", min = 2
let upper = tech.highest(high, length)[1]
let lower = tech.lowest(low, length)[1]
if close > upper
    orders.enter("Long", side.long)
if close < lower
    orders.close("Long")
if bar.is_last
    mut largest = 0.0
    for i in 0 until trades.closed_count
        largest = math.max(largest, trades.closed.profit(i))
    let open_profit = if trades.open_count > 0 then trades.open.profit(0) else 0.0
    let t = table.new(top_right, 2, 5, border_color = color.gray, border_width = 1)
    row(t, 0, "Won / lost", f"{trades.won_count} / {trades.lost_count}")
    row(t, 1, "Largest win", f"{largest:.2f}")
    row(t, 2, "Open profit", f"{open_profit:.2f}")
    row(t, 3, "Net profit", f"{stats.net_profit:.2f}")
    row(t, 4, "Position", f"{position.size}")
plot(upper, name = "Upper", color = color.green)
plot(lower, name = "Lower", color = color.red)