Introduction to strategy.equity
The strategy.equity
function is a part of Pine Script’s strategy namespace, which includes functions and features for defining and managing trading strategies. This particular function provides the ability to access the equity curve of the strategy at any point in time during backtesting. It is a crucial tool for strategy developers who wish to analyze the performance of their strategies in real-time, make adjustments based on the equity curve, or implement advanced trading techniques such as dynamic position sizing.
Practical Example: Accessing and Utilizing Equity Data
To illustrate how strategy.equity
can be effectively used, let’s create a simple strategy that buys and sells based on a moving average crossover and utilizes the equity curve to adjust position sizes dynamically.
Step 1: Setting Up the Strategy
First, we initiate our strategy using strategy()
and specify basic parameters such as title, overlay, and initial capital.
//@version=5 strategy("Dynamic Equity Strategy", overlay=true, initial_capital=10000)
Step 2: Defining the Trading Logic
Next, we define our trading logic. We’ll use a simple moving average crossover system for this example. We calculate two moving averages and execute buy/sell orders when they cross.
shortMA = ta.sma(close, 9) longMA = ta.sma(close, 21) if ta.crossover(shortMA, longMA) strategy.entry("Buy", strategy.long) if ta.crossunder(shortMA, longMA) strategy.close("Buy")
Step 3: Utilizing strategy.equity
Now, let’s incorporate strategy.equity
to dynamically adjust our position size based on the current equity. We aim to increase our position size as our equity increases, demonstrating a positive performance of our strategy.
currentEquity = strategy.equity positionSize = math.max(1, currentEquity / 10000) // Adjust position size based on equity strategy.entry("Buy", strategy.long, qty=positionSize)
Step 4: Complete Script
Combining all the parts, here’s the complete Pine Script strategy:
//@version=5 strategy("Dynamic Equity Strategy", overlay=true, initial_capital=10000) shortMA = ta.sma(close, 9) longMA = ta.sma(close, 21) currentEquity = strategy.equity positionSize = math.max(1, currentEquity / 10000) // Dynamic position sizing if ta.crossover(shortMA, longMA) strategy.entry("Buy", strategy.long, qty=positionSize) if ta.crossunder(shortMA, longMA) strategy.close("Buy")
Walkthrough of the Code
- Strategy Initialization: We start by defining our strategy’s basic parameters such as its name, whether it should be displayed on the chart (
overlay
), and the initial capital. - Moving Averages Calculation: Two simple moving averages (SMAs) are calculated using closing prices over different periods (9 and 21 days). These serve as the basis for our trading signals.
- Dynamic Position Sizing: Before defining our entry criteria, we calculate the current equity using
strategy.equity
and adjust our position size based on this equity. Themath.max
function ensures that the position size never falls below 1. - Entry and Exit Conditions: We enter a long position (
strategy.entry
) when the short-term moving average crosses above the long-term moving average and exit (strategy.close
) when the opposite crossover occurs.
Key Features and Takeaways
- Function Useability:
strategy.equity
is invaluable for accessing real-time equity data, enabling strategies that adapt based on performance. - Syntax and Application: This function, combined with dynamic position sizing, illustrates how strategies can be made more intelligent and responsive to their success or failure.
- Practical Application: The example provided demonstrates a basic application of
strategy.equity
for dynamic position sizing, a technique that can significantly enhance strategy performance by adjusting to equity changes.