Trendoscope® | Position Sizing Strategies

Position sizing is one of the most important aspects in risk management for traders. Proper position sizing helps manage the risk effectively by maximizing profits and limiting the losses. In this publication, we will explore popular position sizing strategies and how to implement them in pinescript strategies

 Importance of Position Sizing in Trading

Let's take an example to demonstrate the importance of position sizing. You have a very good strategy that gives you win on 70% of the times with risk reward of 1:1. If you start trading with this strategy with all your funds tied into a single trade, you have the risk of losing most of your fund in the first few trades and even with 70% win rate at later point of time, you may not be able to recoup the losses. In such scenarios, intelligent position sizing based on the events will help minimize the loss. In this tutorial, let us discuss some of those methods along with appropriate scenarios where that can be used.

Position Sizing Strategies Available in Tradingview Strategy Implementation

Fixed dollar amount position sizing

In this method, trader allocate a fixed value of X per trade. Though this method is simple, there are few drawbacks

  • Does not account for varying equity based on the trade outcomes
  • Does not account for varying risk based on the volatility of the instrument

Fixed Percentage of Equity

In this method, percent of equity is used as position size for every trade. This method is also simple and slightly better than the Fixed dollar amount position sizing. However, there is still a risk of not accounting for volatility of the instrument for position sizing.

In tradingview strategies, you can find the position sizing settings in the properties section.

snapshot

In both cases, Pinescript code for the entry does not need to specify quantity explicitly, as they will be taken care by the framework.

if(longEntry)
    strategy.entry('long', strategy.long)
if(shortEntry)
    strategy.entry('short', strategy.short)

Advanced Position Sizing Strategies

There are not directly supported in Tradingview/Pinescript - however, they can be programmed.

Fixed Fractional Method

The Fixed Fractional Method is similar to the fixed percentage of equity method/fixed dollar amount positioning method, but it takes into account the amount of risk on each trade and calculate the position size on that basis. This method calculates position size based on the trader’s risk tolerance, factoring in stop-loss levels and account equity. Due to this, the trader can use any instrument and any timeframe with any volatility with fixed risk position. This means, the quantity of overall trade may vary, but the risk will remain constant.

Example.

Let's say you have 1000 USD with you and you want to trade BTCUSD with entry price of 100000 and stop price of 80000 and target of 120000. You want to risk only 5% of your capital for this trade.

Calculation will be done as follows.

Risk per trade = 5% of 1000 = 50 USD
Risk per quantity= (entry price - stop price) = 20000

So, the quantity to be used for this trade is calculated by

RiskQty = Risk Amount / Risk Per Quantity = 50 / 20000 = 0.0025 BTC

To implement the similar logic in Pinescript strategy by using the strategy order quantity as risk, we can use the following code

riskAmount = strategy.default_entry_qty(entryPrice)*entryPrice
riskPerQty = math.abs(entryPrice-stopPrice)
riskQty = riskAmount/riskPerQty

With this, entry and exit conditions can be updated to as follows

if(longEntry)
    strategy.entry('long', strategy.long, riskQty, stop=entryPrice)
    strategy.exit('ExitLong', 'long', stop=stopPrice, limit=targetPrice)
if(shortEntry)
    strategy.entry('short', strategy.short, riskQty, stop=entryPrice)
    strategy.exit('ExitShort', 'short', stop=stopPrice, limit=targetPrice)

Kelly Criterion Method

The Kelly Criterion is a mathematical formula used to determine the optimal position size that maximizes the long-term growth of capital, considering both the probability of winning and the payoff ratio (risk-reward). It’s a more sophisticated method that balances risk and reward in an optimal way.

Kelly Criterion method needs a consistent data on the expected win ratio. As and when the win ratio changes, the position sizing will adjust automatically.

Formula is as follows

f = W - L/R

  • f: Fraction of your capital to bet.
  • W : Win Ratio
  • L : Loss Ratio (1-W)
  • R : Risk Reward for the trade

Let's say, you have a strategy that provides 60% win ratio with risk reward of 1.5, then the calculation of position size in terms of percent will be as follows

f = 0.6–0.4/1.5 = 0.33

Pinescript equivalent of this calculation will be

riskReward = 2
factor = 0.1
winPercent = strategy.wintrades/(strategy.wintrades+strategy.losstrades)
kkPercent = winPercent - (1-winPercent)/riskReward
tradeAmount = strategy.equity * kkPercent * factor
tradeQty = tradeAmount/entryPrice

High Risk Position Sizing Strategies

These strategies are considered very high risk and high reward. These are also the strategies that need higher win ratio in order to work effectively.

Martingale Strategy

The Martingale method is a progressive betting strategy where the position size is doubled after every loss. The goal is to recover all previous losses with a single win. The basic idea is that after a loss, you double the size of the next trade to make back the lost money (and make a profit equal to the original bet size).

How it Works:

  • If you lose a trade, you increase your position size on the next trade.
  • You keep doubling the position size until you win.
  • Once you win, you return to the original position size and start the process again.

To implement martingale in Pine strategy, we would need to calculate the last consecutive losses before placing the trade. It can be done via following code.

var consecutiveLosses = 0
if(ta.change(strategy.closedtrades) > 0)
    lastProfit = strategy.closedtrades.profit(strategy.closedtrades-1)
    consecutiveLosses := lastProfit > 0? 0 : consecutiveLosses + 1

Quantity can be calculated using the number of consecutive losses

qtyMultiplier = math.pow(2, consecutiveLosses)
baseQty = 1
tradeQty = baseQty * qtyMultiplier

Paroli System (also known as the Reverse Martingale)

The Paroli System is similar to the Anti-Martingale strategy but with more defined limits on how much you increase your position after each win. It's a progressive betting system where you increase your position after a win, but once you've won a set number of times, you reset to the original bet size.

How it Works:

  • Start with an initial bet.
  • After each win, increase your bet by a predetermined amount (often doubling it).
  • After a set number of wins (e.g., 3 wins in a row), reset to the original position size.

To implement inverse martingale or Paroli system through pinescript, we need to first calculate consecutive wins.

var consecutiveWins = 0
var maxLimit = 3
if(ta.change(strategy.closedtrades) > 0)
    lastProfit = strategy.closedtrades.profit(strategy.closedtrades-1)
    consecutiveWins := lastProfit > 0?  consecutiveWins + 1 : 0
   if(consecutiveWins >= maxLimit)
       consecutiveWins := 0

The quantity is then calculated using a similar formula as that of Martingale, but using consecutiveWins

qtyMultiplier = math.pow(2, consecutiveWins)
baseQty = 1
tradeQty = baseQty * qtyMultiplier

D'Alembert Strategy

The D'Alembert strategy is a more conservative progression method than Martingale. You increase your bet by one unit after a loss and decrease it by one unit after a win. This is a slow, incremental approach compared to the rapid growth of the Martingale system.

How it Works:

  • Start with a base bet (e.g., $1).
  • After each loss, increase your bet by 1 unit.
  • After each win, decrease your bet by 1 unit (but never go below the base bet).

In order to find the position size on pinescript strategy, we can use following code

// Initial position
initialposition = 1.0
var position = initialposition
// Step to increase or decrease position
step = 2
if(ta.change(strategy.closedtrades) > 0)
    lastProfit = strategy.closedtrades.profit(strategy.closedtrades-1)
    position := lastProfit > 0 ? math.max(initialposition, position-step) : position+step

Conclusion

Position sizing is a crucial part of trading strategy that directly impacts your ability to manage risk and achieve long-term profitability. By selecting the appropriate position sizing method, traders can ensure they are taking on an acceptable level of risk while maximizing their potential rewards. The key to success lies in understanding each strategy, testing it, and applying it consistently to align with your risk tolerance and trading objectives.
 
 
By using this website you agree to our Cookie Policy.

Cookie Settings

We use cookies to improve user experience. Choose what cookie categories you allow us to use. You can read more about our Cookie Policy by clicking on Cookie Policy below.

These cookies enable strictly necessary cookies for security, language support and verification of identity. These cookies can’t be disabled.

These cookies collect data to remember choices users make to improve and give a better user experience. Disabling can cause some parts of the site to not work properly.

These cookies help us to understand how visitors interact with our website, help us measure and analyze traffic to improve our service.

These cookies help us to better deliver marketing content and customized ads.