Tracking Maximum Equity in Trading History and Recent Trades

01 Jun 2016
0 comment
0 attachment

This code snippet demonstrates how to track the highest equity reached over all trades and within a specified number of recent trades using the ProBuilder language. Equity is calculated as the sum of the initial capital and the strategy’s profit. The code is divided into two parts: one for tracking the highest equity over all trades and another for tracking the highest equity over the last 100 trades.


// Highest Equity in ALL trades
once capital = 10000
once maxcapital = capital
equity = capital + strategyprofit
maxcapital = max(maxcapital,equity)

// Conditions based on equity comparison
IF equity < maxcapital then
    condition1 = x
Elsif equity > maxcapital then
    condition2 = y
Endif

// Highest Equity in the last 100 trades
once capital = 10000
once maxcapital = capital
once equity = capital
once Trades = 100

IF StrategyProfit <> StrategyProfit[1] THEN
    TradeCount = lastset($myEquity) + 1
    equity = capital + strategyprofit
    $myEquity[TradeCount] = equity
    N = 1
    IF TradeCount >= Trades THEN
        N = TradeCount - (Trades - 1)
    ENDIF
    maxcapital = capital
    FOR i = TradeCount downto N
        maxcapital = max(maxcapital,$myEquity[i])
    NEXT
ENDIF

// Conditions based on equity comparison
IF equity < maxcapital then
    condition1 = x
Elsif equity > maxcapital then
    condition2 = y
Endif

Explanation of the code:

  • Initialization: The initial capital is set to 10000, and the maximum capital (maxcapital) is initially set to the same value. This setup is done using the once keyword to ensure it only happens at the start of the script.
  • Equity Calculation: Equity is calculated by adding the current capital to the strategy’s profit. This value is updated continuously as the strategy runs.
  • Tracking Maximum Equity: The maximum equity (maxcapital) is updated by comparing the current equity to the previous maximum equity and storing the higher value.
  • Condition Checks: Conditions are checked to perform actions based on whether the current equity is less than or greater than the maximum recorded equity.
  • Handling Recent Trades: For the last 100 trades, an array ($myEquity) is used to store equity values per trade. The script checks if the number of trades has reached 100 to start overwriting the oldest equity values.
  • Loop for Recent Trades: A loop runs from the current trade count down to the trade count minus 99 (or to the first trade if fewer than 100 trades have occurred) to determine the maximum equity in this window.

This snippet is useful for tracking performance in trading strategies, allowing developers to implement features based on historical and recent trading data.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/capital-curve-as-an-indicator/#post-222482

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
ROBERTO New
Code artist, my biography is a blank page waiting to be scripted. Imagine a bio so awesome it hasn't been coded yet.
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Logo Logo
Loading...