Position Sizing Calculator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AktieAI

// This calculator will determine your position size per trade, profit, loss, risk/reward ratio and leverage if any.
// It will calculate your leverage if you are trading financial instruments e.g.  Mini Futures, Turbo Warrants etc. that have a financing level.

//@version=5

// Variables
float leverage = na
float stopLossProcent = na
float positionSize = na
float profit = na
float loss = na
float riskReward = na


// User inputs
indicator('Position Sizing Calculator', 'Position Sizing', overlay=true)
position = input.string(defval='Long', title='Position Type', options=['Long', 'Short'])
accountBalance = input.float(defval=100000, title='Account Balance', minval=1, step=0.01)
riskPerTrade = input.float(defval=3.0, title='Risk Per Trade %', minval=0.25, step=0.01) / 100
financingLevel = input.float(defval=0, title='Financing Level', minval=0, step=0.01)
entryPrice = input.float(defval=100.0, title='Entry Price', step=0.01)
targetPrice = input.float(defval=110.0, title='Target Price', step=0.01)
stopLossPrice = input.float(defval=95.0, title='Stop-Loss Price', step=0.01)
textColor = input.color(color.white, 'Text Color')
backgroundColor= input.color (#37383b, 'Background Color')
placement = input.string(position.top_right, 'Placement', options = [position.bottom_right, position.bottom_left, position.top_right, position.top_center, position.bottom_center])


// Calculations depending on Long/Short position
if position == 'Long'
	riskReward := (targetPrice - entryPrice) / (entryPrice - stopLossPrice)
    leverage := financingLevel > 0 ? 0.01 * entryPrice / (entryPrice - financingLevel) * 100 : 1
    stopLossProcent := 1 - stopLossPrice / entryPrice
else
	riskReward := (entryPrice - targetPrice) / (stopLossPrice - entryPrice)
    leverage := financingLevel > 0 ? 0.01 * entryPrice / (financingLevel - entryPrice) * 100 : 1
    stopLossProcent := stopLossPrice / entryPrice - 1

// Round to 2 decimals
riskReward := math.round(riskReward * 100) / 100

// Calculate Position Size
positionSize := math.round(accountBalance * riskPerTrade / (stopLossProcent * leverage), 2)

// Adjust Position Size
if positionSize > accountBalance
// if it exeeds account balance
    positionSize := accountBalance
else if positionSize < accountBalance * riskPerTrade
// if it is less than the risk per trade size
    positionSize := accountBalance * riskPerTrade

// Calculate Profit
if financingLevel == 0
// if there is no leverage
	profit := position == 'Long' ? positionSize * ((targetPrice - entryPrice) / (entryPrice)) : positionSize * ((entryPrice - targetPrice) / (entryPrice))
else
// if there is leverage
	profit := position == 'Long' ? positionSize * ((targetPrice - entryPrice) / (entryPrice - financingLevel)) : positionSize * ((entryPrice - targetPrice) / (financingLevel - entryPrice))

// Calculate Loss
if financingLevel == 0
// if there is no leverage
	// Using a different type of formula here, which is more compact
	loss := position == 'Long' ? positionSize - (positionSize * (stopLossPrice / entryPrice)) : positionSize * ((stopLossPrice / entryPrice) - 1)
else
// if there is leverage
	loss := position == 'Long' ? positionSize * ((entryPrice - stopLossPrice) / (entryPrice - financingLevel)) : positionSize * ((stopLossPrice - entryPrice) / (financingLevel - entryPrice))

//Table
table resultTable = table.new(placement, 1, 7, border_width=1)
if barstate.islast
    table.cell(resultTable, 0,0, ' ', text_color = textColor, bgcolor = na)
    table.cell(resultTable, 0,1, 'Acc. Balance: ' + str.tostring(accountBalance), text_color = textColor, bgcolor = backgroundColor)
    table.cell(resultTable, 0,2, 'Position Size: ' + str.tostring(positionSize), text_color = textColor, bgcolor = backgroundColor)
    table.cell(resultTable, 0,3, 'Profit: ' + str.tostring(math.round(profit, 2)), text_color = textColor, bgcolor = backgroundColor)
    table.cell(resultTable, 0,4, 'Loss: ' + str.tostring(math.round(loss, 2)), text_color = textColor, bgcolor = backgroundColor)
    table.cell(resultTable, 0,5, 'Leverage: 1:' + str.tostring(math.round(leverage)), text_color = textColor, bgcolor = backgroundColor)
    table.cell(resultTable, 0,6, 'R/R Ratio: ' + str.tostring(riskReward), text_color = textColor, bgcolor = backgroundColor)