One of several strategy settings is the default order size. TradingView provides us with different ways to compute that standard order size, and one sizes orders based on a fixed percentage of the strategy’s equity. What are the features of that approach and how to use it?

In this article:

Setting the default TradingView order size to an equity percentage

We configure a TradingView strategy either manually or programmatically, and with the latter the script is always added to the chart with the right settings. The strategy() function configures a strategy with code, and this function needs to be used by every strategy (Pine Script Language Tutorial, n.d.). In addition, we also always need to specify the strategy’s name with that function’s title argument (TradingView, n.d.).

Another setting that’s configurable with strategy() is the default order size, which we set with the default_qty_type and default_qty_value arguments. The first specifies which approach is used to calculate the default order size, while the second sets how large the order size is (TradingView, n.d.).

Note: That default order size sets the amount of contract, shares, units, lots, or whichever quantity an instrument trades in (TradingView, n.d.). In this article, we simply use the term “contracts” but know that this can also be shares, units, or lots (depending on the instrument class you’re trading).

We can set a strategy’s default order size in three ways, depending on how we combine the default_qty_type and default_qty_value arguments (TradingView, n.d.):

  • With default_qty_type set to strategy.fixed or not set at all, default_qty_value specifies how many contracts to trade with each order. We discuss this approach in setting the default order size in TradingView.
  • When default_qty_type is set to strategy.cash, then default_qty_value specifies how much of the strategy’s cash to invest in each order. This method is discussed in setting order size to a fixed amount of cash.
  • And when default_qty_type is set to strategy.percent_of_equity, then default_qty_value specifies the percentage of the strategy’s equity to invest in each order. This approach is discussed in this article.
Note: The strategy equity is the sum of the initial capital, net profit, and unrealised profit of open positions (see TradingView, n.d.). So a strategy that started trading with 10,000 and realised a profit of 1,345, while the open profit/loss is -450, has an equity of 10,895 (10,000 + 1,345 + -450).

To invest 10% of the strategy’s equity in each order, we use the strategy() function like this:

strategy(title="Example - investing 10% with each order", 
     default_qty_type=strategy.percent_of_equity, default_qty_value=10)

Now when our script generates an order, TradingView takes 10% of the current strategy equity and divides that with the instrument’s price. The result is then rounded down to get the number of contracts to trade.

So if a strategy has an equity of 24,372 and the instrument trades at 93.32, the order size will be 26 contracts (24,372 * 10% = 2437.20 and then 2437.20 / 93.32 = 26.12 ≈ 26).

When the strategy uses a different currency than the instrument it trades, TradingView converts the equity percentage to the instrument’s currency. In that case, 10% of the strategy’s equity is first converted into the instrument’s currency. The resulting cash amount is then divided by the instrument’s price and rounded down to get the number of contracts to trade.

Note: The default_qty_value argument can only be set to a literal value (like default_qty_value=10); it doesn’t accept numerical variables nor numerical input variables (such as default_qty_value=myInput). We can, however, manually change the default order size in the strategy settings window (see further down below).
Tip: TradingView uses the default order size that’s configured with strategy() unless we set the order size ourselves with the qty argument of the strategy.entry(), strategy.exit(), or strategy.order() functions. This means we can override the default order size depending on how we use those functions in the strategy’s code.

Now let’s discuss the features of order sizing based on a percentage of equity. Then we’ll look at the manual order settings and examine a full programming example.

TradingView features when sizing orders based on a strategy’s equity

So when we set the default_qty_type argument of the strategy() function to strategy.percent_of_equity, then orders are sized based on a portion of the strategy’s equity. This order sizing approach has several noteworthy features:

  • The default_qty_value specifies a single order’s size but not the position size. To limit the position size, we need to configure the strategy’s pyramiding settings and likely adjust the default_qty_value argument too. For instance, when we want to invest up to 15% equity in one position with not more than three entries (pyramiding=3), we set default_qty_type to strategy.percent_of_equity and default_qty_value to 5.
  • However, even with the strategy’s pyramiding settings in place, we can end up with a bigger position than intended. That happens when we submit several entry orders quickly after each other or submit entry orders with strategy.order(). To learn more, follow the links to those two articles.
  • Since TradingView always rounds the calculated position size down to the next integer, the order size is always a little bit less than the percentage of equity specified with default_qty_value.
  • When sizing orders with an equity percentage, TradingView currently only takes the instrument’s price (and not the lot size) into consideration. Say we invest 10% of our 15,000 equity in EUR/USD that trades at 1.1500. That gives an order size of 1304 lots ([10% * 15,000] / 1.1500 = 1304.35), even though each forex lot might represent 10,000 or 100,000 units (depending on the broker).
  • Similarly, the equity percentage invested with each order isn’t necessarily the capital amount that’s actually tied up in the position. Let’s assume the German DAX Index CFD has a margin requirement of 8% and quotes at 9,975. When we invest 15% of the strategy’s equity (150,250) per order, the calculated order size is 2 contracts ([15% * 150,250] / 9,975 = 2.26). However, with the margin requirement of 798 per contract (8% * 9,975) we don’t even get close to allocating 15% equity to this order. And so in practice, we can invest more than 100% of the strategy’s equity (as calculated by TradingView when using default_qty_type=strategy.percent_of_equity).
  • The default_qty_type argument defaults to strategy.fixed whereas default_qty_value has a fallback value of 1 (TradingView, n.d.). So without specifying these arguments, the default order size is 1 contract. When we set default_qty_type to strategy.percent_of_equity but do not set default_qty_value, orders are sized for 1% of the strategy’s equity. In that case, the strategy likely won’t take any trades even though there aren’t any errors in the code – just a misconfiguration of the strategy’s settings.

Sizing each order with a percentage of equity (that is, default_qty_type=strategy.percent_of_equity) is also known as percent of equity position sizing. With that approach, each position (or order, depending on the number of entries) is based on some percentage of the strategy’s equity.

An important disadvantage of that order sizing approach is that it doesn’t take risk into account. If an instrument is volatile, illiquid, or whether the stop-loss order is close or far away from the entry price, the position is always sized on a fixed percentage of equity For example, with strategy.percent_of_equity we can invest 10% of an account’s equity in a Crude Oil CFD and invest the same 10% in a S&P 500 tracker, even though the first position is much more riskier and volatile than the second.

Now let’s see how we can configure a strategy’s default order size by hand, and then turn our attention to a full programming example.

Setting a TradingView strategy’s default order size manually

Besides setting the default order size with code, we can configure it by hand too. That way we can configure multiple instances of the same strategy differently, which gives additional flexibility. It also allows us to quickly test something without having to edit the strategy’s code.

To set the default order size by hand, we first click on the gear icon ( ) displayed to the right of the strategy’s name on the chart:

Opening the strategy settings window in TradingView

This opens a window with different strategy settings. Here we select the ‘Properties’ tab and there we use the ‘Order Size’ setting to specify the default order size (with the numerical value) and how TradingView computes this order size (with the drop-down menu):

Changing the strategy settings in TradingView by hand
Note: How we configure the order size manually overrides the values set with the strategy() function. That way we can manually configure a strategy differently from how we’ve programmed its options.

The two ‘Order Size’ options correspond to the default_qty_type and default_qty_value arguments of the strategy() function as follows:

Comparing the manual strategy settings to TradingView's strategy() function

Now let’s jump into a programming example to better understand order sizing based on a fixed percentage of a strategy’s equity.

Example: sizing orders with a fixed equity percentage

With the basic example strategy below we open a long or short position when the trading day starts, and then close that position at a fixed time later that day. This simple logic allows us to focus on discussing how the strategy sizes orders. We also plot the strategy’s equity on the chart; that helps when recalculating how TradingView sizes orders with the strategy.percent_of_equity option. The image below gives a quick view of the strategy. After discussing the code, we’ll examine the script’s behaviour in depth.

Example chart of the trading strategy sizing orders based on an equity percentage
//@version=2
strategy(title="Example - strategy.percent_of_equity", 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=10, precision=2, initial_capital=100000)

// Input
exitTime = input(title="Exit Time (hours)", type=integer, defval=15)

// Plot equity
plot(series=strategy.equity, color=blue)

// Determine order conditions
marketOpen = (dayofmonth != dayofmonth[1])

enterLong  = marketOpen and (dayofmonth % 2 == 0)
enterShort = marketOpen and (dayofmonth % 2 != 0)

exitMarket = (hour == exitTime)

// Submit orders
if (enterLong)
    strategy.entry(id="Enter Long", long=true)

if (enterShort)
    strategy.entry(id="Enter Short", long=false)

if (exitMarket)
    strategy.close_all()

We begin with a comment saying @version=2. This specifies that the script should use the second version of Pine Script, and that version makes it possible to use if statements (Pine Script Language Tutorial, n.d.).

Then we use the strategy() function to specify several strategy properties. With the title argument we define the script’s name. Then we set default_qty_type to strategy.percent_of_equity so that our strategy sizes orders based on a fixed equity percentage, and with default_qty_value we set that percentage to 10 (TradingView, n.d.).

The strategy’s precision (precision) is set to 2. This makes our strategy plot its values with two decimals on the chart. Lastly, with initial_capital we set the strategy’s initial capital to 100,000.

Then we add an input option to the script:

exitTime = input(title="Exit Time (hours)", type=integer, defval=15)

The input() function adds a user-configurable input option to the script’s settings, and also returns the input’s current value (Pine Script Language Tutorial, n.d.). Here we assign that value to the exitTime variable. That makes it possible to refer to the input’s value later on in the script by using the variable.

Our input is named “Exit Time (hours)” with the title argument of the input() function. With the type argument set to integer, we create a numerical integer input that can only be set to whole numbers (Pine Script Language Tutorial, n.d.). The input’s standard value (defval) is 15. Based on how we plan to use the input, that value corresponds to 15:00 hour (or 3 p.m.).

Next we plot the strategy’s equity:

plot(series=strategy.equity, color=blue)

The plot() function displays the values of its series argument by default as a line on the chart (TradingView, n.d.). Here we set that argument to strategy.equity, a built-in variable that returns the strategy’s current equity (meaning, initial capital + net profit + open profit) (TradingView, n.d.). With the function’s color argument we have the plot appear in the blue standard TradingView colour.

Then we arrive at the strategy’s order conditions:

marketOpen = (dayofmonth != dayofmonth[1])

enterLong  = marketOpen and (dayofmonth % 2 == 0)
enterShort = marketOpen and (dayofmonth % 2 != 0)

exitMarket = (hour == exitTime)

We use these four true/false variables later on when we submit the strategy’s orders. The first, marketOpen, holds true when the current bar is the first of the trading day and false otherwise.

We evaluate that with dayofmonth. This built-in variable returns the bar’s day of month (a value that ranges from 1 to 31) (TradingView, n.d.). With that variable we check if the current bar’s day of month (dayofmonth) is unequal to (!=) that of the previous bar. To get that historical value, we use the history referencing operator ([]) with a value of 1. And so with dayofmonth[1] we get the previous bar’s day of month (see Pine Script Language Tutorial, n.d.). Because the current bar only has a different date than the previous bar on the first bar of the day, marketOpen only holds true on that first bar.

The next two variables (enterLong and enterShort) are assigned their value based on two true/false expressions that we combine with and. That logical operator returns true whenever the value on its left and the value on its right are both true too. When one or both values are false, then and returns false too (Pine Script Language Tutorial, n.d.).

This means that for the enterLong variable to hold true, the marketOpen variable has to return true and the bar’s day of month needs to be an even number. For this latter we check whether dayofmonth modulo (%) 2 equals (==) 0. That modulus operator (%) returns the remainder after division (Pine Script Language Tutorial, n.d.). So by using it here with a value of 2, it only returns 0 when dayofmonth is an even multiple of 2. That will be the case on the 2nd, 4th, 6th, 8th (and so on) day of the month.

The enterShort variable is assigned a variable in much the same way. Here we also require that marketOpen returns true, but now we want the day of month to be an odd date. Since dayofmonth % 2 returns the remainder of dividing the bar’s date with 2, and odd date happens when the modulus operator returns a value unequal to (!=) 0. After all, when the bar’s date doesn’t divide evenly into 2, it has to be an odd date (like 1, 3, 5, 7, and so on).

By evaluating if the day of month is an even date or not, we have a rudimentary way to alternate between long and short positions: on even days the strategy goes long and on odd dates it enters short positions. Other than this, the expressions with the modulus operator don’t add anything.

The last variable we make here is exitMarket. We assign this variable true or false based on an expression that compares the hour of the current bar (hour) with the exitTime input variable, which we gave a standard value of 15. This means whenever the bar’s time includes 15 (like 15:00 but 15:10, 15:33, and 15:59 are okay too), exitMarket holds true and false otherwise. With this we exit open positions during the afternoon.

Note: Both the dayofmonth and hour variables return values based on the exchange’s time zone (TradingView, n.d.). This means that, to have the script’s behaviour match the price bars we’re seeing on the chart, that chart needs to be set to exchange time zone. To do that, we right-click somewhere on the chart’s background and select ‘Properties’. Then we go to the ‘Timezone/Sessions’ tab:
Changing the time zone settings in TradingView

After creating the order condition variables, we use them to submit the orders:

if (enterLong)
    strategy.entry(id="Enter Long", long=true)

if (enterShort)
    strategy.entry(id="Enter Short", long=false)

if (exitMarket)
    strategy.close_all()

We use three if statements to submit our orders conditionally. The first evaluates enterLong, our variable that’s true when the current bar has an even date and is the first of the (calendar) day. When that’s the case, we open a long position with strategy.entry().

That function opens a position with a market order by default (TradingView, n.d.), and we set two of its arguments here. With id we specify the order identifier, a name that displays on the chart and in the ‘Strategy Tester’ window. And the long argument, when set to a value of true, makes strategy.entry() submit an enter long order while long=false makes it initiate a short position (TradingView, n.d.).

The second if statement is much like the first. Here we evaluate enterShort, which returns true when the current bar is the first of the day and has an odd date. When that happens, we call strategy.entry() to open a short position (long=false) that’s named “Enter Short”.

Since both strategy.entry() function calls didn’t set the qty argument to specify the order size, both orders uses the default order size that we’ve set with the default_qty_type and default_qty_value arguments of the strategy() function.

The third and last if statement checks the exitMarket variable, which holds true whenever the hour of the bar’s time equals the time we set with the “Exit Time (hours)” input option. When the bar’s hour indeed matches that value, we call strategy.close_all(). That function exits a strategy’s current position with a market order (Pine Script Language Tutorial, n.d.; TradingView, n.d.). And when there isn’t an open position, then strategy.close_all() doesn’t do anything.

This ends the discussion of our programming example; now let’s see how it behaves on the chart.

Analysing the example: orders based on a percentage of equity

To recap, the strategy() function used the following settings in the above example:

strategy(title="Example - strategy.percent_of_equity", 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=10, precision=2, initial_capital=100000)

By using strategy() like this, the default order size is 10% of the strategy’s equity. When we add the script to a Volvo stock chart, it trades like:

Example of the TradingView strategy added to a stock chart

Four trades open a position here: “Enter Long” for 114 shares, “Enter Short” for 113 shares, “Enter Short” for 112 shares, and “Enter Long” for 112 shares.

To analyse these trades properly, we need to know that TradingView, by default, only calculates strategies on each price bar’s close (Pine Script Language Tutorial, n.d.). Then when the strategy generates an order during such an on bar close calculation, that order fills at the next bar’s open at soonest.

That means when manually recalculating how TradingView computes order size (as we do in the table below), we have the use the closing price of the bar before the one which the order fills. After all, when the strategy calculates on that bar’s close and calculates the order size, that bar’s close was the current instrument price.

Analysing and recalculating the four trades of the chart above show the following:

Trade Closing price when order generates Percentage of equity Strategy equity when order generates Calculated position size
“Enter Long” for 114 shares 87.95 10% 100,559.89 (10% * 100,559.89) / 87.95 = 114.3376
“Enter Short” for 113 shares 88.40 10% 100,685.29 (10% * 100,685.29) / 88.40 = 113.8974
“Enter Short” for 112 shares 89.15 10% 100,560.99 (10% * 100,560.99) / 89.15 = 112.7998
“Enter Long” for 112 shares 89.50 10% 100,516.19 (10% * 100,516.19) / 89.50 = 112.3086

Here we see that TradingView computes the order size by multiplying the value of the default_qty_value argument (10% in our case) with the strategy’s equity at the time the order generates. That result is then divided with the instrument price from that moment.

The table also shows that the calculated order size is rounded down to the nearest full integer. Consequently, when we size orders with default_qty_type set to strategy.percent_of_equity, the strategy submits orders slightly smaller in size than the equity percentage set with default_qty_value. And this effect will be larger the higher the instrument’s price. This feature is, of course, a good thing since this way order sizes never become bigger than the equity percentage set with the strategy() function.

One important feature that we haven’t addressed in this article, but that nonetheless has a big impact on how TradingView sizes orders, is the strategy’s currency setting. We examine this more closely in sizing orders based on an equity percentage with currency conversion.

Summary

The strategy() function, which needs to be in the code of every strategy, specifies several strategy settings. Two of this function’s optional arguments are default_qty_type and default_qty_value. The first specifies how this default order size is computed; the second the order size itself. When we set default_qty_type to strategy.percent_of_equity, the default order size is based on a percentage of the strategy’s equity (which is the sum of initial capital, net profit, and open profit). The value of that percentage is what we set with default_qty_value. Then when an order generates, the strategy multiplies that percentage with its equity and then divides that result with the instrument’s price. The resulting order size is rounded down and used with the strategy.entry(), strategy.exit(), and strategy.order() functions when we haven’t set the qty argument of these functions ourselves. Besides using that argument, we can also change the order size that’s set with strategy() with the manual ‘Order Size’ option in the strategy’s settings. Note that, with default_qty_type set to strategy.percent_of_equity, TradingView performs currency conversion when the instrument’s currency differs from the one used by the strategy.

Learn more:


References

Pine Script Language Tutorial (n.d.). Retrieved on February 24, 2016, from https://docs.google.com/document/d/1sCfC873xJEMV7MGzt1L70JTStTE9kcG2q-LDuWWkBeY/

TradingView (n.d.). Script Language Reference Manual. Retrieved on April 22, 2016, from https://www.tradingview.com/study-script-reference/