PROTECTED SOURCE SCRIPT

Position Size Calculator CFD Forex Futures

19
//version=5
indicator("AUTO Risk & Position Size Calculator – Forex + Futures", overlay=true)

// === INPUTS ===
accountBalance = input.float(100000, "Account Balance")
riskPercent = input.float(1.0, "Risk % per Trade", step=0.1)

// === TRADE TYPE ===
// Forex/CFD = decimaler tilladt
// Futures = hele kontrakter
tradeType = input.string("Forex / CFD", "Market Type", options=["Forex / CFD", "Futures"])

// === ENTRY & STOP (MANUAL) ===
entryPrice = input.float(0.0, "Entry Price")
stopPrice = input.float(0.0, "Stop Loss Price")

// === SYMBOL DATA (AUTO FROM TRADINGVIEW) ===
tickSize = syminfo.mintick
tickValue = syminfo.pointvalue * syminfo.mintick

// === CALCULATIONS ===
riskAmount = accountBalance * (riskPercent / 100)
stopDistance = math.abs(entryPrice - stopPrice)

// ticks mellem entry & stop
ticks = stopDistance / tickSize

// risiko pr. 1 unit / lot / kontrakt
riskPerUnit = ticks * tickValue

// rå position size
rawPositionSize = riskPerUnit > 0 ? riskAmount / riskPerUnit : na

// === FINAL POSITION SIZE ===
finalPositionSize = tradeType == "Futures" ? math.floor(rawPositionSize) : rawPositionSize

// === DISPLAY ===
var table t = table.new(position.top_right, 2, 10, border_width=1)

table.cell(t, 0, 0, "Symbol")
table.cell(t, 1, 0, syminfo.ticker)

table.cell(t, 0, 1, "Market Type")
table.cell(t, 1, 1, tradeType)

table.cell(t, 0, 2, "Account Balance")
table.cell(t, 1, 2, str.tostring(accountBalance))

table.cell(t, 0, 3, "Risk %")
table.cell(t, 1, 3, str.tostring(riskPercent) + "%")

table.cell(t, 0, 4, "Risk Amount ($)")
table.cell(t, 1, 4, str.tostring(riskAmount))

table.cell(t, 0, 5, "Entry Price")
table.cell(t, 1, 5, str.tostring(entryPrice))

table.cell(t, 0, 6, "Stop Loss Price")
table.cell(t, 1, 6, str.tostring(stopPrice))

table.cell(t, 0, 7, "Risk per 1 Unit ($)")
table.cell(t, 1, 7, str.tostring(riskPerUnit))

table.cell(t, 0, 8, "Raw Position Size")
table.cell(t, 1, 8, str.tostring(rawPositionSize, "#.##"))

table.cell(t, 0, 9, "FINAL SIZE TO TRADE")
table.cell(t, 1, 9, str.tostring(finalPositionSize, "#.##"))

免責聲明

這些資訊和出版物並非旨在提供,也不構成TradingView提供或認可的任何形式的財務、投資、交易或其他類型的建議或推薦。請閱讀使用條款以了解更多資訊。